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?
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]
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.
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.
Activity
srittau commented on Nov 8, 2022
I'm not sure what you would want to change. Stubs don't use isinstance checks.
kasium commented on 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 commented on Nov 8, 2022
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 commented on Nov 8, 2022
Okay, let me then give a PR a try
Adapt number types in ast
ilevkivskyi commented on Nov 8, 2022
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 commented on Nov 8, 2022
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 commented on Nov 8, 2022
sure!