Skip to content

Commit 92f9429

Browse files
authored
Treat Type[Any] as builtins.type with --strict-equality (#7824)
Fixes #7823 This makes `Type[object]`, `Type[Any]`, and `type` behave the same way w.r.t. type overlaps.
1 parent 7ffbc53 commit 92f9429

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

mypy/meet.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,15 @@ def _type_object_overlap(left: ProperType, right: ProperType) -> bool:
249249
if isinstance(left, TypeType) and isinstance(right, CallableType) and right.is_type_obj():
250250
return _is_overlapping_types(left.item, right.ret_type)
251251
# 2. Type[C] vs Meta, where Meta is a metaclass for C.
252-
if (isinstance(left, TypeType) and isinstance(left.item, Instance) and
253-
isinstance(right, Instance)):
254-
left_meta = left.item.type.metaclass_type
255-
if left_meta is not None:
256-
return _is_overlapping_types(left_meta, right)
257-
# builtins.type (default metaclass) overlaps with all metaclasses
258-
return right.type.has_base('builtins.type')
252+
if isinstance(left, TypeType) and isinstance(right, Instance):
253+
if isinstance(left.item, Instance):
254+
left_meta = left.item.type.metaclass_type
255+
if left_meta is not None:
256+
return _is_overlapping_types(left_meta, right)
257+
# builtins.type (default metaclass) overlaps with all metaclasses
258+
return right.type.has_base('builtins.type')
259+
elif isinstance(left.item, AnyType):
260+
return right.type.has_base('builtins.type')
259261
# 3. Callable[..., C] vs Meta is considered below, when we switch to fallbacks.
260262
return False
261263

test-data/unit/check-expressions.test

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2579,20 +2579,35 @@ Bad in subclasses # E: Non-overlapping container check (element type: "Type[Bad
25792579

25802580
[case testStrictEqualityMetaclass]
25812581
# flags: --strict-equality
2582-
from typing import List, Type
2582+
from typing import List, Type, Any
25832583

25842584
class Meta(type): ...
25852585
class OtherMeta(type): ...
25862586

25872587
class A(metaclass=Meta): ...
2588+
class B(metaclass=Meta): ...
25882589
class C(metaclass=OtherMeta): ...
25892590

25902591
o: Type[object]
2592+
a: Type[Any]
2593+
aa: type
25912594
exp: List[Meta]
25922595

25932596
A in exp
2597+
B in exp
25942598
C in exp # E: Non-overlapping container check (element type: "Type[C]", container item type: "Meta")
2599+
25952600
o in exp
2601+
a in exp
2602+
aa in exp
2603+
2604+
a in [A, B]
2605+
aa in [A, B]
2606+
2607+
class AA: ...
2608+
class BB: ...
2609+
a in [AA, BB]
2610+
aa in [AA, BB]
25962611
[builtins fixtures/list.pyi]
25972612
[typing fixtures/typing-full.pyi]
25982613

0 commit comments

Comments
 (0)