python / python/cpython

Add to AST location info for compound exception headers or keywords

オープン
#129,157 コメント 3 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core topic-parser type-feature
主要言語
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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ソースファイルやテストは指定されていません。まず、ここおよびリンクされた PR gh-129162 で説明されている AST の位置情報の挙動を確認し、その後、関連する except ブロックとクラス定義の issue を比較してください。複合文のヘッダーまたはキーワードに利用可能な AST の位置情報があり、コンテキストマネージャーの例外が本体全体に帰属されない状態になれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。