Skip to content

Commit 3804f7e

Browse files
authored
Expand TypeVarTuple default (PEP 696) (#16851)
Small change to fix an error when expanding a TypeVarTuple default. ``` RuntimeError: Invalid type replacement to expand: Unpack[tuple[builtins.int, builtins.str]] ``` Ref: #14851
1 parent 3f58c2d commit 3804f7e

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

mypy/expandtype.py

+2
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ def visit_unpack_type(self, t: UnpackType) -> Type:
277277
def expand_unpack(self, t: UnpackType) -> list[Type]:
278278
assert isinstance(t.type, TypeVarTupleType)
279279
repl = get_proper_type(self.variables.get(t.type.id, t.type))
280+
if isinstance(repl, UnpackType):
281+
repl = get_proper_type(repl.type)
280282
if isinstance(repl, TupleType):
281283
return repl.items
282284
elif (

mypy/test/testtypes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,7 @@ def make_call(*items: tuple[str, str | None]) -> CallExpr:
15411541
class TestExpandTypeLimitGetProperType(TestCase):
15421542
# WARNING: do not increase this number unless absolutely necessary,
15431543
# and you understand what you are doing.
1544-
ALLOWED_GET_PROPER_TYPES = 8
1544+
ALLOWED_GET_PROPER_TYPES = 9
15451545

15461546
@skipUnless(mypy.expandtype.__file__.endswith(".py"), "Skip for compiled mypy")
15471547
def test_count_get_proper_type(self) -> None:

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -289,8 +289,8 @@ def func_c1(
289289
# reveal_type(a) # Revealed type is "__main__.ClassC1[builtins.int, builtins.str]" # TODO
290290
reveal_type(b) # N: Revealed type is "__main__.ClassC1[builtins.float]"
291291

292-
# k = ClassC1() # TODO
293-
# reveal_type(k) # Revealed type is "__main__.ClassC1[builtins.int, builtins.str]" # TODO
292+
k = ClassC1()
293+
reveal_type(k) # N: Revealed type is "__main__.ClassC1[builtins.int, builtins.str]"
294294
l = ClassC1[float]()
295295
reveal_type(l) # N: Revealed type is "__main__.ClassC1[builtins.float]"
296296

@@ -305,8 +305,8 @@ def func_c2(
305305
# reveal_type(b) # Revealed type is "__main__.ClassC2[builtins.int, Unpack[builtins.tuple[builtins.float, ...]]]" # TODO
306306
reveal_type(c) # N: Revealed type is "__main__.ClassC2[builtins.int]"
307307

308-
# k = ClassC2() # TODO
309-
# reveal_type(k) # Revealed type is "__main__.ClassC2[builtins.str, Unpack[builtins.tuple[builtins.float, ...]]]" # TODO
308+
k = ClassC2()
309+
reveal_type(k) # N: Revealed type is "__main__.ClassC2[builtins.str, Unpack[builtins.tuple[builtins.float, ...]]]"
310310
l = ClassC2[int]()
311311
# reveal_type(l) # Revealed type is "__main__.ClassC2[builtins.int, Unpack[builtins.tuple[builtins.float, ...]]]" # TODO
312312
m = ClassC2[int, Unpack[Tuple[()]]]()
@@ -323,8 +323,8 @@ def func_c3(
323323
reveal_type(b) # N: Revealed type is "__main__.ClassC3[builtins.int]"
324324
reveal_type(c) # N: Revealed type is "__main__.ClassC3[builtins.int, builtins.float]"
325325

326-
# k = ClassC3() # TODO
327-
# reveal_type(k) # Revealed type is "__main__.ClassC3[builtins.str]" # TODO
326+
k = ClassC3()
327+
reveal_type(k) # N: Revealed type is "__main__.ClassC3[builtins.str]"
328328
l = ClassC3[int]()
329329
reveal_type(l) # N: Revealed type is "__main__.ClassC3[builtins.int]"
330330
m = ClassC3[int, Unpack[Tuple[float]]]()

0 commit comments

Comments
 (0)