Skip to content

Commit 053c054

Browse files
authored
Fix crash on invalid unpack in base class (#19962)
Fixes #19960 Fix is trivial, we can't use the "assertion" before type checking phase. I also fix missing line/column for union types. I am surprised this didn't cause problems before.
1 parent 4936099 commit 053c054

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

mypy/semanal_shared.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ def calculate_tuple_fallback(typ: TupleType) -> None:
303303
):
304304
items.append(unpacked_type.args[0])
305305
else:
306-
raise NotImplementedError
306+
# This is called before semanal_typeargs.py fixes broken unpacks,
307+
# where the error should also be generated.
308+
items.append(AnyType(TypeOfAny.from_error))
307309
else:
308310
items.append(item)
309311
fallback.args = (make_simplified_union(items),)

mypy/typeanal.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ def try_analyze_special_unbound_type(self, t: UnboundType, fullname: str) -> Typ
634634
)
635635
elif fullname == "typing.Union":
636636
items = self.anal_array(t.args)
637-
return UnionType.make_union(items)
637+
return UnionType.make_union(items, line=t.line, column=t.column)
638638
elif fullname == "typing.Optional":
639639
if len(t.args) != 1:
640640
self.fail(

test-data/unit/check-python312.test

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2121,3 +2121,13 @@ class A: ...
21212121

21222122
x1: Alias1[A] # ok
21232123
x2: Alias2[A] # ok
2124+
2125+
[case testUndefinedUnpackInPEP696Base]
2126+
# Typo below is intentional.
2127+
class MyTuple[*Ts](tuple[*TS]): # E: Name "TS" is not defined
2128+
...
2129+
2130+
x: MyTuple[int, str]
2131+
reveal_type(x[0]) # N: Revealed type is "Any"
2132+
[builtins fixtures/tuple.pyi]
2133+
[typing fixtures/typing-full.pyi]

test-data/unit/check-typevar-tuple.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,3 +2692,27 @@ tuple(a)
26922692
(x,) = a
26932693
(_,) = a
26942694
[builtins fixtures/tuple.pyi]
2695+
2696+
[case testNoCrashOnUndefinedUnpackInBase]
2697+
from typing import TypeVarTuple, Generic, Unpack
2698+
2699+
Ts = TypeVarTuple("Ts")
2700+
2701+
class MyTuple(tuple[Unpack[TsWithTypo]], Generic[Unpack[Ts]]): # E: Name "TsWithTypo" is not defined
2702+
...
2703+
2704+
x: MyTuple[int, str]
2705+
reveal_type(x[0]) # N: Revealed type is "Any"
2706+
[builtins fixtures/tuple.pyi]
2707+
2708+
[case testNoCrashOnInvalidUnpackInBase]
2709+
from typing import TypeVarTuple, Generic, Unpack, Union
2710+
2711+
Ts = TypeVarTuple("Ts")
2712+
2713+
class MyTuple(tuple[Unpack[Union[int, str]]], Generic[Unpack[Ts]]): # E: "Union[int, str]" cannot be unpacked (must be tuple or TypeVarTuple)
2714+
...
2715+
2716+
x: MyTuple[int, str]
2717+
reveal_type(x[0]) # N: Revealed type is "Any"
2718+
[builtins fixtures/tuple.pyi]

0 commit comments

Comments
 (0)