Skip to content

Fix callback protocol assignability when trivial *args/**kwargs pair has retained parameters - #22461

Open
lau0708 wants to merge 1 commit into
python:masterfrom
lau0708:fix/trivial-star-params-with-retained-params
Open

lau0708 wants to merge 1 commit into
python:masterfrom
lau0708:fix/trivial-star-params-with-retained-params

Conversation

@lau0708

@lau0708 lau0708 commented Nov 14, 2026

Copy link
Copy Markdown

Fixes #22460

What changed

Per the typing spec:

If the input signature in a function definition includes both a *args and **kwargs parameter and both are typed as Any (explicitly or implicitly because it has no annotation), a type checker should treat this as the equivalent of .... Any other parameters in the signature are unaffected and are retained as part of the signature.

mypy already treats a signature that consists exactly of (*args: Any, **kwargs: Any) as Callable[..., X] (are_trivial_parameters), but when a retained parameter is declared between them (e.g. def __call__(self, *args: Any, formatter: int, **kwargs: Any) -> None), the fast path does not apply and the star parameters are then checked strictly, rejecting every callable that does not itself declare *args/**kwargs.

This PR adds the trivial_star_params case to are_parameters_compatible:

  • The star parameters impose no constraints on the left callable: Phase 1a (star-vs-star), Phase 1c (right var-args series) and Phase 1d (right kw-args series) are skipped.
  • Retained (non-star) parameters are still checked (Phase 1b), and required parameters of the left callable must still be passable by the right signature (Phase 2), matching the spec's own override example, where a required keyword-only parameter m: str that the base signature never passes is fine.

Out of scope: signatures where only one of the two star parameters is Any are unaffected (still checked strictly), and proper-subtype checks are unchanged (mirroring the existing trivial-parameter fast paths).

Reproducer (from the issue, now passing)

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:
    pass

r: Registerable = cb  # was: error: Incompatible types in assignment

Before this PR:

error: Incompatible types in assignment (expression has type "Callable[[NamedArg(int, 'formatter'), DefaultNamedArg(int, 'extra')], None]", variable has type "Registerable")  [assignment]

Tests

Three data-driven cases added to check-protocols.test:

  • testTrivialStarParamsInCallbackProtocol — accepted callables (the issue's case, plus positional/**kwargs variants).
  • testTrivialStarParamsRetainedParamsChecked — retained parameters are still enforced (missing param, wrong param type are still rejected).
  • testNonTrivialStarParamsStillChecked — no regression when a star parameter is not Any.

Validation

  • pytest mypy/test/testcheck.py: 8364 passed, 16 skipped, 7 xfailed (base: 8361 passed, 16 skipped, 7 xfailed — no failures either side).
  • mypy --config-file mypy_self_check.ini mypy/subtypes.py: clean.

AI assistance

This PR was prepared with the help of an AI coding agent. The spec interpretation, the fix, and the red/green validation above were verified locally against mypy master (0952029).

…has retained parameters

Per the typing spec, an input signature with both *args and **kwargs
typed as Any is equivalent to '...' in Callable, even when other
parameters are declared between them. The star parameters are gradual
and impose no constraints on the assignable callable, while retained
parameters are still checked.

Fixes python#22460
@github-actions

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

prefect (https://github.com/PrefectHQ/prefect)
+ src/prefect/flows.py:1824: error: Overloaded function signatures 4 and 5 overlap with incompatible return types  [overload-overlap]
+ src/prefect/flows.py:1824: note: Flipping the order of overloads will fix this error
+ src/prefect/flows.py:2477: error: Overloaded function signatures 4 and 5 overlap with incompatible return types  [overload-overlap]
+ src/prefect/flows.py:2477: note: Flipping the order of overloads will fix this error

pandas-stubs (https://github.com/pandas-dev/pandas-stubs)
+ pandas-stubs/core/series.pyi:1345: error: Overloaded function signatures 1 and 3 overlap with incompatible return types  [overload-overlap]
+ pandas-stubs/core/series.pyi:1353: error: Overloaded function signatures 2 and 3 overlap with incompatible return types  [overload-overlap]
+ pandas-stubs/core/series.pyi:1370: error: Overloaded function signatures 1 and 2 overlap with incompatible return types  [overload-overlap]
+ pandas-stubs/core/groupby/generic.pyi:243: error: Overloaded function signatures 1 and 2 overlap with incompatible return types  [overload-overlap]

pytensor (https://github.com/pymc-devs/pytensor)
- pytensor/tensor/random/basic.py:632: error: Signature of "__call__" incompatible with supertype "pytensor.tensor.random.op.RandomVariable"  [override]
- pytensor/tensor/random/basic.py:632: note:      Superclass:
- pytensor/tensor/random/basic.py:632: note:          def __call__(self, *args: Any, size: Any = ..., name: Any = ..., rng: Any = ..., dtype: Any = ..., return_next_rng: bool = ..., **kwargs: Any) -> Any
- pytensor/tensor/random/basic.py:632: note:      Subclass:
- pytensor/tensor/random/basic.py:632: note:          def __call__(self, loc: ndarray[tuple[Any, ...], dtype[Any]] | float, scale: ndarray[tuple[Any, ...], dtype[Any]] | float = ..., size: list[int] | int | None = ..., **kwargs: Any) -> RandomVariable

poetry (https://github.com/python-poetry/poetry)
+ src/poetry/utils/env/site_packages.py:188: error: Overloaded function signatures 1 and 2 overlap with incompatible return types  [overload-overlap]

scipy-stubs (https://github.com/scipy/scipy-stubs)
+ scipy-stubs/integrate/_tanhsinh.pyi:79: error: Overloaded function signatures 1 and 5 overlap with incompatible return types  [overload-overlap]
+ scipy-stubs/integrate/_tanhsinh.pyi:95: error: Overloaded function signatures 2 and 6 overlap with incompatible return types  [overload-overlap]
+ scipy-stubs/integrate/_tanhsinh.pyi:95: error: Overloaded function signatures 2 and 7 overlap with incompatible return types  [overload-overlap]
+ scipy-stubs/integrate/_tanhsinh.pyi:111: error: Overloaded function signatures 3 and 6 overlap with incompatible return types  [overload-overlap]
+ scipy-stubs/integrate/_tanhsinh.pyi:111: error: Overloaded function signatures 3 and 7 overlap with incompatible return types  [overload-overlap]
+ scipy-stubs/integrate/_tanhsinh.pyi:127: error: Overloaded function signatures 4 and 5 overlap with incompatible return types  [overload-overlap]
+ scipy-stubs/integrate/_tanhsinh.pyi:127: note: Flipping the order of overloads will fix this error
+ scipy-stubs/integrate/_tanhsinh.pyi:127: error: Overloaded function signatures 4 and 6 overlap with incompatible return types  [overload-overlap]
+ scipy-stubs/integrate/_tanhsinh.pyi:127: error: Overloaded function signatures 4 and 7 overlap with incompatible return types  [overload-overlap]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Callback protocol with keyword-only parameters between *args: Any and **kwargs: Any rejects all functions

1 participant