File: griffe_extensions.py

package info (click to toggle)
mkdocstrings-python-handlers 1.16.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,228 kB
  • sloc: python: 3,496; javascript: 84; makefile: 37; sh: 17
file content (46 lines) | stat: -rw-r--r-- 1,440 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Custom extensions for Griffe.

from __future__ import annotations

import ast
from typing import Any

import griffe

_logger = griffe.get_logger("griffe_extensions")


class CustomFields(griffe.Extension):
    """Support our custom dataclass fields."""

    def on_attribute_instance(
        self,
        *,
        attr: griffe.Attribute,
        agent: griffe.Visitor | griffe.Inspector,
        **kwargs: Any,  # noqa: ARG002
    ) -> None:
        """Fetch descriptions from `Field` annotations."""
        if attr.docstring:
            return
        try:
            field: griffe.ExprCall = attr.annotation.slice.elements[1]  # type: ignore[union-attr]
        except AttributeError:
            return

        if field.canonical_path == "mkdocstrings_handlers.python._internal.config._Field":
            description = next(
                attr.value
                for attr in field.arguments
                if isinstance(attr, griffe.ExprKeyword) and attr.name == "description"
            )
            if not isinstance(description, str):
                _logger.warning(f"Field description of {attr.path} is not a static string")
                description = str(description)

            attr.docstring = griffe.Docstring(
                ast.literal_eval(description),
                parent=attr,
                parser=agent.docstring_parser,
                parser_options=agent.docstring_options,
            )