Fix crash on invalid recursive variadic alias (#21572)
Fixes https://github.com/python/mypy/issues/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.
I
Ivan Levkivskyi committed
363be141eb198ab4526ae9d6dedb02b7aa462475
Parent: cbd8c82
Committed by GitHub <noreply@github.com>
on 6/3/2026, 10:22:13 AM