Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Forward class attribute docstrings to instances. #135

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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