Skip to content

allow subtree KeyError to bubble - #892

Merged
asottile merged 1 commit into
mainfrom
subtree-error
Sep 22, 2026
Merged

asottile merged 1 commit into
mainfrom
subtree-error

Conversation

@asottile

Copy link
Copy Markdown
Member

this behaviour was uncovered -- and I think the KeyError is actually more readable than the custom exception:

>>> Checker(ast.parse('a').body[0].value)
Traceback (most recent call last):
  File "/home/asottile/workspace/pyflakes/pyflakes/checker.py", line 738, in __init__
    scope_tp = Checker._ast_node_scope[type(tree)]
               ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: <class 'ast.Name'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    Checker(ast.parse('a').body[0].value)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/asottile/workspace/pyflakes/pyflakes/checker.py", line 740, in __init__
    raise RuntimeError('No scope implemented for the node %r' % tree)
RuntimeError: No scope implemented for the node Name(id='a', ctx=Load())

now:

>>> Checker(ast.parse('a').body[0].value)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    Checker(ast.parse('a').body[0].value)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/asottile/workspace/pyflakes/pyflakes/checker.py", line 737, in __init__
    scope_tp = Checker._ast_node_scope[type(tree)]
               ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: <class 'ast.Name'>

this behaviour was uncovered -- and I think the KeyError is actually
more readable than the custom exception:

```pytb
>>> Checker(ast.parse('a').body[0].value)
Traceback (most recent call last):
  File "/home/asottile/workspace/pyflakes/pyflakes/checker.py", line 738, in __init__
    scope_tp = Checker._ast_node_scope[type(tree)]
               ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: <class 'ast.Name'>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    Checker(ast.parse('a').body[0].value)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/asottile/workspace/pyflakes/pyflakes/checker.py", line 740, in __init__
    raise RuntimeError('No scope implemented for the node %r' % tree)
RuntimeError: No scope implemented for the node Name(id='a', ctx=Load())
```

now:

```pytb
>>> Checker(ast.parse('a').body[0].value)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    Checker(ast.parse('a').body[0].value)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/asottile/workspace/pyflakes/pyflakes/checker.py", line 737, in __init__
    scope_tp = Checker._ast_node_scope[type(tree)]
               ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: <class 'ast.Name'>
```
@asottile
asottile enabled auto-merge (squash) September 22, 2026 19:54
@asottile
asottile merged commit 4b7e358 into main Sep 22, 2026
12 checks passed
@asottile
asottile deleted the subtree-error branch September 22, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant