-
-
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 1 commit
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
Limit narrowing after in only to optional types
- Loading branch information
commit 59e69d6fee1cdf121cff2d9d0403988d0c1a32b0
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 |
---|---|---|
|
@@ -1771,136 +1771,148 @@ def narrow_any_to_str_then_reassign_to_int() -> None: | |
[builtins fixtures/isinstance.pyi] | ||
|
||
[case testNarrowTypeAfterInList] | ||
from typing import List | ||
# flags: --strict-optional | ||
from typing import List, Optional | ||
|
||
x: List[int] | ||
y: object | ||
y: Optional[int] | ||
|
||
if y in x: | ||
reveal_type(y) # E: Revealed type is 'builtins.int' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'builtins.object' | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
if y not in x: | ||
reveal_type(y) # E: Revealed type is 'builtins.object' | ||
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: object | ||
y: Optional[B] | ||
if y in (B(), C()): | ||
reveal_type(y) # E: Revealed type is '__main__.A' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'builtins.object' | ||
if y not in (B(), C()): | ||
reveal_type(y) # E: Revealed type is 'builtins.object' | ||
reveal_type(y) # E: Revealed type is '__main__.B' | ||
else: | ||
reveal_type(y) # E: Revealed type is '__main__.A' | ||
reveal_type(y) # E: Revealed type is 'Union[__main__.B, builtins.None]' | ||
[builtins fixtures/tuple.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInNamedTuple] | ||
from typing import NamedTuple | ||
# flags: --strict-optional | ||
from typing import NamedTuple, Optional | ||
class NT(NamedTuple): | ||
x: int | ||
y: int | ||
nt: NT | ||
|
||
y: object | ||
if y in nt: | ||
reveal_type(y) # E: Revealed type is 'builtins.int' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'builtins.object' | ||
y: Optional[int] | ||
if y not in nt: | ||
reveal_type(y) # E: Revealed type is 'builtins.object' | ||
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] | ||
from typing import Dict, Union | ||
# 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: Union[int, str] | ||
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.int, builtins.str]' | ||
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.int, builtins.str]' | ||
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] | ||
from typing import List | ||
# flags: --strict-optional | ||
from typing import List, Optional | ||
|
||
x = None # type: List[int] | ||
y = None # type: object | ||
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 'builtins.object' | ||
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 'builtins.object' | ||
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 testNarrowTypeAfterInNoPromotionsOrAny] | ||
from typing import Any, List | ||
x: List[int] | ||
[case testNarrowTypeAfterInNoAnyOrObject] | ||
# flags: --strict-optional | ||
from typing import Any, List, Optional | ||
x: List[Any] | ||
z: List[object] | ||
|
||
y: Any | ||
# We never narrow down from Any | ||
y: Optional[int] | ||
if y in x: | ||
reveal_type(y) # E: Revealed type is 'Any' | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Any' | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
|
||
z: float | ||
# We never use promotions | ||
if z not in x: | ||
reveal_type(z) # E: Revealed type is 'builtins.float' | ||
if y not in z: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(z) # E: Revealed type is 'builtins.float' | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/list.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInUserDefined] | ||
from typing import Container, Union | ||
# flags: --strict-optional | ||
from typing import Container, Optional | ||
|
||
class C(Container[int]): | ||
def __contains__(self, item: object) -> bool: | ||
return item is 'surprise' | ||
|
||
y: Union[int, str] | ||
y: Optional[int] | ||
# We never trust user defined types | ||
if y in C(): | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.str]' | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.str]' | ||
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.str]' | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
else: | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.str]' | ||
reveal_type(y) # E: Revealed type is 'Union[builtins.int, builtins.None]' | ||
[typing fixtures/typing-full.pyi] | ||
[builtins fixtures/list.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInStrictOptional] | ||
[case testNarrowTypeAfterInSet] | ||
# flags: --strict-optional | ||
from typing import Optional, Set | ||
s: Set[str] | ||
|
@@ -1917,7 +1929,7 @@ else: | |
[builtins fixtures/set.pyi] | ||
[out] | ||
|
||
[case testNarrowTypeAfterInStrictOptional2] | ||
[case testNarrowTypeAfterInTypedDict] | ||
# flags: --strict-optional | ||
from typing import Optional | ||
from mypy_extensions import TypedDict | ||
|
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 test for optional item type (e.g.
List[Optional[int]]
).