|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | + |
| 4 | +import dataclasses |
| 5 | +import inspect |
| 6 | + |
| 7 | +import warnings |
| 8 | +from textwrap import dedent |
| 9 | +import ast |
| 10 | +import types |
| 11 | +from enum import Enum |
| 12 | +from typing import TypeVar |
| 13 | + |
| 14 | +T = TypeVar("T", bound=type) |
| 15 | + |
| 16 | + |
| 17 | +def _parse_docstrings(node: ast.ClassDef) -> dict[str, str]: |
| 18 | + docs: dict[str, str] = {} |
| 19 | + body = node.body |
| 20 | + for index in range(len(body) - 1): |
| 21 | + match body[index]: |
| 22 | + case ast.AnnAssign(target=ast.Name(id=name)): |
| 23 | + pass |
| 24 | + case ast.Assign(targets=[ast.Name(id=name)]): |
| 25 | + pass |
| 26 | + case _: |
| 27 | + continue |
| 28 | + |
| 29 | + match body[index + 1]: |
| 30 | + case ast.Expr(value=ast.Constant(value=doc_str)) if isinstance( |
| 31 | + doc_str, str |
| 32 | + ): |
| 33 | + docs[name] = inspect.cleandoc(doc_str) |
| 34 | + return docs |
| 35 | + |
| 36 | + |
| 37 | +def get_docstrings(cls: type) -> dict[str, str]: |
| 38 | + if "__attribute_docs__" in cls.__dict__: |
| 39 | + return cls.__attribute_docs__ |
| 40 | + source = dedent(inspect.getsource(cls)) |
| 41 | + tree = ast.parse(source) |
| 42 | + node = tree.body[0] |
| 43 | + assert isinstance(node, ast.ClassDef) |
| 44 | + # only process top-level class definition (no NodeVisitor recursion) |
| 45 | + docs = _parse_docstrings(node) |
| 46 | + # IDE style (no MRO resolution) |
| 47 | + cls.__attribute_docs__ = docs |
| 48 | + return docs |
| 49 | + |
| 50 | + |
| 51 | +def _attach_class(cls: type, comments: dict[str, str]) -> None: |
| 52 | + for name, docstring in comments.items(): |
| 53 | + setattr(cls, f"__doc_{name}__", docstring) |
| 54 | + |
| 55 | + |
| 56 | +def _attach_dataclass(cls: type, comments: dict[str, str]) -> None: |
| 57 | + for field in dataclasses.fields(cls): |
| 58 | + field.metadata = types.MappingProxyType( |
| 59 | + {"__doc__": comments.get(field.name), **field.metadata} |
| 60 | + ) |
| 61 | + |
| 62 | + |
| 63 | +def _attach_enum(cls: type[Enum], comments: dict[str, str]) -> None: |
| 64 | + # enum members (canonical) |
| 65 | + for member in cls: |
| 66 | + member.__doc__ = comments.get(member.name) |
| 67 | + |
| 68 | + # enum alias members |
| 69 | + for name, member in cls.__members__.items(): |
| 70 | + canonical_name = member.name |
| 71 | + if name != canonical_name and name in comments: |
| 72 | + warnings.warn( |
| 73 | + f"Enum alias member {cls.__name__}.{name} has docstring that should be documented on {cls.__name__}.{canonical_name}" |
| 74 | + ) |
| 75 | + if canonical_name not in comments: |
| 76 | + member.__doc__ = comments[name] |
| 77 | + |
| 78 | + |
| 79 | +def docstrings(cls: T) -> T: |
| 80 | + assert inspect.isclass(cls), "cls must be a class" |
| 81 | + |
| 82 | + # Extract docstrings from the class definition |
| 83 | + comments = get_docstrings(cls) |
| 84 | + if not comments: |
| 85 | + return cls |
| 86 | + |
| 87 | + _attach_class(cls, comments) |
| 88 | + |
| 89 | + if issubclass(cls, Enum): |
| 90 | + _attach_enum(cls, comments) |
| 91 | + elif dataclasses.is_dataclass(cls): |
| 92 | + _attach_dataclass(cls, comments) |
| 93 | + |
| 94 | + return cls |
0 commit comments