Skip to content

Commit

Permalink
fix: Support simple return annotations
Browse files Browse the repository at this point in the history
Issue #9: #9
  • Loading branch information
pawamoy committed Feb 8, 2024
1 parent edef990 commit b4afabe
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/griffe_typingdoc/_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ def _returns_docs(func: Function, **kwargs: Any) -> DocstringSectionReturns | No
"typing_extensions.Generator",
}:
return_annotation = annotation.slice.elements[2] # type: ignore[attr-defined]
elif isinstance(annotation, ExprSubscript) and annotation.canonical_path in {
"typing.Annotated",
"typing_extensions.Annotated",
}:
return_annotation = annotation

if return_annotation:
if isinstance(return_annotation, ExprSubscript) and return_annotation.is_tuple:
Expand Down
21 changes: 19 additions & 2 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,17 @@

from griffe_typingdoc import TypingDocExtension

typing_imports = "from typing import Annotated, Doc, Generator, Iterator, Name, NotRequired, Raises, TypedDict, Unpack, Warns"
typing_imports = (
"from typing import Annotated, Doc, Generator, Iterator, Name, NotRequired, Raises, TypedDict, Unpack, Warns"
)
warning_imports = "from warnings import deprecated"

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


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



def test_other_parameter_doc() -> None:
"""Read documentation for other parameters, in unpack/typeddict annotations."""
with temporary_visited_package(
Expand Down Expand Up @@ -133,3 +140,13 @@ def f() -> Generator[
assert sections[2].value[1].description == "Second received."
assert sections[3].value[0].description == "First returned."
assert sections[3].value[1].description == "Second returned."


def test_return_doc() -> None:
"""Read documentation for return value."""
with temporary_visited_package(
"package",
modules={"__init__.py": f"{typing_imports}\ndef f() -> Annotated[int, Doc('Hello.')]: ..."},
extensions=Extensions(TypingDocExtension()),
) as package:
assert package["f"].docstring.parsed[1].value[0].description == "Hello."

0 comments on commit b4afabe

Please sign in to comment.