Skip to content

docstrings

Class info

🛈 DocStrings

Credits to pydantic-ai.

_infer_docstring_style

_infer_docstring_style(doc: str) -> DocstringStyle

Simplistic docstring style inference.

Source code in src/llmling_agent/utils/docstrings.py
61
62
63
64
65
66
67
68
69
70
71
def _infer_docstring_style(doc: str) -> DocstringStyle:
    """Simplistic docstring style inference."""
    for pattern, replacements, style in _docstring_style_patterns:
        matches = (
            re.search(pattern.format(replacement), doc, re.IGNORECASE | re.MULTILINE)
            for replacement in replacements
        )
        if any(matches):
            return style
    # fallback to google style
    return "google"

get_docstring_info

get_docstring_info(
    func: AnyCallable, sig: Signature, *, docstring_format: DocstringFormat = "auto"
) -> tuple[str, dict[str, str]]

Extract the fn description and parameter descriptions from a fn's docstring.

Returns:

Type Description
tuple[str, dict[str, str]]

A tuple of (main function description, parameter descriptions).

Source code in src/llmling_agent/utils/docstrings.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def get_docstring_info(
    func: AnyCallable,
    sig: Signature,
    *,
    docstring_format: DocstringFormat = "auto",
) -> tuple[str, dict[str, str]]:
    """Extract the fn description and parameter descriptions from a fn's docstring.

    Returns:
        A tuple of (main function description, parameter descriptions).
    """
    doc = func.__doc__
    if doc is None:
        return "", {}

    # see https://github.com/mkdocstrings/griffe/issues/293
    parent = cast(GriffeObject, sig)

    docstring_style = (
        _infer_docstring_style(doc) if docstring_format == "auto" else docstring_format
    )
    docstring = Docstring(doc, lineno=1, parser=docstring_style, parent=parent)
    with _disable_griffe_logging():
        sections = docstring.parse()

    params = {}
    if parameters := next(
        (p for p in sections if p.kind == DocstringSectionKind.parameters), None
    ):
        params = {p.name: p.description for p in parameters.value}

    main_desc = ""
    if main := next((p for p in sections if p.kind == DocstringSectionKind.text), None):
        main_desc = main.value

    return main_desc, params