Add to AST location info for compound exception headers or keywords
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
The location information in the AST for a compound statement spans the whole body.
For example:
>>> src = """with A as cm:
... 1
... 2
... 3
... """
Output:
"Module(body=[With(items=[withitem(context_expr=Name(id='A', ctx=Load(), lineno=1, col_offset=5, end_lineno=1, end_col_offset=6), optional_vars=Name(id='cm', ctx=Store(), lineno=1, col_offset=10, end_lineno=1, end_col_offset=12))], body=[Expr(value=Constant(value=1, lineno=2, col_offset=4, end_lineno=2, end_col_offset=5), lineno=2, col_offset=4, end_lineno=2, end_col_offset=5), Expr(value=Constant(value=2, lineno=3, col_offset=4, end_lineno=3, end_col_offset=5), lineno=3, col_offset=4, end_lineno=3, end_col_offset=5), Expr(value=Constant(value=3, lineno=4, col_offset=4, end_lineno=4, end_col_offset=5), lineno=4, col_offset=4, end_lineno=4, end_col_offset=5)], lineno=1, col_offset=0, end_lineno=4, end_col_offset=5)])"
If __enter__ or __exit__ of the context manager raises an exception, we don't want the location of the exception to span the entire body. Ideally it would be the location of the with keyword (my view) or the entire with x as y statement (@ericsnowcurrently's view). However, we don't have those locations in the AST so currently we hilight the location of the expression that creates the context manager:
>>> class C:
... def __enter__(self): 1/0
... def __exit__(*args): pass
...
>>> with C() as c: pass
...
Traceback (most recent call last):
File "<python-input-11>", line 1, in <module>
with C() as c: pass
~^^
File "<python-input-10>", line 2, in __enter__
def __enter__(self): 1/0
~^~
ZeroDivisionError: division by zero
We have similar issue with except blocks and class definitions.
Can we add location information of the keywords, or of the header of a compound expression, to the AST?
@pablogsal @lysnikolaou
Linked PRs
- gh-129162
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
未指定源文件或测试。首先检查此处和链接的 PR gh-129162 中描述的 AST 位置行为,然后比较相关的 except 块和类定义 issue。当复合语句的语句头或关键字具有可用的 AST 位置,且不会将上下文管理器异常归因于整个主体时,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- compilers
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100