Skip to content

Commit

Permalink
fix: Forward class attribute docstrings to instances
Browse files Browse the repository at this point in the history
Issue #128: #128
PR #135: #135
  • Loading branch information
rodrigogiraoserrao committed Mar 3, 2023
1 parent 67ebd71 commit 7bf4952
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/griffe/agents/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,12 @@ def handle_attribute(
if isinstance(node.parent, (ast.If, ast.ExceptHandler)): # type: ignore[union-attr]
continue # prefer "no-exception" case

parent.members[name].labels |= labels

# forward previous docstring instead of erasing it
if parent.members[name].docstring and not docstring:
docstring = parent.members[name].docstring

attribute = Attribute(
name=name,
value=value,
Expand Down
18 changes: 18 additions & 0 deletions tests/test_visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,3 +279,21 @@ def cached_prop(self) -> int:
assert "property" in module["C.prop"].labels
assert module["C.cached_prop"].is_attribute
assert "cached" in module["C.cached_prop"].labels


def test_forward_docstrings() -> None:
"""Assert docstrings of class attributes are forwarded to instance assignments.
This is a regression test for https://github.com/mkdocstrings/griffe/issues/128.
"""
with temporary_visited_module(
'''
class C:
attr: int
"""This is a non-empty docstring."""
def __init__(self, attr: int) -> None:
self.attr = attr
'''
) as module:
assert module["C.attr"].docstring

0 comments on commit 7bf4952

Please sign in to comment.