fix(notebook): restore the %%backtest cell magic (shadowed by the same-named line magic) - #614
Open
Anai-Guo wants to merge 1 commit into
Open
Conversation
BacktestMagics defined backtest() twice: first with @cell_magic, then with @line_magic (silenced with a noqa: F811). Both decorators record the same IPython magic name AND the same method name, so the class dict keeps only the last definition. The cell entry in the magic registry therefore points at the line-magic implementation, whose signature is (self, line) -- running %%backtest raises TypeError: backtest() takes 2 positional arguments but 3 were given. Merge the two into a single @line_cell_magic dispatcher that forwards to the original bodies (now _backtest_cell / _backtest_line), which is the supported way to serve %name and %%name from one magic class. %backtest is unchanged. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.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.
The bug
BacktestMagics(investing_algorithm_framework/notebook/magic.py) definesbacktesttwice in the same class body:Both IPython decorators call
record_magic(magics, kind, magic_name, method_name)with the same method name, so the class-level table ends up asand the class dict keeps only the last definition.
Magics.__init__then resolves both entries withgetattr(self, 'backtest')— i.e. the line-magic body, whose signature is(self, line).run_cell_magiccalls the registered function asfn(line, cell), so the documented%%backtestcell magic (module docstring, README-style example in its own docstring) cannot run at all:The
# noqa: F811silenced the one linter that would have flagged it;flake8stays quiet, so this has been invisible.Verification
I executed the real
BacktestMagicsclass body (extracted withastfrommainand from this branch) against real IPython 9.17.1Magics/magics_class, instantiated it, and called through the actual magic registry — helpers (_build_parser,_run_backtest, …) stubbed so no market data is needed:The fix
Collapse the pair into a single
@line_cell_magicdispatcher — the supported way for one magic class to serve both%nameand%%name— that forwards to the two original bodies, kept verbatim as_backtest_cell/_backtest_line:%backtest strategies/my_strategy.py --start ...behaves exactly as before;%%backtestnow reaches its own implementation. Thenoqa: F811is no longer needed. The no-IPython fallback stub block is updated to match the new import.flake8 --isolated --max-line-length 79 --select=E,W,Freports the same two pre-existingW503lines before and after — nothing introduced.🤖 Generated with Claude Code