improvement: Add AST subclass constructors#11880
Merged
JelleZijlstra merged 16 commits intopython:mainfrom May 18, 2024
bzoracler:ast-subclass-constructors
Merged
improvement: Add AST subclass constructors#11880JelleZijlstra merged 16 commits intopython:mainfrom bzoracler:ast-subclass-constructors
JelleZijlstra merged 16 commits intopython:mainfrom
bzoracler:ast-subclass-constructors
Conversation
improvement: shift `type_comment` attribute to classes which contain it
This comment has been minimized.
This comment has been minimized.
AlexWaygood
reviewed
May 9, 2024
AlexWaygood
reviewed
May 9, 2024
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Member
|
Thanks for this, I had been thinking of adding it but hadn't gotten around to it. |
stdlib/_ast.pyi
Outdated
| __match_args__ = ("body", "type_ignores") | ||
| body: list[stmt] | ||
| type_ignores: list[TypeIgnore] | ||
| def __init__(self, body: list[stmt], type_ignores: list[TypeIgnore]) -> None: ... |
Member
There was a problem hiding this comment.
These list fields should default to the empty list on 3.13+.
| decorator_list: list[expr], | ||
| returns: expr | None = None, | ||
| *, | ||
| type_comment: str | None = None, |
Member
There was a problem hiding this comment.
I don't think type_comment needs to be keyword-only on this overload.
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
I didn't add these other changes from Python 3.13 runtime to this PR:
|
This comment has been minimized.
This comment has been minimized.
Member
It's used by the constructor of AST nodes to figure out what defaults to use. I did document it, so I think we should add it to the |
This comment has been minimized.
This comment has been minimized.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: pyp (https://github.com/hauntsaninja/pyp)
+ pyp.py:585: error: "AST" has no attribute "lineno" [attr-defined]
+ pyp.py:586: error: "AST" has no attribute "end_lineno" [attr-defined]
sphinx (https://github.com/sphinx-doc/sphinx)
+ sphinx/ext/autodoc/preserve_defaults.py: note: In function "get_default_value":
+ sphinx/ext/autodoc/preserve_defaults.py:114:12: error: "AST" has no attribute "lineno" [attr-defined]
+ sphinx/ext/autodoc/preserve_defaults.py:114:31: error: "AST" has no attribute "end_lineno" [attr-defined]
+ sphinx/ext/autodoc/preserve_defaults.py:115:26: error: "AST" has no attribute "lineno" [attr-defined]
+ sphinx/ext/autodoc/preserve_defaults.py:116:25: error: "AST" has no attribute "col_offset" [attr-defined]
+ sphinx/ext/autodoc/preserve_defaults.py:116:45: error: "AST" has no attribute "end_col_offset" [attr-defined]
mypy (https://github.com/python/mypy)
+ mypy/fastparse.py:1998: error: "Index" has no attribute "col_offset" [attr-defined]
+ mypy/fastparse.py:2001: error: "Slice" has no attribute "col_offset" [attr-defined]
+ mypy/fastparse.py:2002: error: Argument 1 to "Tuple" has incompatible type "List[slice]"; expected "List[expr]" [arg-type]
pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/assertion/rewrite.py:839: error: Argument 1 to "Dict" has incompatible type "list[Constant]"; expected "list[expr | None]" [arg-type]
+ src/_pytest/assertion/rewrite.py:839: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/_pytest/assertion/rewrite.py:839: note: Consider using "Sequence" instead, which is covariant
+ src/_pytest/assertion/rewrite.py:932: error: Argument 3 to "If" has incompatible type "list[If]"; expected "list[stmt]" [arg-type]
+ src/_pytest/assertion/rewrite.py:932: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/_pytest/assertion/rewrite.py:932: note: Consider using "Sequence" instead, which is covariant
+ src/_pytest/assertion/rewrite.py:938: error: Argument 1 to "Assign" has incompatible type "list[Name]"; expected "list[expr]" [arg-type]
+ src/_pytest/assertion/rewrite.py:938: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/_pytest/assertion/rewrite.py:938: note: Consider using "Sequence" instead, which is covariant
+ src/_pytest/assertion/rewrite.py:963: error: Argument 1 to "Assign" has incompatible type "list[Name]"; expected "list[expr]" [arg-type]
+ src/_pytest/assertion/rewrite.py:963: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/_pytest/assertion/rewrite.py:963: note: Consider using "Sequence" instead, which is covariant
+ src/_pytest/assertion/rewrite.py:1145: error: Argument 1 to "Tuple" has incompatible type "list[Constant]"; expected "list[expr]" [arg-type]
+ src/_pytest/assertion/rewrite.py:1145: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/_pytest/assertion/rewrite.py:1145: note: Consider using "Sequence" instead, which is covariant
+ src/_pytest/assertion/rewrite.py:1146: error: Argument 1 to "Tuple" has incompatible type "list[Name]"; expected "list[expr]" [arg-type]
+ src/_pytest/assertion/rewrite.py:1146: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/_pytest/assertion/rewrite.py:1146: note: Consider using "Sequence" instead, which is covariant
+ src/_pytest/assertion/rewrite.py:1147: error: Argument 1 to "Tuple" has incompatible type "list[Constant]"; expected "list[expr]" [arg-type]
+ src/_pytest/assertion/rewrite.py:1147: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/_pytest/assertion/rewrite.py:1147: note: Consider using "Sequence" instead, which is covariant
+ src/_pytest/assertion/rewrite.py:1151: error: Argument 2 to "BoolOp" has incompatible type "list[Name]"; expected "list[expr]" [arg-type]
+ src/_pytest/assertion/rewrite.py:1151: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
+ src/_pytest/assertion/rewrite.py:1151: note: Consider using "Sequence" instead, which is covariant
+ testing/test_assertrewrite.py:133: error: "AST" has no attribute "lineno" [attr-defined]
+ testing/test_assertrewrite.py:134: error: "AST" has no attribute "col_offset" [attr-defined]
+ testing/test_assertrewrite.py:135: error: "AST" has no attribute "end_lineno" [attr-defined]
+ testing/test_assertrewrite.py:136: error: "AST" has no attribute "end_col_offset" [attr-defined]
werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/routing/rules.py:785: error: Argument 1 to "JoinedStr" has incompatible type "list[AST]"; expected "list[expr]" [arg-type]
+ src/werkzeug/routing/rules.py:788: error: List item 0 has incompatible type "AST"; expected "expr" [list-item]
+ src/werkzeug/routing/rules.py:788: error: List item 1 has incompatible type "AST"; expected "expr" [list-item]
+ src/werkzeug/routing/rules.py:818: error: "AST" has no attribute "lineno" [attr-defined]
+ src/werkzeug/routing/rules.py:820: error: "AST" has no attribute "end_lineno" [attr-defined]
+ src/werkzeug/routing/rules.py:820: error: "AST" has no attribute "lineno" [attr-defined]
+ src/werkzeug/routing/rules.py:822: error: "AST" has no attribute "col_offset" [attr-defined]
+ src/werkzeug/routing/rules.py:824: error: "AST" has no attribute "end_col_offset" [attr-defined]
+ src/werkzeug/routing/rules.py:824: error: "AST" has no attribute "col_offset" [attr-defined]
ibis (https://github.com/ibis-project/ibis)
+ ibis/backends/bigquery/udf/core.py:66: error: Missing positional arguments "args", "keywords" in call to "Call" [call-arg]
+ ibis/backends/bigquery/udf/core.py:79: error: Missing positional arguments "args", "keywords" in call to "Call" [call-arg]
+ ibis/backends/bigquery/udf/core.py:85: error: Missing positional arguments "args", "keywords" in call to "Call" [call-arg]
+ ibis/backends/bigquery/udf/core.py:85: error: Missing positional argument "value" in call to "Attribute" [call-arg]
+ ibis/backends/bigquery/udf/core.py:95: error: Missing positional arguments "args", "keywords" in call to "Call" [call-arg]
flake8-pyi (https://github.com/PyCQA/flake8-pyi)
+ pyi.py:2249: error: "AST" has no attribute "lineno" [attr-defined]
+ pyi.py:2249: error: "AST" has no attribute "col_offset" [attr-defined]
streamlit (https://github.com/streamlit/streamlit)
+ lib/streamlit/runtime/scriptrunner/magic.py: note: In function "_build_st_write_call":
+ lib/streamlit/runtime/scriptrunner/magic.py:166:12: error: Unexpected keyword argument "kwargs" for "Call"; did you mean "args"? [call-arg]
+ note: "Call" defined here
+ lib/streamlit/runtime/scriptrunner/magic.py:166:12: error: Unexpected keyword argument "starargs" for "Call" [call-arg]
+ note: "Call" defined here
|
JelleZijlstra
added a commit
to PyCQA/flake8-pyi
that referenced
this pull request
May 18, 2024
python/typeshed#11880 correctly points out that not all AST nodes have a line and column number.
JelleZijlstra
added a commit
to JelleZijlstra/pytest
that referenced
this pull request
May 18, 2024
python/typeshed#11880 adds more precise types for AST nodes. I'm submitting some changes to adapt pytest to these changes.
AlexWaygood
pushed a commit
to PyCQA/flake8-pyi
that referenced
this pull request
May 18, 2024
python/typeshed#11880 correctly points out that not all AST nodes have a line and column number.
JelleZijlstra
added a commit
to JelleZijlstra/werkzeug
that referenced
this pull request
May 18, 2024
Adapting to changes in python/typeshed#11880. This mostly adds more precise types for individual pieces of AST.
JelleZijlstra
added a commit
to JelleZijlstra/sphinx
that referenced
this pull request
May 18, 2024
Followup from python/typeshed#11880. Not all AST nodes have a lineno; in this file only parameter defaults are passed to this function, which are always ast.expr.
bluetech
pushed a commit
to pytest-dev/pytest
that referenced
this pull request
May 18, 2024
python/typeshed#11880 adds more precise types for AST nodes. I'm submitting some changes to adapt pytest to these changes.
JelleZijlstra
added a commit
to JelleZijlstra/streamlit
that referenced
this pull request
May 18, 2024
See https://docs.python.org/3.10/library/ast.html#abstract-grammar for a reference on the attributes `ast.Call` takes. A future version of mypy will give a type error for this code (python/typeshed#11880).
vdonato
added a commit
to streamlit/streamlit
that referenced
this pull request
May 21, 2024
See https://docs.python.org/3.10/library/ast.html#abstract-grammar for a reference on the attributes `ast.Call` takes. A future version of mypy will give a type error for this code (python/typeshed#11880). Co-authored-by: Vincent Donato <vincent@streamlit.io>
davidism
pushed a commit
to JelleZijlstra/werkzeug
that referenced
this pull request
Aug 20, 2024
Adapting to changes in python/typeshed#11880. This mostly adds more precise types for individual pieces of AST.
benjamin-awd
pushed a commit
to benjamin-awd/streamlit
that referenced
this pull request
Sep 29, 2024
See https://docs.python.org/3.10/library/ast.html#abstract-grammar for a reference on the attributes `ast.Call` takes. A future version of mypy will give a type error for this code (python/typeshed#11880). Co-authored-by: Vincent Donato <vincent@streamlit.io>
asmeralt
pushed a commit
to asmeralt/streamlit
that referenced
this pull request
Sep 29, 2025
See https://docs.python.org/3.10/library/ast.html#abstract-grammar for a reference on the attributes `ast.Call` takes. A future version of mypy will give a type error for this code (python/typeshed#11880). Co-authored-by: Vincent Donato <vincent@streamlit.io>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #8378
ast.ASTno longer owns attributes which may not exist on all subclasses (node locations,type_comment). These are all shifted to subclasses which actually own the attributes (d5e0838, 75a2e8e, f9a2cb9)ast.ASTno longer has the constructor__init__(self, *args: Any, **kwargs: Any).ast.ASTwhich have a non-empty_fields: ClassVar[tuple[str, ...]]or_attributes: ClassVar[tuple[str, ...]]now have their own__init__constructor (d886a45). The constructor has:_fields, and_attributes. These are expressed using a combination oftyping.TypedDictandtyping_extensions.Unpack.ast.fix_missing_locations.Updates:
ast.expr_contextare not required to be passed to the constructor, but they will not exist on node instances for Python < 3.13.listare not required to be passed to the constructor for Python >= 3.13.