Skip to content

Commit 306c1af

Browse files
authored
[stubgen] Fix UnpackType for 3.11+ (#18421)
Don't replace `*Ts` with `Unpack[Ts]` on Python 3.11+. This is broken currently since `Unpack` isn't added as import in the stub file.
1 parent 32b860e commit 306c1af

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

mypy/stubutil.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@
1919
from mypy.moduleinspect import InspectError, ModuleInspect
2020
from mypy.nodes import PARAM_SPEC_KIND, TYPE_VAR_TUPLE_KIND, ClassDef, FuncDef, TypeAliasStmt
2121
from mypy.stubdoc import ArgSig, FunctionSig
22-
from mypy.types import AnyType, NoneType, Type, TypeList, TypeStrVisitor, UnboundType, UnionType
22+
from mypy.types import (
23+
AnyType,
24+
NoneType,
25+
Type,
26+
TypeList,
27+
TypeStrVisitor,
28+
UnboundType,
29+
UnionType,
30+
UnpackType,
31+
)
2332

2433
# Modules that may fail when imported, or that may have side effects (fully qualified).
2534
NOT_IMPORTABLE_MODULES = ()
@@ -292,6 +301,11 @@ def visit_type_list(self, t: TypeList) -> str:
292301
def visit_union_type(self, t: UnionType) -> str:
293302
return " | ".join([item.accept(self) for item in t.items])
294303

304+
def visit_unpack_type(self, t: UnpackType) -> str:
305+
if self.options.python_version >= (3, 11):
306+
return f"*{t.type.accept(self)}"
307+
return super().visit_unpack_type(t)
308+
295309
def args_str(self, args: Iterable[Type]) -> str:
296310
"""Convert an array of arguments to strings and join the results with commas.
297311

test-data/unit/stubgen.test

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,6 +1236,7 @@ from typing import Generic
12361236
from typing_extensions import TypeVarTuple, Unpack
12371237
Ts = TypeVarTuple('Ts')
12381238
class D(Generic[Unpack[Ts]]): ...
1239+
def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
12391240
[out]
12401241
from typing import Generic
12411242
from typing_extensions import TypeVarTuple, Unpack
@@ -1244,11 +1245,14 @@ Ts = TypeVarTuple('Ts')
12441245

12451246
class D(Generic[Unpack[Ts]]): ...
12461247

1248+
def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
1249+
12471250
[case testGenericClassTypeVarTuple_semanal]
12481251
from typing import Generic
12491252
from typing_extensions import TypeVarTuple, Unpack
12501253
Ts = TypeVarTuple('Ts')
12511254
class D(Generic[Unpack[Ts]]): ...
1255+
def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
12521256
[out]
12531257
from typing import Generic
12541258
from typing_extensions import TypeVarTuple, Unpack
@@ -1257,30 +1261,38 @@ Ts = TypeVarTuple('Ts')
12571261

12581262
class D(Generic[Unpack[Ts]]): ...
12591263

1264+
def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
1265+
12601266
[case testGenericClassTypeVarTuplePy311]
12611267
# flags: --python-version=3.11
12621268
from typing import Generic, TypeVarTuple
12631269
Ts = TypeVarTuple('Ts')
12641270
class D(Generic[*Ts]): ...
1271+
def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
12651272
[out]
12661273
from typing import Generic, TypeVarTuple
12671274

12681275
Ts = TypeVarTuple('Ts')
12691276

12701277
class D(Generic[*Ts]): ...
12711278

1279+
def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
1280+
12721281
[case testGenericClassTypeVarTuplePy311_semanal]
12731282
# flags: --python-version=3.11
12741283
from typing import Generic, TypeVarTuple
12751284
Ts = TypeVarTuple('Ts')
12761285
class D(Generic[*Ts]): ...
1286+
def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
12771287
[out]
12781288
from typing import Generic, TypeVarTuple
12791289

12801290
Ts = TypeVarTuple('Ts')
12811291

12821292
class D(Generic[*Ts]): ...
12831293

1294+
def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
1295+
12841296
[case testObjectBaseClass]
12851297
class A(object): ...
12861298
[out]

0 commit comments

Comments
 (0)