Skip to content

Fix variadic ParamSpec expansion crash - #21846

Open
KirschBluteX wants to merge 2 commits into
python:masterfrom
KirschBluteX:fix-21778-variadic-paramspec-crash
Open

Fix variadic ParamSpec expansion crash#21846
KirschBluteX wants to merge 2 commits into
python:masterfrom
KirschBluteX:fix-21778-variadic-paramspec-crash

Conversation

@KirschBluteX

@KirschBluteX KirschBluteX commented Aug 13, 2026

Copy link
Copy Markdown

Fixes #21778.

ExpandTypeVisitor.visit_parameters previously expanded parameter types independently from their kinds and names. A starred Unpack[TypeVarTuple] could therefore reach TypeVarTuple expansion with a concrete tuple replacement and raise NotImplementedError. A nested expansion could also produce a concrete prefix, a residual unpack, and a concrete suffix; emitting that suffix as positional parameters after *args created an invalid parameter layout.

This change expands a direct starred TypeVarTuple unpack while updating argument types, kinds, and names in lockstep. Concrete tuple items become positional parameters. When expansion leaves a residual variadic, that unpack and its suffix remain grouped as one starred tuple, preserving nested ParamSpec call semantics and the existing error-recovery path.

The regression tests cover the reported ParamSpec-based context-manager decorator, verify both the with-target and decorated call types, and exercise a nested print_args(0, *args, "end") forwarding call whose revealed result retains the residual TypeVarTuple and suffix.

Tests:

  • python -m pytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-parameter-specification.test
  • python -m pytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-typevar-tuple.test -k "ParamSpec or Unpack"
  • python -m pytest -n0 mypy/test/testtypes.py -k expand
  • python -m mypy --config-file mypy_self_check.ini mypy/expandtype.py
  • python -m ruff check mypy/expandtype.py
  • Targeted pre-commit hooks: trailing-whitespace, end-of-file-fixer, black, ruff-check, codespell
  • GitHub CI: 25 checks passed

Expand unpacked TypeVarTuple parameters while preserving aligned argument types, kinds, and names.

Fixes python#21778.
@github-actions

This comment has been minimized.

@TheMuffinMan1320

Copy link
Copy Markdown

Looks good!

Keep a residual unpack and its suffix together as one vararg when expanding Parameters. This avoids positional arguments after *args and preserves nested TypeVarTuple call semantics.
@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.

Crash with variadic type variable

2 participants