Skip to content

Fix TypeVarTuple subscript with defaulted ParamSpec - #22574

Open
Sudhakar7J wants to merge 2 commits into
python:masterfrom
Sudhakar7J:fix/tvt-default-paramspec-22375
Open

Sudhakar7J wants to merge 2 commits into
python:masterfrom
Sudhakar7J:fix/tvt-default-paramspec-22375

Conversation

@Sudhakar7J

Copy link
Copy Markdown

Fixes #22375

This fixes type argument splitting for generic types that include a TypeVarTuple followed by a defaulted ParamSpec (PEP 696). When a defaulted ParamSpec suffix argument was omitted, mypy split arguments by fixed prefix/suffix counts and incorrectly assigned a regular type argument to the ParamSpec, producing Can only replace ParamSpec... and degrading inferred types to Any.

The fix detects omitted defaulted suffix variables in variadic generic splitting and only consumes a trailing optional ParamSpec argument when it is a valid ParamSpec argument (ParamSpec, parameter list/Parameters, .../Any-like, or UnboundType). The normalized arguments are then used for instance validation/fixing, type alias instantiation, and callable type application.

AI assistance: I used an AI coding assistant to help investigate the issue and draft the patch/tests; I reviewed the behavior, ran targeted tests, and verified the reported reproduction.

When a TypeVarTuple is followed by a ParamSpec with a default, allow the ParamSpec argument to be omitted and only consume a trailing argument as the ParamSpec when it is a valid ParamSpec argument. This fixes incorrect splitting that produced 'Can only replace ParamSpec...' errors and Any fallbacks.

Fixes python#22375
@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
Contributor

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

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.

subscripting a class with a TypeVarTuple followed by a defaulted ParamSpec reports "Can only replace ParamSpec" and infers Any

1 participant