Skip to content

Fix some types of arguments in ast module #14247

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

koyuki7w
Copy link

@koyuki7w koyuki7w commented Jun 9, 2025

The ast document states that Try/TryStar have an attribute of type excepthandler, not ExceptHandler. Also Module has of type type_ignore, not TypeIgnore.

This comment has been minimized.

@brianschubert
Copy link
Contributor

brianschubert commented Jun 9, 2025

Hmm, this doesn't look right. Where in the docs does it indicate this? Are you maybe going by the grammar at the top of the docs, and missed the definitions of type_ignore/execpthandler further down? E.g. type_ignore in the grammar stands for a TypeIgnore node:

    type_ignore = TypeIgnore(int lineno, string tag)

Looking at the nodes at runtime suggests that the current stubs are correct:

>>> ast.parse("# type: ignore", type_comments=True).type_ignores
[<ast.TypeIgnore object at 0x7000551b31d0>]

>>> ast.parse("""
... try:
...     pass
... except:
...     pass
... """).body[0].handlers
[<ast.ExceptHandler object at 0x7000551c8990>]

@AlexWaygood AlexWaygood closed this Jun 9, 2025
@AlexWaygood AlexWaygood reopened this Jun 9, 2025
Copy link
Contributor

github-actions bot commented Jun 9, 2025

Diff from mypy_primer, showing the effect of this PR on open source code:

mypy (https://github.com/python/mypy)
+ mypy/fastparse.py:888: error: "type_ignore" has no attribute "tag"  [attr-defined]
+ mypy/fastparse.py:890: error: "type_ignore" has no attribute "lineno"  [attr-defined]
+ mypy/fastparse.py:892: error: "type_ignore" has no attribute "lineno"  [attr-defined]
+ mypy/fastparse.py:1378: error: "excepthandler" has no attribute "name"  [attr-defined]
+ mypy/fastparse.py:1378: error: Argument 2 to "set_line" of "ASTConverter" has incompatible type "excepthandler"; expected "Union[expr, stmt, ExceptHandler]"  [arg-type]
+ mypy/fastparse.py:1380: error: "excepthandler" has no attribute "type"  [attr-defined]
+ mypy/fastparse.py:1381: error: "excepthandler" has no attribute "body"  [attr-defined]

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.

3 participants