Parse a Griffe docstring into a list of structured section dicts. Each dict has a 'kind' key ('text', 'parameters', 'returns', 'raises', 'admonition') plus kind-specific fields. Returns an empty list when griffe is unavailable or the object has no docstring.
(obj: Any)
| 212 | |
| 213 | |
| 214 | def _docstring_sections(obj: Any) -> list[dict[str, Any]]: |
| 215 | """Parse a Griffe docstring into a list of structured section dicts. |
| 216 | |
| 217 | Each dict has a 'kind' key ('text', 'parameters', 'returns', 'raises', 'admonition') |
| 218 | plus kind-specific fields. Returns an empty list when griffe is unavailable or the |
| 219 | object has no docstring. |
| 220 | """ |
| 221 | try: |
| 222 | from griffe import ( |
| 223 | DocstringSectionAdmonition, |
| 224 | DocstringSectionParameters, |
| 225 | DocstringSectionRaises, |
| 226 | DocstringSectionReturns, |
| 227 | DocstringSectionText, |
| 228 | ) |
| 229 | except Exception: # pragma: no cover |
| 230 | return [] |
| 231 | |
| 232 | docstring = getattr(obj, "docstring", None) |
| 233 | if docstring is None: |
| 234 | return [] |
| 235 | |
| 236 | parameter_lookup = _parameter_lookup(obj) |
| 237 | sections: list[dict[str, Any]] = [] |
| 238 | for section in docstring.parsed: |
| 239 | if isinstance(section, DocstringSectionText): |
| 240 | value = getattr(section, "value", None) |
| 241 | if isinstance(value, str) and value.strip(): |
| 242 | sections.append({"kind": "text", "value": value}) |
| 243 | elif isinstance(section, DocstringSectionParameters): |
| 244 | items = [] |
| 245 | for item in getattr(section, "value", []) or []: |
| 246 | parameter = parameter_lookup.get(getattr(item, "name", "")) |
| 247 | default = getattr(item, "value", None) |
| 248 | if default is None and parameter is not None: |
| 249 | default = getattr(parameter, "default", None) |
| 250 | items.append( |
| 251 | { |
| 252 | "name": getattr(item, "name", "") or "", |
| 253 | "type": _annotation_to_text( |
| 254 | getattr(item, "annotation", None) |
| 255 | or getattr(parameter, "annotation", None) |
| 256 | if parameter |
| 257 | else None |
| 258 | ), |
| 259 | "default": _annotation_to_text(default), |
| 260 | "description": getattr(item, "description", None) or None, |
| 261 | } |
| 262 | ) |
| 263 | if items: |
| 264 | sections.append({"kind": "parameters", "items": items}) |
| 265 | elif isinstance(section, DocstringSectionReturns): |
| 266 | items = [] |
| 267 | for item in getattr(section, "value", []) or []: |
| 268 | items.append( |
| 269 | { |
| 270 | "name": getattr(item, "name", "") or "", |
| 271 | "type": _annotation_to_text(getattr(item, "annotation", None)), |
no test coverage detected