Skip to content

Commit b4afabe

Browse files
committed
fix: Support simple return annotations
Issue #9: #9
1 parent edef990 commit b4afabe

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/griffe_typingdoc/_static.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ def _returns_docs(func: Function, **kwargs: Any) -> DocstringSectionReturns | No
196196
"typing_extensions.Generator",
197197
}:
198198
return_annotation = annotation.slice.elements[2] # type: ignore[attr-defined]
199+
elif isinstance(annotation, ExprSubscript) and annotation.canonical_path in {
200+
"typing.Annotated",
201+
"typing_extensions.Annotated",
202+
}:
203+
return_annotation = annotation
199204

200205
if return_annotation:
201206
if isinstance(return_annotation, ExprSubscript) and return_annotation.is_tuple:

tests/test_extension.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@
77

88
from griffe_typingdoc import TypingDocExtension
99

10-
typing_imports = "from typing import Annotated, Doc, Generator, Iterator, Name, NotRequired, Raises, TypedDict, Unpack, Warns"
10+
typing_imports = (
11+
"from typing import Annotated, Doc, Generator, Iterator, Name, NotRequired, Raises, TypedDict, Unpack, Warns"
12+
)
1113
warning_imports = "from warnings import deprecated"
1214

15+
# NOTE: Important! The value in calls to `Doc` will be parsed as a Name expression
16+
# if it is valid Python syntax for names. To make sure it is correctly parsed as a string,
17+
# it must contain invalid syntax for names, such as a dot at the end.
18+
# The alternative solution would be to add `from __future__ import annotations`
19+
# at the beginning of each temporary visited module.
20+
1321

1422
def test_extension_on_itself() -> None:
1523
"""Load our own package using the extension, assert a parameters section is added to the parsed docstring."""
@@ -41,7 +49,6 @@ def test_parameter_doc() -> None:
4149
assert package["f"].docstring.parsed[1].value[0].description == "Hello."
4250

4351

44-
4552
def test_other_parameter_doc() -> None:
4653
"""Read documentation for other parameters, in unpack/typeddict annotations."""
4754
with temporary_visited_package(
@@ -133,3 +140,13 @@ def f() -> Generator[
133140
assert sections[2].value[1].description == "Second received."
134141
assert sections[3].value[0].description == "First returned."
135142
assert sections[3].value[1].description == "Second returned."
143+
144+
145+
def test_return_doc() -> None:
146+
"""Read documentation for return value."""
147+
with temporary_visited_package(
148+
"package",
149+
modules={"__init__.py": f"{typing_imports}\ndef f() -> Annotated[int, Doc('Hello.')]: ..."},
150+
extensions=Extensions(TypingDocExtension()),
151+
) as package:
152+
assert package["f"].docstring.parsed[1].value[0].description == "Hello."

0 commit comments

Comments
 (0)