Comment lines above a cell magic make it parse as a line magic named `%name`, with a confusing error
还没有人认领这个 Issue。
评估
调研方向
在 inputtransformer2 中开始,重现 %%time 上方有注释的情况,并将 cell-magic 检测与 leading_empty_lines 清理转换进行比较。检查 escaped commands 相关的现有行为以及文档中说明的 magic 注册 API。完成标准是:报告的输入能够产生准确的 cell-magic 错误,或按提议进行清理,同时考虑并通过测试覆盖以编程方式注册的 %foo 的例外行为。
由索引模型根据 Issue 内容生成。
描述
Putting a comment above a cell magic fails like this:
In [1]: # setup
...: %%time
...: pass
UsageError: Line magic function `%%time` not found.
Blank lines above a cell magic are fine, since the leading_empty_lines cleanup transform drops them. Comment lines are the one thing that breaks.
From reading inputtransformer2, what seems to happen is: cell magic detection only looks at the first line of the cell, so the %%time line falls through to the escaped-command transform, which strips one % as the escape character and takes %time as the magic name. Names registered through the documented APIs come from Python function names, which can't contain %, so the lookup always fails. As far as we can tell the current parse serves no reachable purpose, with one exception: register_magic_function doesn't validate names, so a magic literally named %foo can be registered programmatically, and a mid-cell %%foo line does invoke it. We doubt anyone depends on that, but it's why we're not suggesting a hard error.
Two things seem worth improving, assuming we're not missing some history here:
-
The error message. The user wrote a cell magic, and the message says a line magic wasn't found. The doubled
%%in the message also hides that the failed lookup was for%time. A message saying that a cell magic must be the first line of the cell would point at the actual mistake. -
Possibly the parse itself. A
leading_comment_linescleanup transform in the spirit ofleading_empty_lines, dropping leading blank and comment lines when the first real line starts with%%, would make the cell mean what the author obviously intended. A line starting with%%can't begin a Python statement, and a string continuation can't be the first real line of a cell, so we couldn't find currently valid code whose meaning would change, apart from the pathological registration above.
We ran into this through notebook tooling where both humans and LLM agents tend to put a narration comment at the top of a cell. We've added the transform to our own InteractiveShell subclass (execnb) and it has behaved well so far. Happy to send a PR for either or both parts, and equally happy to be told what nuance we've missed.
Tested on IPython 9.15.0.
- 主要语言
- Python
- 星标
- 16.8k
- 派生
- 4.5k
- 平均合并
- 1 天 2 小时
- 30 天内合并 PR
- 6
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
ipython/ipython 的其他 Issue
-
难度 1/5 1 小时以内 新手友好度 78/100
-
难度 2/5 1-3 小时 新手友好度 64/100
-
bug magics
难度 2/5 1-3 小时 新手友好度 72/100
-
难度 5/5 一周以上 新手友好度 25/100
相似的 Issue
-
enhancement
难度 2/5 1-3 小时 新手友好度 70/100
canonical/paas-charm#368 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 75/100
-
tech debt
难度 2/5 1-3 小时 新手友好度 75/100
-
难度 1/5 1 小时以内 新手友好度 90/100
StevenBlack/hosts#3256 ·
-
难度 1/5 1 小时以内 新手友好度 90/100
qualcomm/qai-appbuilder#275 ·