Skip to content

Commit

Permalink
stubgen: Fix crash on star unpack of TypeVarTuple (#16869)
Browse files Browse the repository at this point in the history
Fixes #16864
  • Loading branch information
hamdanal authored Feb 5, 2024
1 parent 8c2ef9d commit 7bdd61f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
4 changes: 4 additions & 0 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 7bdd61f

Please sign in to comment.