Skip to content
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

Improve typing for ast nodes which represent assignment behaviour #9326

Conversation

Domlenart
Copy link
Contributor

@Domlenart Domlenart commented Dec 4, 2022

Closes #9287.

I also decided to type ast.Assign. The new hint for it is still less broad then expr

@github-actions

This comment has been minimized.

stdlib/_ast.pyi Outdated
@@ -98,20 +98,20 @@ class Delete(stmt):
class Assign(stmt):
if sys.version_info >= (3, 10):
__match_args__ = ("targets", "value", "type_comment")
targets: list[expr]
targets: list[Tuple | Name | Attribute | Subscript | Starred]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing List

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd definitely prefer it if we left this one as it is. It feels especially problematic due to list invariance, as we can see from the mypy_primer output. The current annotation is more ergonomic, even if it's slightly less precise. The situation will get worse if we add better __init__ methods for ast nodes, as proposed in #8378

Copy link
Contributor Author

@Domlenart Domlenart Dec 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I will revert this change. I agree with @AlexWaygood, the current state is better from ergonomics standpoint.

Btw, as I am working a lot with ast module lately (writing something similar to https://www.archunit.org/ but for Python), I can take on the #8378 after this one is done and in general I'd like to contribute to improvement of typing for ast module.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can take on the #8378 after this one is done and in general I'd like to contribute to improvement of typing for ast module.

That would be great! I think #8378 would be a big improvement.

stdlib/_ast.pyi Outdated Show resolved Hide resolved
@JelleZijlstra
Copy link
Member

Personally I lean towards not changing this at all: I think it's better to reflect the declared ASDL types directly instead of narrowing things down manually.

@AlexWaygood
Copy link
Member

Personally I lean towards not changing this at all: I think it's better to reflect the declared ASDL types directly instead of narrowing things down manually.

I feel like I'm much more in favour of the changes to ast.NamedExpr and ast.AnnAssign than the other changes. Those two nodes are highly restricted in the syntax that's allowed at runtime. But for the other nodes, you have to use very big unions, which are error-prone, and don't get us much more precise typing than what we have currently.

stdlib/_ast.pyi Outdated Show resolved Hide resolved
@github-actions
Copy link
Contributor

github-actions bot commented Dec 4, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with this to go in, now that it's restricted to only AugAssign, AnnAssign and NamedExpr. I think it's a useful change.

@AlexWaygood AlexWaygood merged commit 75d5a5b into python:main Dec 11, 2022
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.

Annotation for target attribute in ast.Assign and similar nodes is too broad
3 participants