Skip to content

Fix crash on invalid recursive variadic alias#21572

Merged
ilevkivskyi merged 3 commits into
python:masterfrom
ilevkivskyi:fix-pat-rec-tvt
Jun 3, 2026
Merged

Fix crash on invalid recursive variadic alias#21572
ilevkivskyi merged 3 commits into
python:masterfrom
ilevkivskyi:fix-pat-rec-tvt

Conversation

@ilevkivskyi
Copy link
Copy Markdown
Member

@ilevkivskyi ilevkivskyi commented May 31, 2026

Fixes #21125

This one was tricky. This is because the above issue actually exposed two different crash scenarios:

  • A crash on invalid constructs like *tuple[Ts] (must be *tuple[*Ts]).
  • An infinite recursion when trying to detect pathological and divergent aliases.

And while working on this I discovered two more cases:

  • A crash where an invalid type (like a union) appears in unpack in a recursive alias definition.
  • A crash on non-normalizeable recursive tuple.

I fix the first by tightening logic in typeanal.py w.r.t. where exactly a TypeVarTuple is allowed. I fix the second and third by avoiding get_proper_type() calls in expand_type() for recursive tuples. The fourth is the most problematic, and is kind of a fundamental thing. This PR only avoids an immediate crash for such aliases. We will still need to update various call sites where we special-case tuples to expect non-normal ones.

Couple more related things:

  • I fix couple issues with is_recursive cache invalidation.
  • I added a fast path to detect_diverging_alias() to avoid creating sets unless really needed.

@ilevkivskyi ilevkivskyi requested a review from JukkaL May 31, 2026 23:45
@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 2, 2026

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

@ilevkivskyi ilevkivskyi merged commit 363be14 into python:master Jun 3, 2026
25 checks passed
@ilevkivskyi ilevkivskyi deleted the fix-pat-rec-tvt branch June 3, 2026 10:22
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.

NotImplementedError crash on mutually recursive TypeVarTuple type aliases

2 participants