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
Closed
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 25 additions & 2 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
)
from mypy import message_registry
from mypy.subtypes import (
is_subtype, is_equivalent, is_proper_subtype, is_more_precise,
covers_at_runtime, is_subtype, is_equivalent, is_proper_subtype, is_more_precise,
restrict_subtype_away, is_subtype_ignoring_tvars, is_callable_compatible,
unify_generic_callable, find_member
)
Expand Down Expand Up @@ -3986,11 +3986,34 @@ def find_isinstance_check_helper(self, node: Expression) -> Tuple[TypeMap, TypeM
if len(node.args) != 2: # the error will be reported elsewhere
return {}, {}
if literal(expr) == LITERAL_TYPE:
return self.conditional_type_map_with_intersection(
if_map_base, else_map_base = self.conditional_type_map_with_intersection(
expr,
type_map[expr],
get_isinstance_type(node.args[1], type_map),
)
first_node_arg = node.args[0]
if isinstance(first_node_arg, IndexExpr):
arg_type = get_proper_type(type_map[first_node_arg.base])
if (isinstance(arg_type, UnionType) and
if_map_base is not None and else_map_base is not None):
if_branch_union = []
else_branch_union = []
t = if_map_base[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.

return if_map_base, else_map_base
if (is_overlapping_types(x.args[0], t) is not
covers_at_runtime(x.args[0], t, False)):
if_branch_union.append(x)
else_branch_union.append(x)
elif is_overlapping_types(x.args[0], t):
if_branch_union.append(x)
else:
else_branch_union.append(x)
if_map_base[first_node_arg.base] = UnionType(if_branch_union)
else_map_base[first_node_arg.base] = UnionType(else_branch_union)
return if_map_base, else_map_base
elif refers_to_fullname(node.callee, 'builtins.issubclass'):
if len(node.args) != 2: # the error will be reported elsewhere
return {}, {}
Expand Down
76 changes: 76 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -1026,3 +1026,79 @@ else:
reveal_type(str_or_bool_literal) # N: Revealed type is 'Union[Literal[False], Literal[True]]'

[builtins fixtures/primitives.pyi]

[case testNarrowingUnionListTypes]
from typing import Union, List, Any, Sequence

a: Union[List[List[int]], List[int]]
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]'

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

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

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

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

e: Union[List[object], List[str]]
if isinstance(e[0], str):
reveal_type(e) # N: Revealed type is 'Union[builtins.list[builtins.object], builtins.list[builtins.str]]'
else:
reveal_type(e) # N: Revealed type is 'builtins.list[builtins.object]'

f: Union[List[Any], List[str]]
if isinstance(f[0], str):
reveal_type(f) # N: Revealed type is 'Union[builtins.list[Any], builtins.list[builtins.str]]'
else:
reveal_type(f) # N: Revealed type is 'Union[builtins.list[Any], builtins.list[builtins.str]]'

h: Union[List[int], Any]
if isinstance(h[0], int):
reveal_type(h[0]) # N: Revealed type is 'builtins.int*'
reveal_type(h) # N: Revealed type is 'Union[builtins.list[builtins.int], Any]'
else:
reveal_type(h[0]) # N: Revealed type is 'Any'
reveal_type(h) # N: Revealed type is 'Union[builtins.list[builtins.int], Any]'

i: Union[List[int], Sequence[str]]
if isinstance(i[0], int):
reveal_type(i) # N: Revealed type is 'builtins.list[builtins.int]'
else:
reveal_type(i) # N: Revealed type is 'typing.Sequence[builtins.str]'

q: Union[List[int], List[str], List[List[object]]]
if isinstance(q[0], (str, int)):
reveal_type(q[0]) # N: Revealed type is 'Union[builtins.int*, builtins.str*]'
reveal_type(q) # N: Revealed type is 'Union[builtins.list[builtins.int], builtins.list[builtins.str]]'
else:
reveal_type(q[0]) # N: Revealed type is 'builtins.list*[builtins.object]'
reveal_type(q) # N: Revealed type is 'builtins.list[builtins.list[builtins.object]]'

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

[builtins fixtures/list.pyi]
[builtins fixtures/isinstancelist.pyi]
[typing fixtures/typing-full.pyi]