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
8 changes: 8 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6901,6 +6901,14 @@ def narrow_type_by_identity_equality(
expr_in_type_expr = type_expr.expr
else:
continue

p_expr_type = get_proper_type(operand_types[i])
if isinstance(p_expr_type, TypeType) and isinstance(p_expr_type.item, TypeVarType):
# This mirrors logic in comparison_type_narrowing_helper
# In theory, this is like `i not in narrowable_indices`, except that
# narrowable_indices filters all type(x) narrowing as it's a call
continue

for j in expr_indices:
if i == j:
continue
Expand Down
24 changes: 24 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -3395,6 +3395,30 @@ def f(x: str, y: Any, z: object):
reveal_type(z) # N: Revealed type is "builtins.str"
[builtins fixtures/primitives.pyi]

[case testTypeEqualsCheckWideningSelf]
# flags: --strict-equality --warn-unreachable
from typing import Any
from typing_extensions import Self

class A:
def f(self: Self, y: Any, z: object):
if type(self) is type(y):
reveal_type(self) # N: Revealed type is "Self`0"
reveal_type(y) # N: Revealed type is "Self`0"

if type(self) is type(z):
reveal_type(self) # N: Revealed type is "Self`0"
reveal_type(z) # N: Revealed type is "Self`0"

if type(self) == type(y):
reveal_type(self) # N: Revealed type is "Self`0"
reveal_type(y) # N: Revealed type is "Self`0"

if type(self) == type(z):
reveal_type(self) # N: Revealed type is "Self`0"
reveal_type(z) # N: Revealed type is "Self`0"
[builtins fixtures/primitives.pyi]

[case testTypeEqualsCheckUsingIs]
# flags: --strict-equality --warn-unreachable
from typing import Any
Expand Down
Loading