Skip to content

Commit f84384e

Browse files
committed
wip
1 parent 92adb44 commit f84384e

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

mypy/checker.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7984,20 +7984,22 @@ def get_type_range_of_type(self, typ: Type) -> TypeRange | None:
79847984
# except for Type[None], because "'NoneType' is not an acceptable base type"
79857985
is_upper_bound = False
79867986
return TypeRange(typ.item, is_upper_bound=is_upper_bound)
7987+
if isinstance(typ, AnyType):
7988+
return TypeRange(typ, is_upper_bound=False)
79877989
if isinstance(typ, Instance) and typ.type.fullname == "builtins.type":
79887990
object_type = Instance(typ.type.mro[-1], [])
79897991
return TypeRange(object_type, is_upper_bound=True)
79907992
if isinstance(typ, Instance) and typ.type.fullname == "types.UnionType" and typ.args:
79917993
return TypeRange(UnionType(typ.args), is_upper_bound=False)
7992-
if isinstance(typ, AnyType):
7993-
return TypeRange(typ, is_upper_bound=False)
7994-
if not is_subtype(self.named_type("builtins.type"), typ) and not (
7995-
isinstance(typ, Instance) and typ.type.fullname == "typing._SpecialForm"
7996-
):
7994+
if isinstance(typ, Instance) and typ.type.fullname == "typing._SpecialForm":
7995+
# This is probably an alias to a Union object. We don't have the args here so we can't
7996+
# conclude anything
7997+
return None
7998+
if not is_subtype(self.named_type("builtins.type"), typ):
79977999
# We saw something, but it couldn't possibly be valid
79988000
return TypeRange(UninhabitedType(), is_upper_bound=False)
79998001

8000-
# Here we e.g. saw a variable with unknown value
8002+
# This is e.g. a variable of type object, so we can't conclude anything
80018003
return None
80028004

80038005
def is_literal_enum(self, n: Expression) -> bool:

test-data/unit/check-isinstance.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2932,13 +2932,23 @@ if isinstance(a, B):
29322932
[builtins fixtures/isinstance.pyi]
29332933

29342934
[case testIsInstanceTypeAny]
2935+
# flags: --strict-equality --warn-unreachable
29352936
from typing import Any
29362937

29372938
def foo(x: object, t: type[Any]):
29382939
if isinstance(x, t):
29392940
reveal_type(x) # N: Revealed type is "Any"
29402941
[builtins fixtures/isinstance.pyi]
29412942

2943+
[case testIsInstanceObject]
2944+
# flags: --strict-equality --warn-unreachable
2945+
from typing import Any
2946+
2947+
def foo(x: object, t: object):
2948+
if isinstance(x, t): # E: Argument 2 to "isinstance" has incompatible type "object"; expected "type[object] | tuple[type[object], ...]"
2949+
reveal_type(x) # N: Revealed type is "builtins.object"
2950+
[builtins fixtures/isinstance.pyi]
2951+
29422952
[case testIsInstanceUnionTuple]
29432953
# flags: --strict-equality --warn-unreachable
29442954
from typing import Any

0 commit comments

Comments
 (0)