-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Narrow is
with final types correctly
#15646
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
base: master
Are you sure you want to change the base?
Changes from all commits
99d4465
0076ae8
a0308bc
2fcace1
2372c12
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1374,7 +1374,102 @@ def g() -> None: | |
foo() | ||
[builtins fixtures/dict.pyi] | ||
|
||
[case testNarrowingIsFinalType] | ||
from typing import Type, Union | ||
from typing_extensions import final | ||
|
||
@final | ||
class Mark: ... | ||
|
||
@final | ||
class Other: ... | ||
|
||
x: Union[Type[Mark], Type[Other], Type[None], int] | ||
|
||
if x is Mark: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess we can ignore this edge case. I'd be surprised if users are passing the And even then, isn't the issue only in the opposite case, when we have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can you create a |
||
reveal_type(x) # N: Revealed type is "Type[__main__.Mark]" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Other], Type[None], builtins.int]" | ||
|
||
if x is not Mark: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Other], Type[None], builtins.int]" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Type[__main__.Mark]" | ||
|
||
y: Type[Mark] = Mark | ||
if x is y: | ||
reveal_type(x) # N: Revealed type is "Type[__main__.Mark]" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Other], Type[None], builtins.int]" | ||
|
||
if x is not y: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Other], Type[None], builtins.int]" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Type[__main__.Mark]" | ||
|
||
if x is type(None): | ||
reveal_type(x) # N: Revealed type is "Type[None]" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[__main__.Other], builtins.int]" | ||
|
||
if x is not type(None): | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[__main__.Other], builtins.int]" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Type[None]" | ||
|
||
z: Type[None] | ||
if x is z: | ||
reveal_type(x) # N: Revealed type is "Type[None]" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[__main__.Other], builtins.int]" | ||
|
||
if x is not z: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[__main__.Other], builtins.int]" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Type[None]" | ||
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testNarrowingAnyAgainstFinalType] | ||
from typing import Type, Any | ||
from typing_extensions import final | ||
|
||
@final | ||
class Mark: ... | ||
|
||
x: Any | ||
if x is Mark: | ||
reveal_type(x) # N: Revealed type is "def () -> __main__.Mark" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Any" | ||
|
||
y: Type[Any] | ||
if y is Mark: | ||
reveal_type(y) # N: Revealed type is "Type[__main__.Mark]" | ||
else: | ||
reveal_type(y) # N: Revealed type is "Type[Any]" | ||
|
||
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testNarrowingIsNonFinalType] | ||
from typing import Type, Union | ||
|
||
class Mark: ... | ||
|
||
x: Union[Type[Mark], Type[None], int] | ||
|
||
if x is Mark: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[None], builtins.int]" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this not narrow the type to |
||
else: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[None], builtins.int]" | ||
|
||
y: Type[Mark] = Mark | ||
if x is y: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[None], builtins.int]" | ||
else: | ||
reveal_type(x) # N: Revealed type is "Union[Type[__main__.Mark], Type[None], builtins.int]" | ||
[builtins fixtures/isinstancelist.pyi] | ||
[case testNarrowingOptionalEqualsNone] | ||
from typing import Optional | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the RHS of the
is
operator is a specialized generic class likelist[int]
? Theis
conditional will always evaluate to False then.