Skip to content

Support several more constructs in stubgen's AliasPrinter #18888

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
47 changes: 47 additions & 0 deletions mypy/stubgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,38 @@
Block,
BytesExpr,
CallExpr,
CastExpr,
ClassDef,
ComparisonExpr,
ComplexExpr,
ConditionalExpr,
Decorator,
DictExpr,
DictionaryComprehension,
EllipsisExpr,
Expression,
ExpressionStmt,
FloatExpr,
FuncBase,
FuncDef,
GeneratorExpr,
IfStmt,
Import,
ImportAll,
ImportFrom,
IndexExpr,
IntExpr,
LambdaExpr,
ListComprehension,
ListExpr,
MemberExpr,
MypyFile,
NameExpr,
OpExpr,
OverloadedFuncDef,
SetComprehension,
SetExpr,
SliceExpr,
StarExpr,
Statement,
StrExpr,
Expand Down Expand Up @@ -355,6 +362,9 @@ def visit_tuple_expr(self, node: TupleExpr) -> str:
def visit_list_expr(self, node: ListExpr) -> str:
return f"[{', '.join(n.accept(self) for n in node.items)}]"

def visit_set_expr(self, node: SetExpr) -> str:
return f"{{{', '.join(n.accept(self) for n in node.items)}}}"

def visit_dict_expr(self, o: DictExpr) -> str:
dict_items = []
for key, value in o.items:
Expand All @@ -369,13 +379,50 @@ 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_unary_expr(self, o: UnaryExpr, /) -> str:
return f"{o.op}{o.expr.accept(self)}"

def visit_slice_expr(self, o: SliceExpr, /) -> str:
blocks = [
o.begin_index.accept(self) if o.begin_index is not None else "",
o.end_index.accept(self) if o.end_index is not None else "",
]
if o.stride is not None:
blocks.append(o.stride.accept(self))
return ":".join(blocks)

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

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

def _visit_unsupported_expr(self, o: object) -> str:
# Something we do not understand.
return self.stubgen.add_name("_typeshed.Incomplete")

def visit_comparison_expr(self, o: ComparisonExpr) -> str:
return self._visit_unsupported_expr(o)

def visit_cast_expr(self, o: CastExpr) -> str:
return self._visit_unsupported_expr(o)

def visit_conditional_expr(self, o: ConditionalExpr) -> str:
return self._visit_unsupported_expr(o)

def visit_list_comprehension(self, o: ListComprehension) -> str:
return self._visit_unsupported_expr(o)

def visit_set_comprehension(self, o: SetComprehension) -> str:
return self._visit_unsupported_expr(o)

def visit_dictionary_comprehension(self, o: DictionaryComprehension) -> str:
return self._visit_unsupported_expr(o)

def visit_generator_expr(self, o: GeneratorExpr) -> str:
return self._visit_unsupported_expr(o)


def find_defined_names(file: MypyFile) -> set[str]:
finder = DefinitionFinder()
Expand Down
29 changes: 29 additions & 0 deletions test-data/unit/stubgen.test
Original file line number Diff line number Diff line change
Expand Up @@ -4269,6 +4269,35 @@ class Y(missing.Base):
generated_kwargs: float
generated_kwargs_: float

[case testDataclassAliasPrinterVariations_semanal]
from dataclasses import dataclass, field

@dataclass
class X:
a: int = field(default=-1)
b: set[int] = field(default={0})
c: list[int] = field(default=[x for x in range(5)])
d: dict[int, int] = field(default={x: x for x in range(5)})
e: tuple[int, int] = field(default=(1, 2, 3)[1:])
f: tuple[int, int] = field(default=(1, 2, 3)[:2])
g: tuple[int, int] = field(default=(1, 2, 3)[::2])
h: tuple[int] = field(default=(1, 2, 3)[1::2])

[out]
from _typeshed import Incomplete
from dataclasses import dataclass, field

@dataclass
class X:
a: int = field(default=-1)
b: set[int] = field(default={0})
c: list[int] = field(default=Incomplete)
d: dict[int, int] = field(default=Incomplete)
e: tuple[int, int] = field(default=(1, 2, 3)[1:])
f: tuple[int, int] = field(default=(1, 2, 3)[:2])
g: tuple[int, int] = field(default=(1, 2, 3)[::2])
h: tuple[int] = field(default=(1, 2, 3)[1::2])

[case testDataclassTransform]
# dataclass_transform detection only works with semantic analysis.
# Test stubgen doesn't break too badly without it.
Expand Down