-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Changes from all commits
9b89f03
4ece088
59e69d6
a23ad9d
ff33241
42d7cc7
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 |
---|---|---|
|
@@ -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,209 @@ 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] | ||
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 testNarrowTypeAfterInListOfOptional] | ||
# flags: --strict-optional | ||
from typing import List, Optional | ||
|
||
x: List[Optional[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. Here's another special case: What if the container is a nested one, say 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. a) If the container is x: Optional[int]
lst: Optional[List[int]]
nested: List[List[Any]]
if lst in nested:
reveal_type(lst) # List[int]
if x in nested:
reveal_type(x) # Optional[int] There is already a test for non-overlapping items 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. OK, added one more test. |
||
y: Optional[int] | ||
|
||
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 'Union[builtins.int, builtins.None]' | ||
[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 testNarrowTypeAfterInListNested] | ||
# flags: --strict-optional | ||
from typing import List, Optional, Any | ||
|
||
x: Optional[int] | ||
lst: Optional[List[int]] | ||
nested_any: List[List[Any]] | ||
|
||
if lst in nested_any: | ||
reveal_type(lst) # E: Revealed type is 'builtins.list[builtins.int]' | ||
if x in nested_any: | ||
reveal_type(x) # 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, int] | ||
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] |
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]]
).