From 306c1afb9f7a8f6d10cbd1ab05fabe4de53fdcd4 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Tue, 7 Jan 2025 02:54:24 +0100 Subject: [PATCH] [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. --- mypy/stubutil.py | 16 +++++++++++++++- test-data/unit/stubgen.test | 12 ++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/mypy/stubutil.py b/mypy/stubutil.py index cbb3d2f77414..fecd9b29d57d 100644 --- a/mypy/stubutil.py +++ b/mypy/stubutil.py @@ -19,7 +19,16 @@ from mypy.moduleinspect import InspectError, ModuleInspect from mypy.nodes import PARAM_SPEC_KIND, TYPE_VAR_TUPLE_KIND, ClassDef, FuncDef, TypeAliasStmt from mypy.stubdoc import ArgSig, FunctionSig -from mypy.types import AnyType, NoneType, Type, TypeList, TypeStrVisitor, UnboundType, UnionType +from mypy.types import ( + AnyType, + NoneType, + Type, + TypeList, + TypeStrVisitor, + UnboundType, + UnionType, + UnpackType, +) # Modules that may fail when imported, or that may have side effects (fully qualified). NOT_IMPORTABLE_MODULES = () @@ -292,6 +301,11 @@ def visit_type_list(self, t: TypeList) -> str: def visit_union_type(self, t: UnionType) -> str: return " | ".join([item.accept(self) for item in t.items]) + def visit_unpack_type(self, t: UnpackType) -> str: + if self.options.python_version >= (3, 11): + return f"*{t.type.accept(self)}" + return super().visit_unpack_type(t) + def args_str(self, args: Iterable[Type]) -> str: """Convert an array of arguments to strings and join the results with commas. diff --git a/test-data/unit/stubgen.test b/test-data/unit/stubgen.test index 9cfe301a9d0b..5d35f1bb77ce 100644 --- a/test-data/unit/stubgen.test +++ b/test-data/unit/stubgen.test @@ -1236,6 +1236,7 @@ from typing import Generic from typing_extensions import TypeVarTuple, Unpack Ts = TypeVarTuple('Ts') class D(Generic[Unpack[Ts]]): ... +def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ... [out] from typing import Generic from typing_extensions import TypeVarTuple, Unpack @@ -1244,11 +1245,14 @@ Ts = TypeVarTuple('Ts') class D(Generic[Unpack[Ts]]): ... +def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ... + [case testGenericClassTypeVarTuple_semanal] from typing import Generic from typing_extensions import TypeVarTuple, Unpack Ts = TypeVarTuple('Ts') class D(Generic[Unpack[Ts]]): ... +def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ... [out] from typing import Generic from typing_extensions import TypeVarTuple, Unpack @@ -1257,11 +1261,14 @@ Ts = TypeVarTuple('Ts') class D(Generic[Unpack[Ts]]): ... +def callback(func: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None: ... + [case testGenericClassTypeVarTuplePy311] # flags: --python-version=3.11 from typing import Generic, TypeVarTuple Ts = TypeVarTuple('Ts') class D(Generic[*Ts]): ... +def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ... [out] from typing import Generic, TypeVarTuple @@ -1269,11 +1276,14 @@ Ts = TypeVarTuple('Ts') class D(Generic[*Ts]): ... +def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ... + [case testGenericClassTypeVarTuplePy311_semanal] # flags: --python-version=3.11 from typing import Generic, TypeVarTuple Ts = TypeVarTuple('Ts') class D(Generic[*Ts]): ... +def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ... [out] from typing import Generic, TypeVarTuple @@ -1281,6 +1291,8 @@ Ts = TypeVarTuple('Ts') class D(Generic[*Ts]): ... +def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ... + [case testObjectBaseClass] class A(object): ... [out]