Skip to content
Open
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
3 changes: 3 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -8397,6 +8397,9 @@ def get_type_range_of_type(self, typ: Type) -> TypeRange | None:
is_upper_bound = False
if isinstance(typ.item, Instance) and typ.item.type.is_final:
is_upper_bound = False
if isinstance(typ.item, TypeVarType) and typ.item.id.is_self():
# except for Self types, because isinstance checks for exact type
is_upper_bound = False
return TypeRange(typ.item, is_upper_bound=is_upper_bound)
if isinstance(typ, AnyType):
return TypeRange(typ, is_upper_bound=False)
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-narrowing.test
Original file line number Diff line number Diff line change
Expand Up @@ -3052,6 +3052,21 @@ def check_d(arg: D[T]) -> None:
[builtins fixtures/tuple.pyi]


[case testIsinstanceNarrowingWithSelfTypeInClassmethod]
from typing_extensions import Self

class Foo:
@classmethod
def foo_1(cls, x: Self | float) -> None:
assert isinstance(x, cls)
reveal_type(x) # N: Revealed type is "Self`0"

@classmethod
def foo_2(cls, x: Self | float) -> None:
assert not isinstance(x, cls)
reveal_type(x) # N: Revealed type is "builtins.float"
[builtins fixtures/isinstancelist.pyi]

[case testNarrowingUnionMixins]
# flags: --strict-equality --warn-unreachable
class Base: ...
Expand Down
Loading