Skip to content

Add to AST location info for compound exception headers or keywords #129157

Open

Description

@iritkatriel

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

Activity

  1. changed the title [-]Add to AST location info for compound exception headers[/-] [+]Add to AST location info for compound exception headers or keywords[/+] on Jan 21, 2025
  2. pablogsal commented on Jan 21, 2025

    @pablogsal
    Member

    On it

  3. added a commit that references this issue on Jan 21, 2025
  4. pablogsal commented on Jan 21, 2025

    @pablogsal
    Member

    Let's settle on this one first and I will take the other in other PRs

  5. added 4 commits that reference this issue on Jan 22, 2025
  6. JelleZijlstra commented on Jan 22, 2025

    @JelleZijlstra
    Member

    There's an analogous issue with for loops:

    >>> class X:
    ...     def __iter__(self): return self
    ...     def __next__(self): 1/0
    ...     
    >>> for f in X():
    ...     pass
    ...     
    Traceback (most recent call last):
      File "<python-input-9>", line 1, in <module>
        for f in X():
                 ~^^
      File "<python-input-8>", line 3, in __next__
        def __next__(self): 1/0
                            ~^~
    ZeroDivisionError: division by zero
    
  7. added a commit that references this issue on Jan 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions