Skip to content

Commit c7a61c6

Browse files
authored
refactor: Refactor implementation with latest version in typing_extensions, Doc(), and de-indent
PR #2: #2
1 parent cc15edc commit c7a61c6

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ tests = [
8484
"pytest-cov>=3.0",
8585
"pytest-randomly>=3.10",
8686
"pytest-xdist>=2.4",
87-
"typing-extensions @ git+https://github.com/tiangolo/typing_extensions@typing-doc",
87+
"typing-extensions>=4.8.0rc1",
8888
]
8989
typing = [
9090
"mypy>=0.910",

src/griffe_typingdoc/extension.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import inspect
56
from ast import literal_eval
67
from collections import defaultdict
78
from typing import TYPE_CHECKING, Any, Sequence
@@ -14,21 +15,26 @@
1415
if TYPE_CHECKING:
1516
import ast
1617

17-
from typing_extensions import Annotated, doc # type: ignore[attr-defined]
18+
from typing_extensions import Annotated, Doc # type: ignore[attr-defined]
1819

1920

2021
class TypingDocExtension(Extension):
21-
"""Griffe extension that reads documentation from `typing.doc`."""
22+
"""Griffe extension that reads documentation from `typing.Doc`."""
2223

2324
def on_function_instance(
2425
self,
2526
node: Annotated[
2627
ast.AST | ObjectNode,
27-
doc("The object/AST node describing the function or its definition."),
28+
Doc("The object/AST node describing the function or its definition."),
2829
],
2930
func: Annotated[
3031
Function,
31-
doc("The Griffe function just instantiated."),
32+
Doc(
33+
# Multiline docstring to test de-indentation.
34+
"""
35+
The Griffe function just instantiated.
36+
""",
37+
),
3238
],
3339
) -> None:
3440
"""Post-process Griffe functions to add a parameters section."""
@@ -56,10 +62,10 @@ def on_function_instance(
5662
doc = None
5763
for data in metadata:
5864
if isinstance(data, ExprCall) and data.function.canonical_path in {
59-
"typing.doc",
60-
"typing_extensions.doc",
65+
"typing.Doc",
66+
"typing_extensions.Doc",
6167
}:
62-
doc = literal_eval(str(data.arguments[0]))
68+
doc = inspect.cleandoc(literal_eval(str(data.arguments[0])))
6369
params_doc[parameter.name]["annotation"] = annotation
6470
if doc:
6571
params_doc[parameter.name]["description"] = doc
@@ -72,7 +78,7 @@ def on_function_instance(
7278
[
7379
DocstringParameter(
7480
name=param_name,
75-
description=param_doc["description"],
81+
description=param_doc.get("description") or "",
7682
annotation=param_doc["annotation"],
7783
value=func.parameters[param_name].default, # type: ignore[arg-type]
7884
)

tests/test_extension.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ def test_extension() -> None:
1414
sections = typingdoc["extension.TypingDocExtension.on_function_instance"].docstring.parsed
1515
assert len(sections) == 2
1616
assert sections[1].kind is DocstringSectionKind.parameters
17+
assert sections[1].value[1].description == "The Griffe function just instantiated."

0 commit comments

Comments
 (0)