Skip to content

Commit

Permalink
[stubgen] Fix UnpackType for 3.11+ (#18421)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cdce8p authored Jan 7, 2025
1 parent 32b860e commit 306c1af
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
16 changes: 15 additions & 1 deletion mypy/stubutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ()
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -1257,30 +1261,38 @@ 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

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

Ts = TypeVarTuple('Ts')

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

def callback(func: Callable[[*Ts], None], *args: *Ts) -> None: ...

[case testObjectBaseClass]
class A(object): ...
[out]
Expand Down

0 comments on commit 306c1af

Please sign in to comment.