Fix crash with tuple unpack inside TypeVar default#20456
Fix crash with tuple unpack inside TypeVar default#20456ilevkivskyi merged 2 commits intopython:masterfrom
Conversation
This comment has been minimized.
This comment has been minimized.
| and unpacked_type.type.fullname == "builtins.tuple" | ||
| ): | ||
| items.append(unpacked_type.args[0]) | ||
| elif isinstance(unpacked_type, TupleType): |
There was a problem hiding this comment.
IIRC the idea was that it should never be true when we call tuple_fallback - we have several places where things like tuple[*tuple[X,Y]] are normalized to a plain tuple, and the original intent was to get rid of such constructs as early as possible. This is fine as a quickfix, but I'm afraid that this will manifest again when such nested tuple is used elsewhere.
There was a problem hiding this comment.
Not entirely sure about that but isn't the function already doing the flatting? E.g. just the line above flattens builtins.tuple Instances.
There was a problem hiding this comment.
builtins.tuple is already normalized: you can't rewrite e.g. tuple[str, *tuple[int, ...]] as a single tuple[...] construct. This is only about expanding unpacks of fixed-size TupleType types (which is always possible), see flatten_nested_tuples
There was a problem hiding this comment.
Think I found the right place now. The normalization happens in the TypeArgumentAnalyzer.
Lines 110 to 115 in b69309b
The visitor wasn't traversing TypeVar defaults.
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Fixes #20451