Skip to content

Check the parameters before a gradual *args: Any, **kwargs: Any tail - #5692

Closed
samwgoldman wants to merge 1 commit into
mainfrom
export-D133047779
Closed

samwgoldman wants to merge 1 commit into
mainfrom
export-D133047779

Conversation

@samwgoldman

Copy link
Copy Markdown
Contributor

Summary:
Fixes #5194.

The typing spec treats *args: Any, **kwargs: Any in a signature as ..., but keeps the signature's other parameters. Pyrefly instead treated the whole signature as gradual: when either side of a callable assignability check had such a tail, any parameter mismatch counted as success. So def f(a: str, /, *args, **kwargs) was assignable to Callable[[int], None], and an override def m(self, x: str, *args: Any, **kwargs: Any) of def m(self, x: int) was accepted, also against an overloaded parent.

This change models such a signature as its other parameters followed by a gradual tail, and checks those parameters normally:

  • When the target has the tail, the parameter matcher matches the target's other parameters as usual, and the tail stands for whatever positional and keyword parameters of the source remain.
  • When the source has the tail, no special case is needed: its *args: Any and **kwargs: Any already accept the remaining parameters of the target, and its other parameters are checked like any others.
  • Against a ParamSpec, a remainder that is only the tail is .... So Callable[P, R] accepts it even where P is not being solved, and a gradual *args can consume the rest of a Concatenate prefix.

Because no errors are suppressed anymore, failed checks keep their details. strict-callable-subtyping still turns the gradual tail off.

Overrides use the same relation, so an override that lacks a parameter that precedes the parent's gradual tail (or the reverse) is now reported.

Error differential:

  • mypy_primer: no differences. pytorch and pandas: no changes.
  • numpy: -1, an assert_type in numpy's own typing tests now passes.

Differential Revision: D133047779

Summary:
Fixes #5194.

The typing spec treats `*args: Any, **kwargs: Any` in a signature as `...`, but keeps the signature's other parameters. Pyrefly instead treated the whole signature as gradual: when either side of a callable assignability check had such a tail, any parameter mismatch counted as success. So `def f(a: str, /, *args, **kwargs)` was assignable to `Callable[[int], None]`, and an override `def m(self, x: str, *args: Any, **kwargs: Any)` of `def m(self, x: int)` was accepted, also against an overloaded parent.

This change models such a signature as its other parameters followed by a gradual tail, and checks those parameters normally:
- When the target has the tail, the parameter matcher matches the target's other parameters as usual, and the tail stands for whatever positional and keyword parameters of the source remain.
- When the source has the tail, no special case is needed: its `*args: Any` and `**kwargs: Any` already accept the remaining parameters of the target, and its other parameters are checked like any others.
- Against a `ParamSpec`, a remainder that is only the tail is `...`. So `Callable[P, R]` accepts it even where `P` is not being solved, and a gradual `*args` can consume the rest of a `Concatenate` prefix.

Because no errors are suppressed anymore, failed checks keep their details. `strict-callable-subtyping` still turns the gradual tail off.

Overrides use the same relation, so an override that lacks a parameter that precedes the parent's gradual tail (or the reverse) is now reported.

Error differential:
- mypy_primer: no differences. pytorch and pandas: no changes.
- numpy: -1, an `assert_type` in numpy's own typing tests now passes.

Differential Revision: D133047779
@meta-cla meta-cla Bot added the cla signed label Nov 19, 2026
@meta-codesync

meta-codesync Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

@samwgoldman has exported this pull request. If you are a Meta employee, you can view the originating Diff in D133047779.

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@rchen152 rchen152 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review automatically exported from Phabricator review in Meta.

@meta-codesync

meta-codesync Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

This pull request has been merged in 76655fa.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Callable assignability ignores explicit positional parameters when the signature ends in *args: Any, **kwargs: Any

2 participants