Skip to content

fix(checker): avoid crash for constrained unpacked args - #21908

Open
daleselaji-dev wants to merge 1 commit into
python:masterfrom
daleselaji-dev:codex/fix-21907
Open

fix(checker): avoid crash for constrained unpacked args#21908
daleselaji-dev wants to merge 1 commit into
python:masterfrom
daleselaji-dev:codex/fix-21907

Conversation

@daleselaji-dev

Copy link
Copy Markdown

Problem

A constrained generic function whose *args annotation contains an unpacked concrete tuple can crash mypy with IndexError: list index out of range during function checking.

Root Cause

Expanding constrained type variables turns Unpack[Tuple[int, float]] into synthetic positional entries in the CallableType, but the copied FuncItem still contains only the original *args AST argument. store_argument_type() indexed the AST argument list using every expanded callable entry.

Solution

Ignore expanded callable entries that have no corresponding AST argument. The original variadic argument is still stored normally before expansion, while synthetic entries no longer overwrite it or index past the AST list.

Changes

  • Add a bounds guard in store_argument_type() for synthetic expanded arguments.
  • Add a generics regression case covering a constrained type variable followed by an unpacked tuple argument.

Testing

  • Baseline on current master (b974556f): the issue reproducer raised IndexError and reported INTERNAL_ERROR.
  • py -3.10 -m pytest 'mypy/test/testcheck.py::TypeCheckSuite::check-generics.test' -q: 197 passed, 2 skipped.
  • py -3.10 -m black --check mypy/types_utils.py: passed.
  • py -3.10 -m ruff check mypy/types_utils.py: passed.
  • py -3.10 -m compileall -q mypy/types_utils.py: passed.
  • py -3.10 -m mypy --config-file mypy_self_check.ini -p mypy: 196 source files passed.
  • git diff --check: passed.
  • The full repository test suite was not run.

Compatibility/Risk

The change affects only type storage during function checking and does not alter accepted type syntax or runtime behavior. Expanded synthetic arguments are skipped because they have no AST variable to receive a type; existing generic regression tests remain green.

Notes for Reviewer

The regression is reproduced with the Python 3.10-compatible spelling Unpack[Tuple[int, float]]; it exercises the same expanded callable shape as the Python 3.13 syntax in the issue. I first tried matching expanded entries by argument name, but that caused two existing generic tests to regress, so the final patch uses the narrower bounds-only guard.

Linked Issue

Fixes #21907

@daleselaji-dev
daleselaji-dev marked this pull request as ready for review August 29, 2026 02:59
@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.

"INTERNAL_ERROR": generic fun w/ Callable param w/ constrained return type followed by *args typed as *Tuple[int, float] crashes mypy

1 participant