Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize subtype checking by avoiding a nested function #14325

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 20 additions & 11 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,20 @@ def _is_subtype(
# ErasedType as we do for non-proper subtyping.
return True

def check_item(left: Type, right: Type, subtype_context: SubtypeContext) -> bool:
if proper_subtype:
return is_proper_subtype(left, right, subtype_context=subtype_context)
return is_subtype(left, right, subtype_context=subtype_context)

if isinstance(right, UnionType) and not isinstance(left, UnionType):
# Normally, when 'left' is not itself a union, the only way
# 'left' can be a subtype of the union 'right' is if it is a
# subtype of one of the items making up the union.
is_subtype_of_item = any(
check_item(orig_left, item, subtype_context) for item in right.items
)
if proper_subtype:
is_subtype_of_item = any(
is_proper_subtype(orig_left, item, subtype_context=subtype_context)
for item in right.items
)
else:
is_subtype_of_item = any(
is_subtype(orig_left, item, subtype_context=subtype_context)
for item in right.items
)
# Recombine rhs literal types, to make an enum type a subtype
# of a union of all enum items as literal types. Only do it if
# the previous check didn't succeed, since recombining can be
Expand All @@ -312,9 +314,16 @@ def check_item(left: Type, right: Type, subtype_context: SubtypeContext) -> bool
and (left.type.is_enum or left.type.fullname == "builtins.bool")
):
right = UnionType(mypy.typeops.try_contracting_literals_in_union(right.items))
is_subtype_of_item = any(
check_item(orig_left, item, subtype_context) for item in right.items
)
if proper_subtype:
is_subtype_of_item = any(
is_proper_subtype(orig_left, item, subtype_context=subtype_context)
for item in right.items
)
else:
is_subtype_of_item = any(
is_subtype(orig_left, item, subtype_context=subtype_context)
for item in right.items
)
# However, if 'left' is a type variable T, T might also have
# an upper bound which is itself a union. This case will be
# handled below by the SubtypeVisitor. We have to check both
Expand Down