Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Comment lines above a cell magic make it parse as a line magic named `%name`, with a confusing error

未关闭
#15,312 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
jupyter, python
领域
tooling

调研方向

在 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:

  1. 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.

  2. Possibly the parse itself. A leading_comment_lines cleanup transform in the spirit of leading_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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ipython/ipython 的其他 Issue

查看 ipython/ipython 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。