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

Retain None (unreachable) when typemap is None with type(x) is Foo check #18486

Merged
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
2 changes: 2 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5914,6 +5914,8 @@ def is_type_call(expr: CallExpr) -> bool:

def combine_maps(list_maps: list[TypeMap]) -> TypeMap:
"""Combine all typemaps in list_maps into one typemap"""
if all(m is None for m in list_maps):
return None
result_map = {}
for d in list_maps:
if d is not None:
Comment on lines +5917 to 5921
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
if all(m is None for m in list_maps):
return None
result_map = {}
for d in list_maps:
if d is not None:
result_map = None
for d in list_maps:
if d is not None:
if result_map is None:
result_map = {}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'd keep my version if you don't mind: this part isn't hot (only reachable when we actually have some type(x) <op> Something checks), and I find it more explicit/slightly easier to follow. This shouldn't have any measurable performance impact.

Expand Down
27 changes: 27 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -2661,6 +2661,33 @@ y: Any
if type(y) is int:
reveal_type(y) # N: Revealed type is "builtins.int"

[case testTypeEqualsCheckUsingIsNonOverlapping]
# flags: --warn-unreachable
from typing import Union

y: str
if type(y) is int: # E: Subclass of "str" and "int" cannot exist: would have incompatible method signatures
y # E: Statement is unreachable
else:
reveal_type(y) # N: Revealed type is "builtins.str"
[builtins fixtures/isinstance.pyi]

[case testTypeEqualsCheckUsingIsNonOverlappingChild-xfail]
# flags: --warn-unreachable
from typing import Union

class A: ...
class B: ...
class C(A): ...
x: Union[B, C]
# C instance cannot be exactly its parent A, we need reversed subtyping relationship
# here (type(parent) is Child).
if type(x) is A:
reveal_type(x) # E: Statement is unreachable
else:
reveal_type(x) # N: Revealed type is "Union[__main__.B, __main__.C]"
[builtins fixtures/isinstance.pyi]

[case testTypeEqualsNarrowingUnionWithElse]
from typing import Union

Expand Down
13 changes: 13 additions & 0 deletions test-data/unit/check-typevar-values.test
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,16 @@ def foo3(x: NT) -> None:
def foo4(x: NT) -> None:
p, q = 1, 2.0 # type: (int, float)
[builtins fixtures/tuple.pyi]

[case testTypeVarValuesNarrowing]
from typing import TypeVar

W = TypeVar("W", int, str)

def fn(w: W) -> W:
if type(w) is str:
reveal_type(w) # N: Revealed type is "builtins.str"
elif type(w) is int:
reveal_type(w) # N: Revealed type is "builtins.int"
return w
[builtins fixtures/isinstance.pyi]
Loading