99import ast
1010import types
1111from enum import Enum
12- from typing import TypeVar
13-
14- T = TypeVar ("T" , bound = type )
1512
1613
1714def _parse_docstrings (node : ast .ClassDef ) -> dict [str , str ]:
@@ -35,6 +32,15 @@ def _parse_docstrings(node: ast.ClassDef) -> dict[str, str]:
3532
3633
3734def get_docstrings (cls : type ) -> dict [str , str ]:
35+ """Parse and return a mapping of attribute names to their docstrings for a given class.
36+
37+ Args:
38+ cls: The class to parse attribute docstrings from.
39+
40+ Returns:
41+ A dictionary where keys are attribute names and values are their
42+ corresponding docstrings.
43+ """
3844 if "__attribute_docs__" in cls .__dict__ :
3945 return cls .__attribute_docs__
4046 source = dedent (inspect .getsource (cls ))
@@ -76,7 +82,18 @@ def _attach_enum(cls: type[Enum], comments: dict[str, str]) -> None:
7682 member .__doc__ = comments [name ]
7783
7884
79- def docstrings (cls : T ) -> T :
85+ def docstrings (cls : type ) -> type :
86+ """Decorator that attaches attribute/member docstrings to a class.
87+
88+ If the class is an enum, docstrings are attached via enum members.
89+
90+ If the class is a dataclass, docstrings are attached via field metadata.
91+
92+ Parameters:
93+ cls: The class to process.
94+ Returns:
95+ The same class with attached docstrings.
96+ """
8097 assert inspect .isclass (cls ), "cls must be a class"
8198
8299 # Extract docstrings from the class definition
0 commit comments