Skip to content

Commit c73781c

Browse files
committed
[stubgen] Fix UnpackType for 3.11+
1 parent bac9984 commit c73781c

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
@@ -1225,6 +1225,7 @@ from typing import Generic
12251225
from typing_extensions import TypeVarTuple, Unpack
12261226
Ts = TypeVarTuple('Ts')
12271227
class D(Generic[Unpack[Ts]]): ...
1228+
def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
12281229
[out]
12291230
from typing import Generic
12301231
from typing_extensions import TypeVarTuple, Unpack
@@ -1233,11 +1234,14 @@ Ts = TypeVarTuple('Ts')
12331234

12341235
class D(Generic[Unpack[Ts]]): ...
12351236

1237+
def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
1238+
12361239
[case testGenericClassTypeVarTuple_semanal]
12371240
from typing import Generic
12381241
from typing_extensions import TypeVarTuple, Unpack
12391242
Ts = TypeVarTuple('Ts')
12401243
class D(Generic[Unpack[Ts]]): ...
1244+
def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
12411245
[out]
12421246
from typing import Generic
12431247
from typing_extensions import TypeVarTuple, Unpack
@@ -1246,30 +1250,38 @@ Ts = TypeVarTuple('Ts')
12461250

12471251
class D(Generic[Unpack[Ts]]): ...
12481252

1253+
def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ...
1254+
12491255
[case testGenericClassTypeVarTuplePy311]
12501256
# flags: --python-version=3.11
12511257
from typing import Generic, TypeVarTuple
12521258
Ts = TypeVarTuple('Ts')
12531259
class D(Generic[*Ts]): ...
1260+
def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
12541261
[out]
12551262
from typing import Generic, TypeVarTuple
12561263

12571264
Ts = TypeVarTuple('Ts')
12581265

12591266
class D(Generic[*Ts]): ...
12601267

1268+
def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
1269+
12611270
[case testGenericClassTypeVarTuplePy311_semanal]
12621271
# flags: --python-version=3.11
12631272
from typing import Generic, TypeVarTuple
12641273
Ts = TypeVarTuple('Ts')
12651274
class D(Generic[*Ts]): ...
1275+
def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
12661276
[out]
12671277
from typing import Generic, TypeVarTuple
12681278

12691279
Ts = TypeVarTuple('Ts')
12701280

12711281
class D(Generic[*Ts]): ...
12721282

1283+
def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...
1284+
12731285
[case testObjectBaseClass]
12741286
class A(object): ...
12751287
[out]

0 commit comments

Comments
 (0)