-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Model exact narrowing with type(x) checks #20703
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
Changes from 2 commits
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 |
|---|---|---|
|
|
@@ -3152,7 +3152,7 @@ else: | |
| reveal_type(y) # N: Revealed type is "builtins.str" | ||
| [builtins fixtures/isinstance.pyi] | ||
|
|
||
| [case testTypeEqualsCheckUsingIsNonOverlappingChild-xfail] | ||
| [case testTypeEqualsCheckUsingIsNonOverlappingChild] | ||
| # flags: --strict-equality --warn-unreachable | ||
| from typing import Union | ||
|
|
||
|
|
@@ -3168,6 +3168,32 @@ def main(x: Union[B, C]): | |
| reveal_type(x) # N: Revealed type is "__main__.B | __main__.C" | ||
| [builtins fixtures/isinstance.pyi] | ||
|
|
||
| [case testTypeEqualsCheckTypeVar] | ||
| # flags: --strict-equality --warn-unreachable | ||
| from typing import TypeVar | ||
|
|
||
| class A: ... | ||
| class B: ... | ||
|
|
||
| T = TypeVar("T", A, B) | ||
|
|
||
| def f1(self, obj: T) -> T: | ||
| if type(obj) == A: | ||
| return A() | ||
| elif type(obj) == B: | ||
| return B() | ||
| raise | ||
|
|
||
| T_value = TypeVar("T_value", A, B) | ||
|
|
||
| def f2(self, obj: T_value) -> T_value: | ||
|
Member
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 is the difference between
Member
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. Nvm, I see you just fixed it.
Collaborator
Author
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. Oh sorry, I fixed that already :-) More generally, the behaviour here isn't yet perfect. I opened this PR to fix the The test case I added highlights two preexisting issues. Ideally a) the narrowing works in the unconstrained case, b) we issue an error in both branches in the bad constrained case |
||
| if type(obj) == A: | ||
| return A() | ||
| elif type(obj) == B: | ||
| return B() | ||
| raise | ||
| [builtins fixtures/primitives.pyi] | ||
|
|
||
| [case testTypeEqualsCheckUsingDifferentSpecializedTypes] | ||
| # flags: --warn-unreachable | ||
| from collections import defaultdict | ||
|
|
||
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.
This is the test case I was targeting with this PR