Skip to content
Closed
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
Use safe_meet() for callable argument merging to handle UnpackType
Fixes #20093

The callable_corresponding_argument() function in typeops.py was calling
meet_types() directly, which doesn't handle UnpackType. This caused crashes
when type-checking code combining ParamSpec and TypeVarTuple with Unpack.

Changed to use safe_meet() instead, which properly handles UnpackType along
with other variadic types. The safe_meet() function already exists in join.py
and is designed specifically for handling variadic types like UnpackType.

This approach is preferred over implementing visit_unpack_type() in meet.py
because safe_meet() already contains the necessary logic for handling different
kinds of unpacks (TypeVarTuple, Tuple, etc.) with proper fallback types.

Added regression test in check-parameter-specification.test.
  • Loading branch information
jameslovespancakes committed Oct 22, 2025
commit 0ffc31a5ce7a36a1285dfb1e8c4faa0df0917245
6 changes: 5 additions & 1 deletion mypy/meet.py
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,11 @@ def visit_type_var_tuple(self, t: TypeVarTupleType) -> ProperType:
return self.default(self.s)

def visit_unpack_type(self, t: UnpackType) -> ProperType:
raise NotImplementedError
if isinstance(self.s, UnpackType):
return UnpackType(self.meet(t.type, self.s.type))
if isinstance(self.s, Instance) and self.s.type.fullname == "builtins.object":
return t
return self.default(self.s)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If safe_meet is enough, then this shouldn't be required?


def visit_parameters(self, t: Parameters) -> ProperType:
if isinstance(self.s, Parameters):
Expand Down
4 changes: 2 additions & 2 deletions mypy/typeops.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,15 @@ def callable_corresponding_argument(

# def right(a: int = ...) -> None: ...
# def left(__a: int = ..., *, a: int = ...) -> None: ...
from mypy.meet import meet_types
from mypy.join import safe_meet

if (
not (by_name.required or by_pos.required)
and by_pos.name is None
and by_name.pos is None
):
return FormalArgument(
by_name.name, by_pos.pos, meet_types(by_name.typ, by_pos.typ), False
by_name.name, by_pos.pos, safe_meet(by_name.typ, by_pos.typ), False
)
return by_name if by_name is not None else by_pos

Expand Down
20 changes: 20 additions & 0 deletions test-data/unit/check-parameter-specification.test
Original file line number Diff line number Diff line change
Expand Up @@ -2599,3 +2599,23 @@ def run3(predicate: Callable[Concatenate[int, str, _P], None], *args: _P.args, *
# E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "str" \
# E: Argument 1 has incompatible type "*tuple[Union[int, str], ...]"; expected "_P.args"
[builtins fixtures/paramspec.pyi]

[case testParamSpecWithTypeVarTupleUnpack]
# Regression test for crash with ParamSpec and TypeVarTuple Unpack in meet.py
from typing import Callable, ParamSpec, TypeVar, TypeVarTuple, Unpack

P = ParamSpec("P")
T = TypeVar("T")
Ts = TypeVarTuple("Ts")

def call(func: Callable[P, T], *args: P.args, **kwargs: P.kwargs) -> T:
return func(*args, **kwargs)

def run(func: Callable[[Unpack[Ts]], T], *args: Unpack[Ts], some_kwarg: str = "asdf") -> T:
...

def foo() -> str:
...

call(run, foo, some_kwarg="a") # E: Argument 1 to "call" has incompatible type "Callable[[Callable[[VarArg(Unpack[Ts])], T], VarArg(Unpack[Ts]), DefaultNamedArg(str, 'some_kwarg')], T]"; expected "Callable[[Callable[[], str], str], str]"
[builtins fixtures/paramspec.pyi]