Skip to content
Merged
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
25 changes: 17 additions & 8 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -7143,14 +7143,23 @@ def replay_lookup(new_parent_type: ProperType) -> Type | None:
if int_literals is not None:

def replay_lookup(new_parent_type: ProperType) -> Type | None:
if not isinstance(new_parent_type, TupleType):
return None
try:
assert int_literals is not None
member_types = [new_parent_type.items[key] for key in int_literals]
except IndexError:
return None
return make_simplified_union(member_types)
if isinstance(new_parent_type, TupleType):
try:
assert int_literals is not None
member_types = [
new_parent_type.items[key] for key in int_literals
]
except IndexError:
return None
return make_simplified_union(member_types)
if (
isinstance(new_parent_type, Instance)
and new_parent_type.type.fullname
in ("builtins.list", "builtins.tuple")
and new_parent_type.args
):
return new_parent_type.args[0]
return None

else:
return output
Expand Down
65 changes: 65 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,71 @@ def f(x: Union[int, str, List]) -> None:
reveal_type(x) # N: Revealed type is "builtins.int | builtins.str | builtins.list[Any]"
[builtins fixtures/isinstancelist.pyi]

[case testIsinstanceNarrowsSequenceParentFromElement]
# flags: --warn-unreachable
from __future__ import annotations

def f1(x: list[str] | list[int]) -> None:
if isinstance(x[0], str):
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str]"
reveal_type(x[0]) # N: Revealed type is "builtins.str"
else:
reveal_type(x) # N: Revealed type is "builtins.list[builtins.int]"
reveal_type(x[0]) # N: Revealed type is "builtins.int"

def f2(x: tuple[str, ...] | tuple[int, ...]) -> None:
if isinstance(x[0], str):
reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.str, ...]"
reveal_type(x[0]) # N: Revealed type is "builtins.str"
else:
reveal_type(x) # N: Revealed type is "builtins.tuple[builtins.int, ...]"
reveal_type(x[0]) # N: Revealed type is "builtins.int"

def f3(x: tuple[str, str] | tuple[int, int]) -> None:
if isinstance(x[0], str):
reveal_type(x) # N: Revealed type is "tuple[builtins.str, builtins.str]"
else:
reveal_type(x) # N: Revealed type is "tuple[builtins.int, builtins.int]"

class A: ...
class B: ...
class C(A, B): ...

def f4(x: list[A] | list[B]):
# Technically this is unsound in the presence of multiple inheritance,
# but we already do this kind of narrowing for tuples
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What about a situation like this:

class Super: ...
class Sub(Super): ...

def foo(x: list[Sub] | list[Super]) -> None:
    if isinstance(x[0], Sub):
        ...

Will this cause a narrowing here? I would say we should not narrow. Same question about list[str] | list[str | None] with x[0] is not None.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No it won't (from the pre-existing logic). I can add a test case for it though

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

OK, thanks!

if isinstance(x[0], A):
reveal_type(x) # N: Revealed type is "builtins.list[__main__.A]"
else:
reveal_type(x) # N: Revealed type is "builtins.list[__main__.B]"

def f5(x: tuple[A, ...] | tuple[B, ...]):
if isinstance(x[0], A):
reveal_type(x) # N: Revealed type is "builtins.tuple[__main__.A, ...]"
else:
reveal_type(x) # N: Revealed type is "builtins.tuple[__main__.B, ...]"

def f6(x: tuple[A, A] | tuple[B, B]):
if isinstance(x[0], A):
reveal_type(x) # N: Revealed type is "tuple[__main__.A, __main__.A]"
else:
reveal_type(x) # N: Revealed type is "tuple[__main__.B, __main__.B]"

class AA(A): ...

def f7(x: list[A] | list[AA]):
if isinstance(x[0], AA):
reveal_type(x) # N: Revealed type is "builtins.list[__main__.A] | builtins.list[__main__.AA]"
else:
reveal_type(x) # N: Revealed type is "builtins.list[__main__.A] | builtins.list[__main__.AA]"

def f8(x: list[str] | list[str | None]):
if x[0] is not None:
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.str | None]"
else:
reveal_type(x) # N: Revealed type is "builtins.list[builtins.str | None]"
[builtins fixtures/tuple.pyi]

[case testClassAttributeInitialization]
# flags: --warn-unreachable
class A:
Expand Down
Loading