python / python/cpython

Add to AST location info for compound exception headers or keywords

Đang mở
#129,157 3 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

interpreter-core topic-parser type-feature
Ngôn ngữ chính
Python
Star
77.2k
Fork
35.9k
Chỉ số merge pull request
Chỉ số pull request đang chờ

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Không có tệp mã nguồn hoặc bài kiểm thử nào được nêu. Hãy bắt đầu bằng việc xem xét hành vi về vị trí AST được mô tả ở đây và trong PR gh-129162 được liên kết, sau đó so sánh các issue liên quan đến các khối except và định nghĩa lớp. Công việc được coi là hoàn tất khi các phần đầu hoặc từ khóa của câu lệnh phức hợp có vị trí AST có thể sử dụng được, mà không quy các ngoại lệ của context manager cho toàn bộ phần thân.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
python
Lĩnh vực
compilers
Loại issue
Tính năng
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.