Skip to content

Fix callback protocol compatibility with Any varargs - #22522

Closed
pei711 wants to merge 1 commit into
python:masterfrom
pei711:fix-22460-callable-protocol-any-params
Closed

pei711 wants to merge 1 commit into
python:masterfrom
pei711:fix-22460-callable-protocol-any-params

Conversation

@pei711

@pei711 pei711 commented Nov 19, 2026

Copy link
Copy Markdown

Callback protocols with *args: Any, an explicit keyword-only parameter, and **kwargs: Any reject callbacks that implement the explicit parameter but omit arbitrary varargs. Treat the Any-typed vararg pair as a callable ellipsis while continuing to check explicitly declared parameters, and cover both matching and missing required keywords with a regression test.

Verified with the focused callback protocol test and the full check-protocols.test suite (204 passed, 1 skipped).

Fixes #22460

@github-actions

Copy link
Copy Markdown
Contributor

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

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

jax (https://github.com/google/jax)
- jax/_src/pallas/mosaic/random.py:160: error: Argument 1 to "_make_stateful_sampler" has incompatible type overloaded function; expected "SampleFn"  [arg-type]
- jax/_src/pallas/mosaic/random.py:160: note: "SampleFn.__call__" has type "def __call__(self, key: Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex, *args: Any, shape: Sequence[int], **kwargs: Any) -> Array"
- jax/_src/pallas/mosaic/random.py:161: error: Argument 1 to "_make_stateful_sampler" has incompatible type "def uniform(key: Key | Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex, shape: Sequence[int] = ..., dtype: str | type[Any] | dtype[Any] | SupportsDType | None = ..., minval: Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex = ..., maxval: Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex = ..., *, out_sharding: NamedSharding | P | None = ...) -> Array"; expected "SampleFn"  [arg-type]
- jax/_src/pallas/mosaic/random.py:161: note: "SampleFn.__call__" has type "def __call__(self, key: Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex, *args: Any, shape: Sequence[int], **kwargs: Any) -> Array"
- jax/_src/pallas/mosaic/random.py:162: error: Argument 1 to "_make_stateful_sampler" has incompatible type "def bernoulli(key: Key | Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex, p: Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex = ..., shape: Sequence[int] | None = ..., mode: str = ..., *, out_sharding: NamedSharding | P | None = ...) -> Array"; expected "SampleFn"  [arg-type]
- jax/_src/pallas/mosaic/random.py:162: note: "SampleFn.__call__" has type "def __call__(self, key: Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex, *args: Any, shape: Sequence[int], **kwargs: Any) -> Array"
- jax/_src/pallas/mosaic/random.py:163: error: Argument 1 to "_make_stateful_sampler" has incompatible type "def normal(key: Key | Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex, shape: Sequence[int] = ..., dtype: str | type[Any] | dtype[Any] | SupportsDType | None = ..., *, mode: str | None = ..., out_sharding: NamedSharding | P | None = ...) -> Array"; expected "SampleFn"  [arg-type]
- jax/_src/pallas/mosaic/random.py:163: note: "SampleFn.__call__" has type "def __call__(self, key: Array | ndarray[tuple[Any, ...], dtype[Any]] | numpy.bool[builtins.bool] | number[Any, Any] | builtins.bool | int | float | complex, *args: Any, shape: Sequence[int], **kwargs: Any) -> Array"

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

2 participants