Skip to content

Commit e12ffce

Browse files
committed
misc: support lone :rtype: meta
1 parent c6a28d7 commit e12ffce

File tree

6 files changed

+38
-9
lines changed

6 files changed

+38
-9
lines changed

docstring_parser/epydoc.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def parse(text: str) -> Docstring:
156156

157157
meta_item = DocstringParam(
158158
args=[key, arg_name],
159-
description=info["description"],
159+
description=info.get("description"),
160160
arg_name=arg_name,
161161
type_name=type_name,
162162
is_optional=is_optional,
@@ -167,7 +167,7 @@ def parse(text: str) -> Docstring:
167167
info = params["return"]
168168
meta_item = DocstringReturns(
169169
args=[key],
170-
description=info["description"],
170+
description=info.get("description"),
171171
type_name=info.get("type_name"),
172172
is_generator=info.get("is_generator", False),
173173
)
@@ -256,8 +256,9 @@ def process_desc(desc: T.Optional[str], is_type: bool) -> str:
256256
if meta.type_name:
257257
text = f"@{type_key}:" + process_desc(meta.type_name, True)
258258
parts.append(text)
259-
text = f"@{arg_key}:" + process_desc(meta.description, False)
260-
parts.append(text)
259+
if meta.description:
260+
text = f"@{arg_key}:" + process_desc(meta.description, False)
261+
parts.append(text)
261262
elif isinstance(meta, DocstringRaises):
262263
text = f"@raise {meta.type_name}:" if meta.type_name else "@raise:"
263264
text += process_desc(meta.description, False)

docstring_parser/rest.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ def parse(text: str) -> Docstring:
160160
elif isinstance(meta, DocstringReturns):
161161
meta.type_name = meta.type_name or rtypes.get(meta.return_name)
162162

163+
if not any(isinstance(m, DocstringReturns) for m in ret.meta) and rtypes:
164+
for (return_name, type_name) in rtypes.items():
165+
ret.meta.append(
166+
DocstringReturns(
167+
args=[],
168+
type_name=type_name,
169+
description=None,
170+
is_generator=False,
171+
return_name=return_name,
172+
)
173+
)
174+
163175
return ret
164176

165177

@@ -227,9 +239,10 @@ def process_desc(desc: T.Optional[str]) -> str:
227239
key = "yields" if meta.is_generator else "returns"
228240

229241
if rendering_style == RenderingStyle.EXPANDED:
230-
text = f":{key}:"
231-
text += process_desc(meta.description)
232-
parts.append(text)
242+
if meta.description:
243+
text = f":{key}:"
244+
text += process_desc(meta.description)
245+
parts.append(text)
233246
if type_text:
234247
parts.append(f":rtype:{type_text}")
235248
else:

docstring_parser/tests/test_epydoc.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,10 @@ def test_compose_expanded(source: str, expected: str) -> None:
714714
compose(parse(source), rendering_style=RenderingStyle.EXPANDED)
715715
== expected
716716
)
717+
718+
719+
def test_short_rtype() -> None:
720+
"""Test abbreviated docstring with only return type information."""
721+
string = "Short description.\n\n@rtype: float"
722+
docstring = parse(string)
723+
assert compose(docstring) == string

docstring_parser/tests/test_google.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def test_meta_with_multiline_description() -> None:
329329
assert docstring.meta[0].description == "asd\n1\n 2\n3"
330330

331331

332-
def test_default_args():
332+
def test_default_args() -> None:
333333
"""Test parsing default arguments."""
334334
docstring = parse(
335335
"""A sample function

docstring_parser/tests/test_numpydoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ def test_meta_with_multiline_description() -> None:
225225
assert docstring.meta[0].description == "asd\n1\n 2\n3"
226226

227227

228-
def test_default_args():
228+
def test_default_args() -> None:
229229
"""Test parsing default arguments."""
230230
docstring = parse(
231231
"""

docstring_parser/tests/test_rest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,11 @@ def test_compose(rendering_style: RenderingStyle, expected: str) -> None:
521521
"""
522522
)
523523
assert compose(docstring, rendering_style=rendering_style) == expected
524+
525+
526+
def test_short_rtype() -> None:
527+
"""Test abbreviated docstring with only return type information."""
528+
string = "Short description.\n\n:rtype: float"
529+
docstring = parse(string)
530+
rendering_style = RenderingStyle.EXPANDED
531+
assert compose(docstring, rendering_style=rendering_style) == string

0 commit comments

Comments
 (0)