Skip to content

Commit b229f27

Browse files
committed
stubgenc: add support for including class and property docstrings
1 parent 256cf68 commit b229f27

File tree

3 files changed

+31
-15
lines changed

3 files changed

+31
-15
lines changed

mypy/stubdoc.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class FunctionSig(NamedTuple):
7878
args: list[ArgSig]
7979
ret_type: str | None
8080
type_args: str = "" # TODO implement in stubgenc and remove the default
81+
docstring: str | None = None
8182

8283
def is_special_method(self) -> bool:
8384
return bool(
@@ -110,6 +111,7 @@ def format_sig(
110111
is_async: bool = False,
111112
any_val: str | None = None,
112113
docstring: str | None = None,
114+
include_docstrings: bool = False,
113115
) -> str:
114116
args: list[str] = []
115117
for arg in self.args:
@@ -144,8 +146,10 @@ def format_sig(
144146

145147
prefix = "async " if is_async else ""
146148
sig = f"{indent}{prefix}def {self.name}{self.type_args}({', '.join(args)}){retfield}:"
147-
if docstring:
148-
suffix = f"\n{indent} {mypy.util.quote_docstring(docstring)}"
149+
# if an overload has a docstring use it, otherwise fall back to the generic docstring
150+
doc = (self.docstring or docstring) if include_docstrings else None
151+
if doc:
152+
suffix = f"\n{indent} {mypy.util.quote_docstring(doc)}"
149153
else:
150154
suffix = " ..."
151155
return f"{sig}{suffix}"

mypy/stubgenc.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
infer_method_arg_types,
3838
infer_method_ret_type,
3939
)
40+
from mypy.util import quote_docstring
4041

4142

4243
class ExternalSignatureGenerator(SignatureGenerator):
@@ -645,8 +646,7 @@ def generate_function_stub(
645646
if inferred[0].args and inferred[0].args[0].name == "cls":
646647
decorators.append("@classmethod")
647648

648-
if docstring:
649-
docstring = self._indent_docstring(docstring)
649+
docstring = self._indent_docstring(ctx.docstring) if ctx.docstring else None
650650
output.extend(self.format_func_def(inferred, decorators=decorators, docstring=docstring))
651651
self._fix_iter(ctx, inferred, output)
652652

@@ -750,9 +750,16 @@ def generate_property_stub(
750750
)
751751
else: # regular property
752752
if readonly:
753+
docstring = self._indent_docstring(ctx.docstring) if ctx.docstring else None
753754
ro_properties.append(f"{self._indent}@property")
754755
sig = FunctionSig(name, [ArgSig("self")], inferred_type)
755-
ro_properties.append(sig.format_sig(indent=self._indent))
756+
ro_properties.append(
757+
sig.format_sig(
758+
indent=self._indent,
759+
docstring=docstring,
760+
include_docstrings=self._include_docstrings,
761+
)
762+
)
756763
else:
757764
if inferred_type is None:
758765
inferred_type = self.add_name("_typeshed.Incomplete")
@@ -867,8 +874,16 @@ def generate_class_stub(
867874
bases_str = "(%s)" % ", ".join(bases)
868875
else:
869876
bases_str = ""
870-
if types or static_properties or rw_properties or methods or ro_properties:
877+
878+
if class_info.docstring and self._include_docstrings:
879+
doc = f" {self._indent}{quote_docstring(class_info.docstring)}"
880+
docstring = doc.splitlines(keepends=False)
881+
else:
882+
docstring = []
883+
884+
if docstring or types or static_properties or rw_properties or methods or ro_properties:
871885
output.append(f"{self._indent}class {class_name}{bases_str}:")
886+
output.extend(docstring)
872887
for line in types:
873888
if (
874889
output
@@ -878,14 +893,10 @@ def generate_class_stub(
878893
):
879894
output.append("")
880895
output.append(line)
881-
for line in static_properties:
882-
output.append(line)
883-
for line in rw_properties:
884-
output.append(line)
885-
for line in methods:
886-
output.append(line)
887-
for line in ro_properties:
888-
output.append(line)
896+
output.extend(static_properties)
897+
output.extend(rw_properties)
898+
output.extend(methods)
899+
output.extend(ro_properties)
889900
else:
890901
output.append(f"{self._indent}class {class_name}{bases_str}: ...")
891902

mypy/stubutil.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ def format_func_def(
803803
signature.format_sig(
804804
indent=self._indent,
805805
is_async=is_coroutine,
806-
docstring=docstring if self._include_docstrings else None,
806+
docstring=docstring,
807+
include_docstrings=self._include_docstrings,
807808
)
808809
)
809810
return lines

0 commit comments

Comments
 (0)