Skip to content

Use %% in the @magic_arguments usage line of cell magics 🤖🤖 - #15432

Open
ShivamSharma43 wants to merge 1 commit into
ipython:mainfrom
ShivamSharma43:fix/14715-cell-magic-usage-prefix
Open

ShivamSharma43 wants to merge 1 commit into
ipython:mainfrom
ShivamSharma43:fix/14715-cell-magic-usage-prefix

Conversation

@ShivamSharma43

Copy link
Copy Markdown

🤖🤖 Fixes #14715.

The usage line built by @magic_arguments() always started with %, because MagicHelpFormatter.add_usage hard-codes that prefix. So the help of cell magics showed the line-magic form:

In [1]: %%capture?
::

  %capture [--no-stderr] [--no-stdout] [--no-display] [output]

The same happens for %%writefile, %%html and %%script.

The parser is built when @magic_arguments() runs, and at that point it has no way to know the magic kind. In the documented decorator order, @cell_magic / @register_cell_magic sit under @magic_arguments() and run first. So:

  • The _method_magic_marker and _function_magic_marker decorators now record the kind on the function, as _ipython_magic_kind (MAGIC_KIND_ATTR, next to the existing _ipython_magic_* attributes).
  • construct_parser uses a new CellMagicHelpFormatter, which uses the %% prefix, when the kind is "cell". A formatter_class passed through @kwds still wins.
  • Line and line/cell magics keep %, so %time, %timeit and %code_wrap don't change.

After the change:

  %%capture [--no-stderr] [--no-stdout] [--no-display] [output]
  %%writefile [-a] filename
  %%html [--isolated]
  %time [--no-raise-error]

If @cell_magic is applied above @magic_arguments(), the parser has already been built and the help keeps %. No built-in magic uses that order.

Separately, %%script? shows %%shebang because @cell_magic("script") names the magic differently from the method, and real_name() uses the function name. I left that out to keep this PR to the prefix.

The new test in tests/test_magic_arguments.py covers a line magic, a cell magic (both the bare and the named @cell_magic("...") form) and a line/cell magic.

🤖 Generated with Claude Code

Copilot AI balanced review requested due to automatic review settings October 11, 2026 15:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

The usage line generated by @magic_arguments always started with %, so
the help of cell magics such as %%capture, %%writefile and %%html showed
"%capture ...". The @cell_magic/@register_cell_magic markers now record
the magic kind on the function, and construct_parser uses a formatter
with the %% prefix for cell magics. Line and line/cell magics keep %.

Fixes ipython#14715

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Carreau
Carreau force-pushed the fix/14715-cell-magic-usage-prefix branch from 8ecb434 to efa5e86 Compare October 21, 2026 09:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@magic_arguments() always use % in help regardless of line/cell magic

2 participants