Skip to content
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

Narrow types after 'in' operator #4072

Merged
merged 6 commits into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2875,7 +2875,19 @@ def remove_optional(typ: Type) -> Type:


def builtin_item_type(tp: Type) -> Optional[Type]:
# This is only OK for built-in containers, where we know the behavior of __contains__.
"""Get the item type of a builtin container.

If 'tp' is not one of the built containers (these includes NamedTuple and TypedDict)
or if the container is not parameterized (like List or List[Any])
return None. This function is used to narrow optional types in situations like this:

x: Optional[int]
if x in (1, 2, 3):
x + 42 # OK

Note: this is only OK for built-in containers, where we know the behavior
of __contains__.
"""
if isinstance(tp, Instance):
if tp.type.fullname() in ['builtins.list', 'builtins.tuple', 'builtins.dict',
'builtins.set', 'builtins.frozenset']:
Expand Down
16 changes: 15 additions & 1 deletion test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,20 @@ else:
[builtins fixtures/list.pyi]
[out]

[case testNarrowTypeAfterInListOfOptional]
# flags: --strict-optional
from typing import List, Optional

x: List[Optional[int]]
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 List[List[x]], where x might be Any? How do we deal with various item types, such as List[int] and List[Any]?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a) If the container is List[Any], then we do nothing (there is a test for this).
b) If the container is List[List[Any]] we narrow the type (provided there is an overlap, this is consistent with how == currently treated), for example:

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 int vs str. I will add one more test for other (nested) types?

Copy link
Member Author

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -1836,7 +1850,7 @@ else:
[case testNarrowTypeAfterInDict]
# flags: --strict-optional
from typing import Dict, Optional
x: Dict[str, str]
x: Dict[str, int]
y: Optional[str]

if y in x:
Expand Down