Skip to content

Commit

Permalink
fix: Resolve names in Unpack, instead of naively trying to get them…
Browse files Browse the repository at this point in the history
… from the parent of the function being handled

Issue-11: #11
  • Loading branch information
pawamoy committed Sep 10, 2024
1 parent 0b43e5c commit 5e06b33
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/griffe_typingdoc/_static.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ def _other_parameters_docs(func: Function, **kwargs: Any) -> DocstringSectionPar
"typing.Unpack",
"typing_extensions.Unpack",
}:
typed_dict = annotation.slice.parent.get_member(annotation.slice.name) # type: ignore[attr-defined]
slice_path = annotation.slice.canonical_path
typed_dict = func.modules_collection[slice_path]
params_doc = {
attr.name: {"annotation": attr.annotation, "description": _metadata(attr.annotation).get("doc", "")}
for attr in typed_dict.members.values()
Expand Down
55 changes: 55 additions & 0 deletions tests/test_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,58 @@ def test_return_doc() -> None:
extensions=Extensions(TypingDocExtension()),
) as package:
assert package["f"].docstring.parsed[1].value[0].description == "Hello."


def test_unpacking_typed_dict() -> None:
"""Unpack typed dicts, resolving them to their right location."""
with temporary_visited_package(
"package",
{
"__init__.py": """
from typing import TypedDict
from typing_extensions import Annotated, Doc, Unpack
from package import module
class Options(TypedDict):
foo: Annotated[int, Doc("Foo's description.")]
class A:
def __init__(self, **kwargs: Unpack[Options]) -> None:
'''Init.'''
self.options = kwargs
class B:
def __init__(self, **kwargs: Unpack[module.Options]) -> None:
'''Init.'''
self.options = kwargs
""",
"module.py": """
from typing import TypedDict
from typing_extensions import Annotated, Doc
class Options(TypedDict):
bar: Annotated[str, Doc("Bar's description.")]
""",
},
extensions=Extensions(TypingDocExtension()),
) as package:
sections = package["A.__init__"].docstring.parsed
assert len(sections) == 3
assert sections[0].kind is DocstringSectionKind.text
assert sections[1].kind is DocstringSectionKind.parameters
assert sections[2].kind is DocstringSectionKind.other_parameters
foo = sections[2].value[0]
assert foo.name == "foo"
assert foo.description == "Foo's description."
assert str(foo.annotation).startswith("Annotated[int")

sections = package["B.__init__"].docstring.parsed
assert len(sections) == 3
assert sections[0].kind is DocstringSectionKind.text
assert sections[1].kind is DocstringSectionKind.parameters
assert sections[2].kind is DocstringSectionKind.other_parameters
bar = sections[2].value[0]
assert bar.name == "bar"
assert bar.description == "Bar's description."
assert str(bar.annotation).startswith("Annotated[str")

0 comments on commit 5e06b33

Please sign in to comment.