|
| 1 | +"""Helpers to build docstring sections.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING, Any, Iterator |
| 6 | + |
| 7 | +from griffe.docstrings.dataclasses import ( |
| 8 | + DocstringParameter, |
| 9 | + DocstringRaise, |
| 10 | + DocstringReceive, |
| 11 | + DocstringReturn, |
| 12 | + DocstringSectionAdmonition, |
| 13 | + DocstringSectionOtherParameters, |
| 14 | + DocstringSectionParameters, |
| 15 | + DocstringSectionRaises, |
| 16 | + DocstringSectionReceives, |
| 17 | + DocstringSectionReturns, |
| 18 | + DocstringSectionWarns, |
| 19 | + DocstringSectionYields, |
| 20 | + DocstringWarn, |
| 21 | + DocstringYield, |
| 22 | +) |
| 23 | + |
| 24 | +if TYPE_CHECKING: |
| 25 | + from griffe import Function |
| 26 | + from griffe.dataclasses import Parameter |
| 27 | + |
| 28 | + |
| 29 | +def _no_self_params(func: Function) -> list[Parameter]: |
| 30 | + if func.parent and func.parent.is_class and func.parameters[0].name in {"self", "cls"}: |
| 31 | + return list(func.parameters)[1:] |
| 32 | + return list(func.parameters) |
| 33 | + |
| 34 | + |
| 35 | +def _to_parameters_section(params_dict: dict[str, dict[str, Any]], func: Function) -> DocstringSectionParameters: |
| 36 | + return DocstringSectionParameters( |
| 37 | + [ |
| 38 | + DocstringParameter( |
| 39 | + name=param_name, |
| 40 | + description=param_doc["description"], |
| 41 | + annotation=param_doc["annotation"], |
| 42 | + value=func.parameters[param_name].default, # type: ignore[arg-type] |
| 43 | + ) |
| 44 | + for param_name, param_doc in params_dict.items() |
| 45 | + ], |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def _to_other_parameters_section(params_dict: dict[str, dict[str, Any]]) -> DocstringSectionOtherParameters: |
| 50 | + return DocstringSectionOtherParameters( |
| 51 | + [ |
| 52 | + DocstringParameter( |
| 53 | + name=param_name, |
| 54 | + description=param_doc["description"], |
| 55 | + annotation=param_doc["annotation"], |
| 56 | + ) |
| 57 | + for param_name, param_doc in params_dict.items() |
| 58 | + ], |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def _to_yields_section(yield_data: Iterator[dict[str, Any]]) -> DocstringSectionYields: |
| 63 | + return DocstringSectionYields( |
| 64 | + [ |
| 65 | + DocstringYield( |
| 66 | + name=yield_dict.get("name", ""), |
| 67 | + description=yield_dict.get("doc", ""), |
| 68 | + annotation=yield_dict["annotation"], |
| 69 | + ) |
| 70 | + for yield_dict in yield_data |
| 71 | + ], |
| 72 | + ) |
| 73 | + |
| 74 | + |
| 75 | +def _to_receives_section(receive_data: Iterator[dict[str, Any]]) -> DocstringSectionReceives: |
| 76 | + return DocstringSectionReceives( |
| 77 | + [ |
| 78 | + DocstringReceive( |
| 79 | + name=receive_dict.get("name", ""), |
| 80 | + description=receive_dict.get("doc", ""), |
| 81 | + annotation=receive_dict["annotation"], |
| 82 | + ) |
| 83 | + for receive_dict in receive_data |
| 84 | + ], |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +def _to_returns_section(return_data: Iterator[dict[str, Any]]) -> DocstringSectionReturns: |
| 89 | + return DocstringSectionReturns( |
| 90 | + [ |
| 91 | + DocstringReturn( |
| 92 | + name=return_dict.get("name", ""), |
| 93 | + description=return_dict.get("doc", ""), |
| 94 | + annotation=return_dict["annotation"], |
| 95 | + ) |
| 96 | + for return_dict in return_data |
| 97 | + ], |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def _to_warns_section(warn_data: Iterator[dict[str, Any]]) -> DocstringSectionWarns: |
| 102 | + return DocstringSectionWarns( |
| 103 | + [ |
| 104 | + DocstringWarn( |
| 105 | + annotation=warn_dict["annotation"], |
| 106 | + description=warn_dict.get("description", ""), |
| 107 | + ) |
| 108 | + for warn_dict in warn_data |
| 109 | + ], |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +def _to_raises_section(raise_data: Iterator[dict[str, Any]]) -> DocstringSectionRaises: |
| 114 | + return DocstringSectionRaises( |
| 115 | + [ |
| 116 | + DocstringRaise( |
| 117 | + annotation=raise_dict["annotation"], |
| 118 | + description=raise_dict.get("description", ""), |
| 119 | + ) |
| 120 | + for raise_dict in raise_data |
| 121 | + ], |
| 122 | + ) |
| 123 | + |
| 124 | + |
| 125 | +def _to_deprecated_section(deprecation_data: dict[str, Any]) -> DocstringSectionAdmonition: |
| 126 | + description = deprecation_data["description"] |
| 127 | + description_lines = deprecation_data["description"].split("\n") |
| 128 | + if len(description_lines) > 1: |
| 129 | + title = description_lines[0].strip() |
| 130 | + description = "\n".join(description_lines[1:]).strip() |
| 131 | + else: |
| 132 | + title = description |
| 133 | + description = "" |
| 134 | + return DocstringSectionAdmonition(kind="danger", title=title, text=description) |
0 commit comments