Skip to content

Commit 02d8cb6

Browse files
committed
fix: Do not always add docstrings to attributes
1 parent ef3a834 commit 02d8cb6

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

src/griffe_typingdoc/_dynamic.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def _doc(name: str, hints: dict[str, Any]) -> str | None:
2929
return None
3030

3131

32-
def _attribute_docs(node: ObjectNode, attr: Attribute) -> str | None:
33-
return _doc(attr.name, _hints(node))
32+
def _attribute_docs(node: ObjectNode, attr: Attribute) -> str:
33+
return _doc(attr.name, _hints(node)) or ""
3434

3535

3636
def _parameters_docs(node: ObjectNode, func: Function) -> DocstringSectionParameters | None:

src/griffe_typingdoc/_extension.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,18 @@ def on_attribute_instance(
3333
"""Post-process Griffe attributes to create their docstring."""
3434
module = _dynamic if isinstance(node, ObjectNode) else _static
3535

36+
new_sections = (
37+
docstring := module._attribute_docs(node, attr),
38+
deprecated_section := module._deprecated_docs(node, attr),
39+
raises_section := module._raises_docs(node, attr),
40+
warns_section := module._warns_docs(node, attr),
41+
)
42+
43+
if not any(new_sections):
44+
return
45+
3646
if not attr.docstring:
37-
attr.docstring = Docstring(module._attribute_docs(node, attr), parent=attr)
47+
attr.docstring = Docstring(docstring, parent=attr)
3848

3949
sections = attr.docstring.parsed
4050

src/griffe_typingdoc/_static.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def _set_metadata(metadata: dict[str, Any], data: ExprCall) -> None:
8383

8484
def _metadata(annotation: str | Expr | None) -> dict[str, Any]:
8585
metadata: dict[str, Any] = {"raises": [], "warns": []}
86-
if isinstance(annotation, ExprSubscript) and annotation.left.canonical_path in {
86+
if isinstance(annotation, ExprSubscript) and annotation.canonical_path in {
8787
"typing.Annotated",
8888
"typing_extensions.Annotated",
8989
}:

0 commit comments

Comments
 (0)