Skip to content

Commit d27bff6

Browse files
authored
Merge subtype visitors (#13303)
Fixes #3297 This removes a significant chunk of code duplication. This is not a pure refactor, there were some cases when one of the visitors (mostly non-proper one) was more correct and/or complete. In few corner cases, where it was hard to decide, I merged behavior with `if` checks.
1 parent d2063d2 commit d27bff6

File tree

6 files changed

+271
-494
lines changed

6 files changed

+271
-494
lines changed

mypy/meet.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,8 @@ def _type_object_overlap(left: Type, right: Type) -> bool:
361361
"""Special cases for type object types overlaps."""
362362
# TODO: these checks are a bit in gray area, adjust if they cause problems.
363363
left, right = get_proper_types((left, right))
364-
# 1. Type[C] vs Callable[..., C], where the latter is class object.
365-
if isinstance(left, TypeType) and isinstance(right, CallableType) and right.is_type_obj():
364+
# 1. Type[C] vs Callable[..., C] overlap even if the latter is not class object.
365+
if isinstance(left, TypeType) and isinstance(right, CallableType):
366366
return _is_overlapping_types(left.item, right.ret_type)
367367
# 2. Type[C] vs Meta, where Meta is a metaclass for C.
368368
if isinstance(left, TypeType) and isinstance(right, Instance):
@@ -381,13 +381,18 @@ def _type_object_overlap(left: Type, right: Type) -> bool:
381381
return _type_object_overlap(left, right) or _type_object_overlap(right, left)
382382

383383
if isinstance(left, CallableType) and isinstance(right, CallableType):
384-
return is_callable_compatible(
385-
left,
386-
right,
387-
is_compat=_is_overlapping_types,
388-
ignore_pos_arg_names=True,
389-
allow_partial_overlap=True,
390-
)
384+
385+
def _callable_overlap(left: CallableType, right: CallableType) -> bool:
386+
return is_callable_compatible(
387+
left,
388+
right,
389+
is_compat=_is_overlapping_types,
390+
ignore_pos_arg_names=True,
391+
allow_partial_overlap=True,
392+
)
393+
394+
# Compare both directions to handle type objects.
395+
return _callable_overlap(left, right) or _callable_overlap(right, left)
391396
elif isinstance(left, CallableType):
392397
left = left.fallback
393398
elif isinstance(right, CallableType):

0 commit comments

Comments
 (0)