Skip to content

add narrowing for lists via index isinstance check #9382

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

Closed

Conversation

sbdchd
Copy link
Contributor

@sbdchd sbdchd commented Aug 30, 2020

Allows for narrowing a union of list types by indexing into the list
and checking the type of an element.

Since mypy already narrows the index expression's type, we use this
expression's type to compare against the type arguments of the lists
in the union and generate a new type that is narrower than the original.

from typing import Union, List

a: Union[List[List[int]], List[str]]

# before
if isinstance(a[0], list):
    reveal_type(a) # N: Revealed type is 'Union[builtins.list[builtins.list[builtins.int]], builtins.list[builtins.str]]'
else:
    reveal_type(a) # N: Revealed type is 'Union[builtins.list[builtins.list[builtins.int]], builtins.list[builtins.str]]'

# after
if isinstance(a[0], list):
    reveal_type(a) # N: Revealed type is 'builtins.list[builtins.list[builtins.int]]'
else:
    reveal_type(a) # N: Revealed type is 'builtins.list[builtins.str]'

fixes: #9362

Allows for narrowing a union of list types by indexing into the list
and checking the type of an element.

mypy already narrows the specific index expression.
Since mypy already narrows the index expression's type, we use the type
to compare against the type arguments of the lists and generate a new
type that is narrower than the original.

```
from typing import Union, List

a: Union[List[List[int]], List[int]]

# before
if isinstance(a[0], list):
    reveal_type(a) # N: Revealed type is 'Union[builtins.list[builtins.list[builtins.int]], builtins.list[builtins.int]]'
else:
    reveal_type(a) # N: Revealed type is 'Union[builtins.list[builtins.list[builtins.int]], builtins.list[builtins.int]]'

# after
if isinstance(a[0], list):
    reveal_type(a) # N: Revealed type is 'builtins.list[builtins.list[builtins.int]]'
else:
    reveal_type(a) # N: Revealed type is 'builtins.list[builtins.int]'
```

fixes: python#9362
t = if_map[expr]
for x in arg_type.items:
x = get_proper_type(x)
if not isinstance(x, Instance) or not x.args:
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't sure how to restrict this to only list or sequence like types, so ended up doing this which is probably incorrect.

mypy/checker.py Outdated
x = get_proper_type(x)
if not isinstance(x, Instance) or not x.args:
return if_map, else_map
if is_overlapping_types(x.args[0], t) is not covers_at_runtime(x.args[0], t, False):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

wasn't sure about this, essentially needed this special case to get object to work as expected in the test cases

@sbdchd sbdchd closed this Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support narrowing Union of List types via index and isinstance
1 participant