Coverage for packages / griffelib / src / griffe / _internal / agents / nodes / docstrings.py: 100.00%
11 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-11 11:48 +0100
« prev ^ index » next coverage.py v7.13.4, created at 2026-02-11 11:48 +0100
1# This module contains utilities for extracting docstrings from nodes.
3from __future__ import annotations
5import ast
8def get_docstring(
9 node: ast.AST,
10 *,
11 strict: bool = False,
12) -> tuple[str | None, int | None, int | None]:
13 """Extract a docstring.
15 Parameters:
16 node: The node to extract the docstring from.
17 strict: Whether to skip searching the body (functions).
19 Returns:
20 A tuple with the value and line numbers of the docstring.
21 """
22 # TODO: Possible optimization using a type map.
23 if isinstance(node, ast.Expr):
24 doc = node.value
25 elif not strict and node.body and isinstance(node.body, list) and isinstance(node.body[0], ast.Expr): # ty:ignore[unresolved-attribute]
26 doc = node.body[0].value # ty:ignore[unresolved-attribute]
27 else:
28 return None, None, None
29 if isinstance(doc, ast.Constant) and isinstance(doc.value, str):
30 return doc.value, doc.lineno, doc.end_lineno
31 return None, None, None