From a48750c933390d4da0148b67b2b4e4d31113431d Mon Sep 17 00:00:00 2001 From: pei711 Date: Wed, 18 Nov 2026 01:04:20 +0000 Subject: [PATCH] Fix callback protocol compatibility with Any varargs --- mypy/subtypes.py | 18 ++++++++++-------- test-data/unit/check-protocols.test | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) diff --git a/mypy/subtypes.py b/mypy/subtypes.py index ca21e67f3a6e5..80009560dbbf5 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -1883,12 +1883,11 @@ def are_trivial_parameters(param: Parameters | NormalizedCallableType) -> bool: ) -def is_trivial_suffix(param: Parameters | NormalizedCallableType) -> bool: +def has_trivial_varargs(param: Parameters | NormalizedCallableType) -> bool: param_star = param.var_arg() param_star2 = param.kw_arg() return ( - param.arg_kinds[-2:] == [ARG_STAR, ARG_STAR2] - and param_star is not None + param_star is not None and isinstance(get_proper_type(param_star.typ), AnyType) and param_star2 is not None and isinstance(get_proper_type(param_star2.typ), AnyType) @@ -1917,7 +1916,10 @@ def are_parameters_compatible( # Treat "def _(*a: Any, **kw: Any) -> X" similarly to "Callable[..., X]" if are_trivial_parameters(right) and not is_proper_subtype: return True - trivial_suffix = is_trivial_suffix(right) and not is_proper_subtype + # Any-typed *args and **kwargs behave like an ellipsis even when explicit + # keyword-only arguments appear between them. Those arguments are still + # checked below. + trivial_varargs = has_trivial_varargs(right) and not is_proper_subtype trivial_vararg_suffix = False if ( @@ -1961,7 +1963,7 @@ def _incompatible(left_arg: FormalArgument | None, right_arg: FormalArgument | N if right_arg is None: return False if left_arg is None: - return not allow_partial_overlap and not trivial_suffix + return not allow_partial_overlap and not trivial_varargs return not is_compat(right_arg.typ, left_arg.typ) if ( @@ -1990,9 +1992,9 @@ def _incompatible(left_arg: FormalArgument | None, right_arg: FormalArgument | N ): return False - if trivial_suffix: - # For trivial right suffix we *only* check that every non-star right argument - # has a valid match on the left. + if trivial_varargs: + # For trivial right varargs we *only* check that every non-star right + # argument has a valid match on the left. return True # Phase 1c: Check var args. Right has an infinite series of optional positional diff --git a/test-data/unit/check-protocols.test b/test-data/unit/check-protocols.test index 4c0fdd55b1224..58a0b34f59a0c 100644 --- a/test-data/unit/check-protocols.test +++ b/test-data/unit/check-protocols.test @@ -4589,6 +4589,21 @@ t = Test(Mock()) reveal_type(t) # N: Revealed type is "__main__.Test[Any]" [builtins fixtures/dict.pyi] +[case testProtocolCallableWithAnyVarargsAndExplicitKeyword] +from typing import Any, Protocol + +class Registerable(Protocol): + def __call__(self, *args: Any, formatter: int, **kwargs: Any) -> None: ... + +def cb(*, formatter: int, extra: int = 0) -> None: ... + +def missing_formatter(*, extra: int = 0) -> None: ... + +x: Registerable = cb +y: Registerable = missing_formatter # E: Incompatible types in assignment (expression has type "def missing_formatter(*, extra: int = ...) -> None", variable has type "Registerable") \ + # N: "Registerable.__call__" has type "def __call__(self, *args: Any, formatter: int, **kwargs: Any) -> None" +[builtins fixtures/dict.pyi] + [case testProtocolClassObjectDescriptor] from typing import Any, Protocol, overload