-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Narrow types after 'in' operator #4072
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b89f03
Narrow types after 'in' operator
ilevkivskyi 4ece088
Be safe: no Any or promotions
ilevkivskyi 59e69d6
Limit narrowing after in only to optional types
ilevkivskyi a23ad9d
Fix strict-optional
ilevkivskyi ff33241
Address CR
ilevkivskyi 42d7cc7
Add one more test
ilevkivskyi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1757,7 +1757,6 @@ if isinstance(x, str, 1): # E: Too many arguments for "isinstance" | |
reveal_type(x) # E: Revealed type is 'builtins.int' | ||
[builtins fixtures/isinstancelist.pyi] | ||
|
||
|
||
[case testIsinstanceNarrowAny] | ||
from typing import Any | ||
|
||
|
@@ -1770,3 +1769,180 @@ def narrow_any_to_str_then_reassign_to_int() -> None: | |
reveal_type(v) # E: Revealed type is 'Any' | ||
|
||
[builtins fixtures/isinstance.pyi] | ||
|
||
[case testNarrowTypeAfterInList] | ||
# flags: --strict-optional | ||
from typing import List, Optional | ||
|
||
x: List[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. Add test for optional item type (e.g. |
||
y: Optional[int] | ||
|
||
if y in x: | ||
reveal_type(y) # E: Revealed type is 'builtins.int' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
if y not in x: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'builtins.int' | ||
[builtins fixtures/list.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInListNonOverlapping] | ||
# flags: --strict-optional | ||
from typing import List, Optional | ||
|
||
x: List[str] | ||
y: Optional[int] | ||
|
||
if y in x: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
[builtins fixtures/list.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInTuple] | ||
# flags: --strict-optional | ||
from typing import Optional | ||
class A: pass | ||
class B(A): pass | ||
class C(A): pass | ||
|
||
y: Optional[B] | ||
if y in (B(), C()): | ||
reveal_type(y) # E: Revealed type is '__main__.B' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[__main__.B, builtins.None]' | ||
[builtins fixtures/tuple.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInNamedTuple] | ||
# flags: --strict-optional | ||
from typing import NamedTuple, Optional | ||
class NT(NamedTuple): | ||
x: int | ||
y: int | ||
nt: NT | ||
|
||
y: Optional[int] | ||
if y not in nt: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'builtins.int' | ||
[builtins fixtures/tuple.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInDict] | ||
# flags: --strict-optional | ||
from typing import Dict, Optional | ||
x: Dict[str, str] | ||
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. Use a different type for values to make sure that this works against the key type. |
||
y: Optional[str] | ||
|
||
if y in x: | ||
reveal_type(y) # E: Revealed type is 'builtins.str' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.str, builtins.None]' | ||
if y not in x: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.str, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'builtins.str' | ||
[builtins fixtures/dict.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInList_python2] | ||
# flags: --strict-optional | ||
from typing import List, Optional | ||
|
||
x = [] # type: List[int] | ||
y = None # type: Optional[int] | ||
|
||
# TODO: Fix running tests on Python 2: "Iterator[int]" has no attribute "next" | ||
if y in x: # type: ignore | ||
reveal_type(y) # E: Revealed type is 'builtins.int' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
if y not in x: # type: ignore | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'builtins.int' | ||
|
||
[builtins_py2 fixtures/python2.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInNoAnyOrObject] | ||
# flags: --strict-optional | ||
from typing import Any, List, Optional | ||
x: List[Any] | ||
z: List[object] | ||
|
||
y: Optional[int] | ||
if y in x: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
|
||
if y not in z: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/list.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInUserDefined] | ||
# flags: --strict-optional | ||
from typing import Container, Optional | ||
|
||
class C(Container[int]): | ||
def __contains__(self, item: object) -> bool: | ||
return item is 'surprise' | ||
|
||
y: Optional[int] | ||
# We never trust user defined types | ||
if y in C(): | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
if y not in C(): | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/list.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInSet] | ||
# flags: --strict-optional | ||
from typing import Optional, Set | ||
s: Set[str] | ||
|
||
y: Optional[str] | ||
if y in {'a', 'b', 'c'}: | ||
reveal_type(y) # E: Revealed type is 'builtins.str' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.str, builtins.None]' | ||
if y not in s: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.str, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'builtins.str' | ||
[builtins fixtures/set.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInTypedDict] | ||
# flags: --strict-optional | ||
from typing import Optional | ||
from mypy_extensions import TypedDict | ||
class TD(TypedDict): | ||
a: int | ||
b: str | ||
td: TD | ||
|
||
def f() -> None: | ||
x: Optional[str] | ||
if x not in td: | ||
return | ||
reveal_type(x) # E: Revealed type is 'builtins.str' | ||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/dict.pyi] | ||
[out] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Add docstring.