Skip to content

[stubgen] Fix UnpackType for 3.11+ #18421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -1225,6 +1225,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 @@ -1233,11 +1234,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 @@ -1246,30 +1250,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
Loading