-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Variable incorrectly typed as None, misleading error message #1691
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
Comments
This looks like a bug to me. If you change the check from |
A minimal reproducer: from typing import Tuple, Union
Pair = Tuple[int, int]
Variant = Union[int, Pair]
def tuplify(v: Variant) -> None:
if isinstance(v, tuple):
reveal_type(v) # E: Revealed type is 'builtins.None' One way to fix this would be to make def restrict_subtype_to(e: Type, t: TypeInfo) -> Type: ...
def restrict_subtype_away(e: Type, t: TypeInfo) -> Type: ... since the second argument of |
I think it shouldn't be correct that « I think the problem here should be that |
PEP 484 implies that |
That makes sense for covariant parameters, but might be a little weird for invariant and especially contravariant parameters. At any rate, if On the other hand, we can ask the question def f(x: object) -> None:
if isinstance(x, list):
... # what is the type of x here? If the type is |
For future reference, here's a simpler repro of the problem: from typing import Tuple, Union
x = (0, 0) # type: Union[Tuple[int,int], int]
if isinstance(x, tuple):
reveal_type(x) # E: Revealed type is 'builtins.None'
else:
reveal_type(x) # E: Revealed type is 'builtins.int' |
I also find it leaves the variable in a weird state:
Note: if you reverse the instance check to check for the other side it works fine:
|
Uh oh!
There was an error while loading. Please reload this page.
While debugging a more complicated issue, I reduced it to this:
mypy running on this example detects an error on the
print
line saying:For some reason it says that the value of
v
has typeNone
. I understand why the checker may not be able to follow the negative isinstance check, but I expected the error to say something likeValue of type Union[...] is not indexable
.The text was updated successfully, but these errors were encountered: