Skip to content

Commit 5f26e71

Browse files
committed
Micro-optimize is_overlapping_types
1 parent bf70dab commit 5f26e71

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

mypy/meet.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -336,20 +336,6 @@ def is_overlapping_types(
336336

337337
left, right = get_proper_types((left, right))
338338

339-
def _is_overlapping_types(left: Type, right: Type) -> bool:
340-
"""Encode the kind of overlapping check to perform.
341-
342-
This function mostly exists, so we don't have to repeat keyword arguments everywhere.
343-
"""
344-
return is_overlapping_types(
345-
left,
346-
right,
347-
ignore_promotions=ignore_promotions,
348-
prohibit_none_typevar_overlap=prohibit_none_typevar_overlap,
349-
overlap_for_overloads=overlap_for_overloads,
350-
seen_types=seen_types.copy(),
351-
)
352-
353339
# We should never encounter this type.
354340
if isinstance(left, PartialType) or isinstance(right, PartialType):
355341
assert False, "Unexpectedly encountered partial type"
@@ -399,14 +385,16 @@ def _is_overlapping_types(left: Type, right: Type) -> bool:
399385
if is_none_object_overlap(left, right) or is_none_object_overlap(right, left):
400386
return False
401387

402-
def _is_subtype(left: Type, right: Type) -> bool:
403-
if overlap_for_overloads:
404-
return is_proper_subtype(left, right, ignore_promotions=ignore_promotions)
405-
else:
406-
return is_subtype(left, right, ignore_promotions=ignore_promotions)
407-
408-
if _is_subtype(left, right) or _is_subtype(right, left):
409-
return True
388+
if overlap_for_overloads:
389+
if is_proper_subtype(
390+
left, right, ignore_promotions=ignore_promotions
391+
) or is_proper_subtype(right, left, ignore_promotions=ignore_promotions):
392+
return True
393+
else:
394+
if is_subtype(left, right, ignore_promotions=ignore_promotions) or is_subtype(
395+
right, left, ignore_promotions=ignore_promotions
396+
):
397+
return True
410398

411399
# See the docstring for 'get_possible_variants' for more info on what the
412400
# following lines are doing.
@@ -428,6 +416,20 @@ def _is_subtype(left: Type, right: Type) -> bool:
428416
if is_none_typevarlike_overlap(left, right) or is_none_typevarlike_overlap(right, left):
429417
return False
430418

419+
def _is_overlapping_types(left: Type, right: Type) -> bool:
420+
"""Encode the kind of overlapping check to perform.
421+
422+
This function mostly exists, so we don't have to repeat keyword arguments everywhere.
423+
"""
424+
return is_overlapping_types(
425+
left,
426+
right,
427+
ignore_promotions=ignore_promotions,
428+
prohibit_none_typevar_overlap=prohibit_none_typevar_overlap,
429+
overlap_for_overloads=overlap_for_overloads,
430+
seen_types=seen_types.copy(),
431+
)
432+
431433
if (
432434
len(left_possible) > 1
433435
or len(right_possible) > 1
@@ -564,8 +566,16 @@ def _type_object_overlap(left: Type, right: Type) -> bool:
564566
if isinstance(left, Instance) and isinstance(right, Instance):
565567
# First we need to handle promotions and structural compatibility for instances
566568
# that came as fallbacks, so simply call is_subtype() to avoid code duplication.
567-
if _is_subtype(left, right) or _is_subtype(right, left):
568-
return True
569+
if overlap_for_overloads:
570+
if is_proper_subtype(
571+
left, right, ignore_promotions=ignore_promotions
572+
) or is_proper_subtype(right, left, ignore_promotions=ignore_promotions):
573+
return True
574+
else:
575+
if is_subtype(left, right, ignore_promotions=ignore_promotions) or is_subtype(
576+
right, left, ignore_promotions=ignore_promotions
577+
):
578+
return True
569579

570580
if right.type.fullname == "builtins.int" and left.type.fullname in MYPYC_NATIVE_INT_NAMES:
571581
return True

0 commit comments

Comments
 (0)