Propagate magic opt-out flags through MagicAlias - #15365
Open
l46983284-cpu wants to merge 1 commit into
Open
Conversation
run_line_magic/run_cell_magic read MAGIC_NO_VAR_EXPAND_ATTR,
needs_local_scope and MAGIC_OUTPUT_CAN_BE_SILENCED off the registered
magic object. A MagicAlias carries none of them, so aliased calls to
%time et al. had {var} interpolated from user_ns before execution and
lost access to function locals (GH ipython#13064).
Give MagicAlias property delegates that resolve the aliased magic at
call time, preserving the documented late binding, and report the
target's flags. The lookup walks alias chains iteratively and stops on
cycles, so a self-referencing alias terminates and raises the original
UsageError instead of recursing forever. A missing target yields False,
i.e. today's behavior.
Adds five regression tests in tests/test_magic.py.
Signed-off-by: Alex Chen <l46983284@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13064
Magics created via
%alias_magicdon't carry the target magic's opt-out flags.run_line_magic/run_cell_magicreadMAGIC_NO_VAR_EXPAND_ATTR,needs_local_scopeandMAGIC_OUTPUT_CAN_BE_SILENCEDoff the registered object; on aMagicAliasthose attributes don't exist, so{var}in an aliased call gets interpolated fromuser_nsbefore execution (the stale-if-string symptom from the issue), and aliases ofneeds_local_scopemagics lose access to function locals.Give
MagicAliasproperty delegates that resolve the aliased magic at call time — the class already documents late binding — and report the target's flags. The lookup walks alias chains iteratively and stops on cycles, so a self-referencing alias terminates and raises the originalUsageErrorinstead of recursing forever. A missing target yieldsFalse, i.e. today's behavior.Regression tests (all in
tests/test_magic.py):test_alias_magic_no_var_expand— line alias of%timedoes not expand{a}test_alias_magic_cell_no_var_expand— same through the cell path,%%timeitsetup linetest_alias_magic_needs_local_scope— alias sees the caller's function localstest_alias_magic_fstring_loop— exact repro from the issuetest_alias_magic_flag_lookup_on_self_loop— cyclic alias terminatesChecked in a fresh sandbox on upstream main (dd41c52): the new flag tests fail without the change as expected; with it, 130 passed, 6 skipped in
tests/test_magic.py.