-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Type narrowing fails when narrowing Optional NewType #11995
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
Labels
Comments
It is not just about def new_types(arg: Union[Int, Float, str]) -> None:
if isinstance(arg, int):
reveal_type(arg) # Revealed type is "__main__.Int"
elif isinstance(arg, float):
reveal_type(arg) # Revealed type is "Union[__main__.Int, __main__.Float]" (NARROWING FAILED)
else:
reveal_type(arg) # Revealed type is "builtins.str" |
This happens because Line 530 in 17850b3
I don't think that this is correct 🤔 |
Funny enough, this does type-check: from typing import NewType
UserId = NewType('UserId', int)
some_id: int = UserId(1)
other_id: float = UserId(2) |
This bug appears to have been fixed. The latest version of mypy (1.5) does not exhibit the incorrect behavior indicated in the bug report. |
Fixed in #13781 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Bug Report
The bug is fairly straightforward. When narrowing a
Union
ofNewType
types withisinstance
, the narrowing fails if one of the possible types of the variable isNone
.To Reproduce
https://mypy-play.net/?mypy=0.931&python=3.10&gist=557b54a098f8ed366fa62ff380ec12c8
Expected Behavior
All revealed types after
isinstance(arg, float)
would bebuiltins.float
or__main__.Float
.Actual Behavior
The revealed type in the buggy case is still
Union[__main__.Int, __main__.Float]
.The text was updated successfully, but these errors were encountered: