You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 commentedon Nov 8, 2022
I'm not sure what you would want to change. Stubs don't use
isinstancechecks.kasium commentedon 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 ofast.Num.nto beUnion[int, float, complex]instead ofcomplex(which impliesintandfloatbut not explicitly enough for mypy).Proposal: change
complextypes in_ast.pyito beUnion[int, float, complex]ilevkivskyi commentedon Nov 8, 2022
FWIW I also think
nfor bothNumandConstantshould beUnion[int, float, complex]. This is more correct, and doesn't implicitly rely on type promotions.kasium commentedon Nov 8, 2022
Okay, let me then give a PR a try
Adapt number types in ast
ilevkivskyi commentedon 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 plaincomplexannotations.@srittau I would propose to disable this rule globally.
ilevkivskyi commentedon 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 commentedon Nov 8, 2022
sure!