Skip to content
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

stubgen: Fix crash on star unpack of TypeVarTuple #16869

Merged
merged 2 commits into from Feb 5, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions mypy/stubgen.py
Expand Up @@ -100,6 +100,7 @@
OpExpr,
OverloadedFuncDef,
SetExpr,
StarExpr,
Statement,
StrExpr,
TempNode,
Expand Down Expand Up @@ -339,6 +340,9 @@ def visit_ellipsis(self, node: EllipsisExpr) -> str:
def visit_op_expr(self, o: OpExpr) -> str:
return f"{o.left.accept(self)} {o.op} {o.right.accept(self)}"

def visit_star_expr(self, o: StarExpr) -> str:
return f"*{o.expr.accept(self)}"


def find_defined_names(file: MypyFile) -> set[str]:
finder = DefinitionFinder()
Expand Down
4 changes: 4 additions & 0 deletions mypy/test/teststubgen.py
Expand Up @@ -10,6 +10,8 @@
from types import ModuleType
from typing import Any

import pytest

from mypy.errors import CompileError
from mypy.moduleinspect import InspectError, ModuleInspect
from mypy.stubdoc import (
Expand Down Expand Up @@ -698,6 +700,8 @@ def run_case_inner(self, testcase: DataDrivenTestCase) -> None:
f.write(content)

options = self.parse_flags(source, extra)
if sys.version_info < options.pyversion:
pytest.skip()
modules = self.parse_modules(source)
out_dir = "out"
try:
Expand Down
50 changes: 50 additions & 0 deletions test-data/unit/stubgen.test
Expand Up @@ -1211,6 +1211,56 @@ T = TypeVar('T')

class D(Generic[T]): ...

[case testGenericClassTypeVarTuple]
from typing import Generic
from typing_extensions import TypeVarTuple, Unpack
Ts = TypeVarTuple('Ts')
class D(Generic[Unpack[Ts]]): ...
[out]
from typing import Generic
from typing_extensions import TypeVarTuple, Unpack

Ts = TypeVarTuple('Ts')

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

[case testGenericClassTypeVarTuple_semanal]
from typing import Generic
from typing_extensions import TypeVarTuple, Unpack
Ts = TypeVarTuple('Ts')
class D(Generic[Unpack[Ts]]): ...
[out]
from typing import Generic
from typing_extensions import TypeVarTuple, Unpack

Ts = TypeVarTuple('Ts')

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

[case testGenericClassTypeVarTuplePy311]
# flags: --python-version=3.11
from typing import Generic, TypeVarTuple
Ts = TypeVarTuple('Ts')
class D(Generic[*Ts]): ...
[out]
from typing import Generic, TypeVarTuple

Ts = TypeVarTuple('Ts')

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

[case testGenericClassTypeVarTuplePy311_semanal]
# flags: --python-version=3.11
from typing import Generic, TypeVarTuple
Ts = TypeVarTuple('Ts')
class D(Generic[*Ts]): ...
[out]
from typing import Generic, TypeVarTuple

Ts = TypeVarTuple('Ts')

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

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