|
| 1 | +"""This module defines the Griffe TypingDoc extension.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import ast |
| 6 | +from collections import defaultdict |
| 7 | +from typing import Annotated, Any |
| 8 | + |
| 9 | +from griffe.agents.extensions import VisitorExtension, When |
| 10 | +from griffe.agents.nodes import safe_get_annotation |
| 11 | +from griffe.dataclasses import Function |
| 12 | +from griffe.docstrings.dataclasses import DocstringParameter, DocstringSectionParameters |
| 13 | + |
| 14 | +from griffe_typingdoc.typing_doc import __typing_doc__ |
| 15 | + |
| 16 | + |
| 17 | +@__typing_doc__(description="Griffe extension parsing the `typing.doc` decorator.") |
| 18 | +class TypingDocExtension(VisitorExtension): |
| 19 | + """Griffe extension parsing the `typing.doc` decorator.""" |
| 20 | + |
| 21 | + when = When.after_all |
| 22 | + |
| 23 | + @__typing_doc__(description="Visit a function definition.") |
| 24 | + def visit_functiondef( |
| 25 | + self, |
| 26 | + node: Annotated[ |
| 27 | + ast.FunctionDef, |
| 28 | + __typing_doc__( |
| 29 | + description="The AST node describing the function definition.", |
| 30 | + ), |
| 31 | + ], |
| 32 | + ) -> None: |
| 33 | + """Visit a function definition. |
| 34 | +
|
| 35 | + This function takes a function definition node and visits its contents, |
| 36 | + particularly its decorators, to build up the documentation metadata. |
| 37 | + """ |
| 38 | + function: Function = self.visitor.current.members[node.name] # type: ignore[assignment] |
| 39 | + |
| 40 | + func_doc = {} |
| 41 | + for decorator_node in node.decorator_list: |
| 42 | + if isinstance(decorator_node, ast.Call): |
| 43 | + if decorator_node.func.id == "__typing_doc__": # type: ignore[attr-defined] |
| 44 | + func_doc.update({kw.arg: kw.value.value for kw in decorator_node.keywords}) # type: ignore[attr-defined] |
| 45 | + |
| 46 | + params_doc: dict[str, dict[str, Any]] = defaultdict(dict) |
| 47 | + for arg in node.args.args: |
| 48 | + if isinstance(arg.annotation, ast.Subscript): |
| 49 | + if arg.annotation.value.id == "Annotated": # type: ignore[attr-defined] |
| 50 | + param_name = arg.arg |
| 51 | + params_doc[param_name]["annotation"] = safe_get_annotation( |
| 52 | + arg.annotation.slice.elts[0], # type: ignore[attr-defined] |
| 53 | + function.parent, # type: ignore[arg-type] |
| 54 | + ) |
| 55 | + doc = arg.annotation.slice.elts[1] # type: ignore[attr-defined] |
| 56 | + if isinstance(doc, ast.Call): |
| 57 | + if doc.func.id == "__typing_doc__": # type: ignore[attr-defined] |
| 58 | + params_doc[param_name].update({kw.arg: kw.value.value for kw in doc.keywords}) # type: ignore[attr-defined,misc] |
| 59 | + |
| 60 | + if func_doc or params_doc: |
| 61 | + if function.docstring: |
| 62 | + sections = function.docstring.parsed |
| 63 | + if params_doc: |
| 64 | + docstring_params = [] |
| 65 | + for param_name, param_doc in params_doc.items(): # noqa: WPS440 |
| 66 | + docstring_params.append( |
| 67 | + DocstringParameter( |
| 68 | + name=param_name, |
| 69 | + description=param_doc["description"], |
| 70 | + annotation=param_doc["annotation"], |
| 71 | + value=function.parameters[param_name].default, |
| 72 | + ), |
| 73 | + ) |
| 74 | + sections.append(DocstringSectionParameters(docstring_params)) |
0 commit comments