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

Adapt ast types for mypy 0.990 #9129

Closed
kasium opened this issue Nov 8, 2022 · 7 comments · Fixed by #9130
Closed

Adapt ast types for mypy 0.990 #9129

kasium opened this issue Nov 8, 2022 · 7 comments · Fixed by #9130

Comments

@kasium
Copy link
Contributor

kasium commented Nov 8, 2022

With mypy 0.990 the complex/int/float promotion was changed (see python/mypy#14030).
Therefore checks like isinstance(node, ast.Num) and isinstance(node.n, int) fails with strict mode enabled.

The recommendation by mypy is to change complex to Union[int, float, complex]. Are there any objections to do so in the ast module?

@srittau
Copy link
Collaborator

srittau commented Nov 8, 2022

I'm not sure what you would want to change. Stubs don't use isinstance checks.

@kasium
Copy link
Contributor Author

kasium commented Nov 8, 2022

Ah sorry;

so in my code I use code like isinstance(node, ast.Num) and isinstance(node.n, int) and this fails now with mypy 0.990 (see python/mypy#14030). The recommendation by mypy is to adapt the types of ast.Num.n to be Union[int, float, complex] instead of complex (which implies int and float but not explicitly enough for mypy).

Proposal: change complex types in _ast.pyi to be Union[int, float, complex]

@ilevkivskyi
Copy link
Member

FWIW I also think n for both Num and Constant should be Union[int, float, complex]. This is more correct, and doesn't implicitly rely on type promotions.

@kasium
Copy link
Contributor Author

kasium commented Nov 8, 2022

Okay, let me then give a PR a try

kasium added a commit to kasium/typeshed that referenced this issue Nov 8, 2022
Since mypy  0.990 type promotions was limited.
This means that complex is not longer promoted to int/float, therefore
we should adapt the types to list all possible types

Closes python#9129
@ilevkivskyi
Copy link
Member

Hm, as noticed in the PR, it looks like tpyeshed is using a flake8 lint rule that actually prohibits int | float. This is bad, there is a chance there are some other places with incorrect plain complex annotations.

@srittau I would propose to disable this rule globally.

@ilevkivskyi
Copy link
Member

I briefly scrolled through and found only other problematic annotations in typed-ast https://github.com/python/typeshed/blob/main/stubs/typed-ast/typed_ast/ast27.pyi#L241 and https://github.com/python/typeshed/blob/main/stubs/typed-ast/typed_ast/ast3.pyi#L260. @kasium maybe you can add these to your PR?

@kasium
Copy link
Contributor Author

kasium commented Nov 8, 2022

sure!

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 a pull request may close this issue.

3 participants