Skip to content

Commit 6f0c0b5

Browse files
authored
Fix subtyping for def f(*a: Unpack[tuple[T, ...]])
1 parent 1995155 commit 6f0c0b5

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

mypy/subtypes.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1603,6 +1603,17 @@ def are_parameters_compatible(
16031603
return True
16041604
trivial_suffix = is_trivial_suffix(right) and not is_proper_subtype
16051605

1606+
# def _(*a: Unpack[tuple[object, ...]]) allows any number of arguments, not just infinite.
1607+
if right_star and isinstance(right_star.typ, UnpackType):
1608+
right_star_inner_type = get_proper_type(right_star.typ.type)
1609+
trivial_varargs = (
1610+
isinstance(right_star_inner_type, Instance)
1611+
and right_star_inner_type.type.fullname == "builtins.tuple"
1612+
and len(right_star_inner_type.args) == 1
1613+
)
1614+
else:
1615+
trivial_varargs = False
1616+
16061617
if (
16071618
right.arg_kinds == [ARG_STAR]
16081619
and isinstance(get_proper_type(right.arg_types[0]), AnyType)
@@ -1644,7 +1655,7 @@ def _incompatible(left_arg: FormalArgument | None, right_arg: FormalArgument | N
16441655
if right_arg is None:
16451656
return False
16461657
if left_arg is None:
1647-
return not allow_partial_overlap and not trivial_suffix
1658+
return not allow_partial_overlap and not trivial_suffix and not trivial_varargs
16481659
return not is_compat(right_arg.typ, left_arg.typ)
16491660

16501661
if _incompatible(left_star, right_star) or _incompatible(left_star2, right_star2):
@@ -1672,8 +1683,7 @@ def _incompatible(left_arg: FormalArgument | None, right_arg: FormalArgument | N
16721683
# Phase 1c: Check var args. Right has an infinite series of optional positional
16731684
# arguments. Get all further positional args of left, and make sure
16741685
# they're more general than the corresponding member in right.
1675-
# TODO: are we handling UnpackType correctly here?
1676-
if right_star is not None and not trivial_suffix:
1686+
if right_star is not None and not trivial_suffix and not trivial_varargs:
16771687
# Synthesize an anonymous formal argument for the right
16781688
right_by_position = right.try_synthesizing_arg_from_vararg(None)
16791689
assert right_by_position is not None

0 commit comments

Comments
 (0)