Skip to content

Commit a64c880

Browse files
committed
Avoid nested function object creation when not needed
1 parent 6376499 commit a64c880

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

mypy/meet.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -485,27 +485,28 @@ def _is_overlapping_types(left: Type, right: Type) -> bool:
485485
if isinstance(left, TypeType) and isinstance(right, TypeType):
486486
return _is_overlapping_types(left.item, right.item)
487487

488-
def _type_object_overlap(left: Type, right: Type) -> bool:
489-
"""Special cases for type object types overlaps."""
490-
# TODO: these checks are a bit in gray area, adjust if they cause problems.
491-
left, right = get_proper_types((left, right))
492-
# 1. Type[C] vs Callable[..., C] overlap even if the latter is not class object.
493-
if isinstance(left, TypeType) and isinstance(right, CallableType):
494-
return _is_overlapping_types(left.item, right.ret_type)
495-
# 2. Type[C] vs Meta, where Meta is a metaclass for C.
496-
if isinstance(left, TypeType) and isinstance(right, Instance):
497-
if isinstance(left.item, Instance):
498-
left_meta = left.item.type.metaclass_type
499-
if left_meta is not None:
500-
return _is_overlapping_types(left_meta, right)
501-
# builtins.type (default metaclass) overlaps with all metaclasses
502-
return right.type.has_base("builtins.type")
503-
elif isinstance(left.item, AnyType):
504-
return right.type.has_base("builtins.type")
505-
# 3. Callable[..., C] vs Meta is considered below, when we switch to fallbacks.
506-
return False
507-
508488
if isinstance(left, TypeType) or isinstance(right, TypeType):
489+
490+
def _type_object_overlap(left: Type, right: Type) -> bool:
491+
"""Special cases for type object types overlaps."""
492+
# TODO: these checks are a bit in gray area, adjust if they cause problems.
493+
left, right = get_proper_types((left, right))
494+
# 1. Type[C] vs Callable[..., C] overlap even if the latter is not class object.
495+
if isinstance(left, TypeType) and isinstance(right, CallableType):
496+
return _is_overlapping_types(left.item, right.ret_type)
497+
# 2. Type[C] vs Meta, where Meta is a metaclass for C.
498+
if isinstance(left, TypeType) and isinstance(right, Instance):
499+
if isinstance(left.item, Instance):
500+
left_meta = left.item.type.metaclass_type
501+
if left_meta is not None:
502+
return _is_overlapping_types(left_meta, right)
503+
# builtins.type (default metaclass) overlaps with all metaclasses
504+
return right.type.has_base("builtins.type")
505+
elif isinstance(left.item, AnyType):
506+
return right.type.has_base("builtins.type")
507+
# 3. Callable[..., C] vs Meta is considered below, when we switch to fallbacks.
508+
return False
509+
509510
return _type_object_overlap(left, right) or _type_object_overlap(right, left)
510511

511512
if isinstance(left, Parameters) and isinstance(right, Parameters):

0 commit comments

Comments
 (0)