|
102 | 102 | OverloadedFuncDef,
|
103 | 103 | Statement,
|
104 | 104 | StrExpr,
|
| 105 | + TempNode, |
105 | 106 | TupleExpr,
|
106 | 107 | TypeInfo,
|
107 | 108 | UnaryExpr,
|
@@ -637,6 +638,7 @@ def __init__(
|
637 | 638 | self._state = EMPTY
|
638 | 639 | self._toplevel_names: list[str] = []
|
639 | 640 | self._include_private = include_private
|
| 641 | + self._current_class: ClassDef | None = None |
640 | 642 | self.import_tracker = ImportTracker()
|
641 | 643 | # Was the tree semantically analysed before?
|
642 | 644 | self.analyzed = analyzed
|
@@ -886,6 +888,7 @@ def get_fullname(self, expr: Expression) -> str:
|
886 | 888 | return resolved_name
|
887 | 889 |
|
888 | 890 | def visit_class_def(self, o: ClassDef) -> None:
|
| 891 | + self._current_class = o |
889 | 892 | self.method_names = find_method_names(o.defs.body)
|
890 | 893 | sep: int | None = None
|
891 | 894 | if not self._indent and self._state != EMPTY:
|
@@ -922,6 +925,7 @@ def visit_class_def(self, o: ClassDef) -> None:
|
922 | 925 | else:
|
923 | 926 | self._state = CLASS
|
924 | 927 | self.method_names = set()
|
| 928 | + self._current_class = None |
925 | 929 |
|
926 | 930 | def get_base_types(self, cdef: ClassDef) -> list[str]:
|
927 | 931 | """Get list of base classes for a class."""
|
@@ -1330,7 +1334,20 @@ def get_init(
|
1330 | 1334 | typename += f"[{final_arg}]"
|
1331 | 1335 | else:
|
1332 | 1336 | typename = self.get_str_type_of_node(rvalue)
|
1333 |
| - return f"{self._indent}{lvalue}: {typename}\n" |
| 1337 | + initializer = self.get_assign_initializer(rvalue) |
| 1338 | + return f"{self._indent}{lvalue}: {typename}{initializer}\n" |
| 1339 | + |
| 1340 | + def get_assign_initializer(self, rvalue: Expression) -> str: |
| 1341 | + """Does this rvalue need some special initializer value?""" |
| 1342 | + if self._current_class and self._current_class.info: |
| 1343 | + # Current rules |
| 1344 | + # 1. Return `...` if we are dealing with `NamedTuple` and it has an existing default value |
| 1345 | + if self._current_class.info.is_named_tuple and not isinstance(rvalue, TempNode): |
| 1346 | + return " = ..." |
| 1347 | + # TODO: support other possible cases, where initializer is important |
| 1348 | + |
| 1349 | + # By default, no initializer is required: |
| 1350 | + return "" |
1334 | 1351 |
|
1335 | 1352 | def add(self, string: str) -> None:
|
1336 | 1353 | """Add text to generated stub."""
|
|
0 commit comments