Skip to content

Commit 0eaa652

Browse files
committed
Support more constructs in stubgen AliasPrinter
1 parent 67b70ce commit 0eaa652

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

mypy/stubgen.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
OpExpr,
104104
OverloadedFuncDef,
105105
SetExpr,
106+
SliceExpr,
106107
StarExpr,
107108
Statement,
108109
StrExpr,
@@ -355,6 +356,9 @@ def visit_tuple_expr(self, node: TupleExpr) -> str:
355356
def visit_list_expr(self, node: ListExpr) -> str:
356357
return f"[{', '.join(n.accept(self) for n in node.items)}]"
357358

359+
def visit_set_expr(self, node: SetExpr) -> str:
360+
return f"{{{', '.join(n.accept(self) for n in node.items)}}}"
361+
358362
def visit_dict_expr(self, o: DictExpr) -> str:
359363
dict_items = []
360364
for key, value in o.items:
@@ -369,13 +373,38 @@ def visit_ellipsis(self, node: EllipsisExpr) -> str:
369373
def visit_op_expr(self, o: OpExpr) -> str:
370374
return f"{o.left.accept(self)} {o.op} {o.right.accept(self)}"
371375

376+
def visit_unary_expr(self, o: UnaryExpr, /) -> str:
377+
return f"{o.op}{o.expr.accept(self)}"
378+
379+
def visit_slice_expr(self, o: SliceExpr, /) -> str:
380+
blocks = [
381+
o.begin_index.accept(self) if o.begin_index is not None else "",
382+
o.end_index.accept(self) if o.end_index is not None else "",
383+
]
384+
if o.stride is not None:
385+
blocks.append(o.stride.accept(self))
386+
return ":".join(blocks)
387+
372388
def visit_star_expr(self, o: StarExpr) -> str:
373389
return f"*{o.expr.accept(self)}"
374390

375391
def visit_lambda_expr(self, o: LambdaExpr) -> str:
376392
# TODO: Required for among other things dataclass.field default_factory
377393
return self.stubgen.add_name("_typeshed.Incomplete")
378394

395+
def _visit_unsupported_expr(self, o: object) -> str:
396+
# Something we do not understand.
397+
return self.stubgen.add_name("_typeshed.Incomplete")
398+
399+
visit_comparison_expr = _visit_unsupported_expr
400+
visit_cast_expr = _visit_unsupported_expr
401+
visit_conditional_expr = _visit_unsupported_expr
402+
403+
visit_list_comprehension = _visit_unsupported_expr
404+
visit_set_comprehension = _visit_unsupported_expr
405+
visit_dictionary_comprehension = _visit_unsupported_expr
406+
visit_generator_expr = _visit_unsupported_expr
407+
379408

380409
def find_defined_names(file: MypyFile) -> set[str]:
381410
finder = DefinitionFinder()

test-data/unit/stubgen.test

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4269,6 +4269,34 @@ class Y(missing.Base):
42694269
generated_kwargs: float
42704270
generated_kwargs_: float
42714271

4272+
[case testDataclassAliasPrinterVariations_semanal]
4273+
from dataclasses import dataclass, field
4274+
4275+
@dataclass
4276+
class X:
4277+
a: int = field(default=-1)
4278+
b: set[int] = field(default={0})
4279+
c: list[int] = field(default=[x for x in range(5)])
4280+
c: list[int] = field(default={x: x for x in range(5)})
4281+
e: tuple[int, int] = field(default=(1, 2, 3)[1:])
4282+
f: tuple[int, int] = field(default=(1, 2, 3)[:2])
4283+
g: tuple[int, int] = field(default=(1, 2, 3)[::2])
4284+
h: tuple[int] = field(default=(1, 2, 3)[1::2])
4285+
4286+
[out]
4287+
from _typeshed import Incomplete
4288+
from dataclasses import dataclass, field
4289+
4290+
@dataclass
4291+
class X:
4292+
a: int = field(default=-1)
4293+
b: set[int] = field(default={0})
4294+
c: list[int] = field(default=Incomplete)
4295+
e: tuple[int, int] = field(default=(1, 2, 3)[1:])
4296+
f: tuple[int, int] = field(default=(1, 2, 3)[:2])
4297+
g: tuple[int, int] = field(default=(1, 2, 3)[::2])
4298+
h: tuple[int] = field(default=(1, 2, 3)[1::2])
4299+
42724300
[case testDataclassTransform]
42734301
# dataclass_transform detection only works with semantic analysis.
42744302
# Test stubgen doesn't break too badly without it.

0 commit comments

Comments
 (0)