Skip to content

Implement more cases for typevartuple subtyping. #13211

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

Merged
merged 1 commit into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions mypy/subtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,19 +319,36 @@ def check_mixed(
return False
if isinstance(unpacked_type, AnyType):
return True
assert False
if isinstance(unpacked_type, TupleType):
if len(unpacked_type.items) != len(compare_to):
return False
for t1, t2 in zip(unpacked_type.items, compare_to):
if not is_equivalent(t1, t2):
return False
return True
return False

# Case 1: Both are unpacks, in this case we check what is being
# unpacked is the same.
if left_unpacked is not None and right_unpacked is not None:
if not is_equivalent(left_unpacked, right_unpacked):
return False

# Case 2: Only one of the types is an unpack.
# Case 2: Only one of the types is an unpack. The equivalence
# case is mostly the same but we check some additional
# things when unpacking on the right.
elif left_unpacked is not None and right_unpacked is None:
if not check_mixed(left_unpacked, right_middle):
return False
elif left_unpacked is None and right_unpacked is not None:
if (
isinstance(right_unpacked, Instance)
and right_unpacked.type.fullname == "builtins.tuple"
):
return all(
is_equivalent(l, right_unpacked.args[0])
for l in left_middle
)
if not check_mixed(right_unpacked, left_middle):
return False

Expand Down
85 changes: 84 additions & 1 deletion mypy/test/testsubtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from mypy.nodes import CONTRAVARIANT, INVARIANT, COVARIANT
from mypy.subtypes import is_subtype
from mypy.test.typefixture import TypeFixture, InterfaceTypeFixture
from mypy.types import Type, Instance, UnpackType
from mypy.types import Type, Instance, UnpackType, TupleType


class SubtypingSuite(Suite):
Expand Down Expand Up @@ -217,6 +217,89 @@ def test_type_var_tuple(self) -> None:
Instance(self.fx.gvi, [self.fx.anyt]),
)

def test_type_var_tuple_with_prefix_suffix(self) -> None:
self.assert_subtype(
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss)]),
)
self.assert_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss)]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [self.fx.b, UnpackType(self.fx.ss)]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss)]),
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss)]),
)

self.assert_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a]),
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a]),
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.b]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a]),
Instance(self.fx.gvi, [UnpackType(self.fx.ss), self.fx.a, self.fx.b]),
)

self.assert_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss), self.fx.c]),
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss), self.fx.c]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.b, UnpackType(self.fx.ss), self.fx.c]),
Instance(self.fx.gvi, [self.fx.a, UnpackType(self.fx.ss), self.fx.b, self.fx.c]),
)

def test_type_var_tuple_unpacked_tuple(self) -> None:
self.assert_subtype(
Instance(self.fx.gvi, [
UnpackType(TupleType(
[self.fx.a, self.fx.b], fallback=Instance(self.fx.std_tuplei, [self.fx.o]),
))
]),
Instance(self.fx.gvi, [self.fx.a, self.fx.b]),
)
self.assert_subtype(
Instance(self.fx.gvi, [
UnpackType(TupleType(
[self.fx.a, self.fx.b], fallback=Instance(self.fx.std_tuplei, [self.fx.o]),
))
]),
Instance(self.fx.gvi, [self.fx.anyt, self.fx.anyt]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [
UnpackType(TupleType(
[self.fx.a, self.fx.b], fallback=Instance(self.fx.std_tuplei, [self.fx.o]),
))
]),
Instance(self.fx.gvi, [self.fx.a]),
)
self.assert_not_subtype(
Instance(self.fx.gvi, [
UnpackType(TupleType(
[self.fx.a, self.fx.b], fallback=Instance(self.fx.std_tuplei, [self.fx.o]),
))
]),
# Order flipped here.
Instance(self.fx.gvi, [self.fx.b, self.fx.a]),
)

def test_type_var_tuple_unpacked_variable_length_tuple(self) -> None:
self.assert_strict_subtype(
Instance(self.fx.gvi, [self.fx.a, self.fx.a]),
Instance(self.fx.gvi, [
UnpackType(Instance(self.fx.std_tuplei, [self.fx.a])),
]),
)

# IDEA: Maybe add these test cases (they are tested pretty well in type
# checker tests already):
# * more interface subtyping test cases
Expand Down