Skip to content

Commit 8b4363b

Browse files
committed
fix: Support Griffe loader options, correctly pass them to the loader
Issue-13: #13
1 parent cfe0f3d commit 8b4363b

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ plugins:
109109
python:
110110
inventories:
111111
- https://docs.python.org/3/objects.inv
112+
- https://mkdocstrings.github.io/griffe/objects.inv
112113
paths: [src]
113114
options:
114115
backlinks: tree

src/griffe2md/_internal/config.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
if TYPE_CHECKING:
1515
from re import Pattern
1616

17+
from griffe import DocstringOptions, LoadableExtensionType
18+
1719
_logger = logging.getLogger(__name__)
1820

1921
CONFIG_FILE_PATHS = (
@@ -59,7 +61,7 @@ class ConfigDict(TypedDict):
5961
annotations_path: Literal["brief", "source", "full"]
6062
"""The verbosity for annotations path: `brief` (recommended), `source` (as written in the source), or `full`."""
6163

62-
docstring_options: dict
64+
docstring_options: DocstringOptions
6365
"""mkdocstring [configuration](https://mkdocstrings.github.io/python/usage/configuration/general/)"""
6466

6567
docstring_section_style: Literal["list", "table"]
@@ -68,6 +70,9 @@ class ConfigDict(TypedDict):
6870
docstring_style: Literal["google", "numpy", "sphinx", "auto"] | None
6971
"""The style in which docstrings are written: `auto`, `google`, `numpy`, `sphinx`, or `None`."""
7072

73+
extensions: list[LoadableExtensionType]
74+
"""A list of Griffe extensions to load."""
75+
7176
filters: list[str] | list[tuple[Pattern[str], bool]]
7277
"""A list of filters.
7378
@@ -76,6 +81,9 @@ class ConfigDict(TypedDict):
7681
to lower members in the hierarchy).
7782
"""
7883

84+
force_inspection: bool
85+
"""Force using introspection on modules even if sources are available."""
86+
7987
group_by_category: bool
8088
"""Group the object's children by categories: attributes, classes, functions, and modules."""
8189

@@ -125,6 +133,9 @@ class ConfigDict(TypedDict):
125133
The modules must be listed as an array of strings.
126134
"""
127135

136+
search_paths: list[str]
137+
"""A list of paths to search packages into."""
138+
128139
separate_signature: bool
129140
"""Whether to put the whole signature in a code block below the heading.
130141
@@ -248,9 +259,12 @@ class ConfigDict(TypedDict):
248259
"preload_modules": None,
249260
"load_external_modules": False,
250261
"allow_inspection": True,
262+
"force_inspection": False,
251263
"summary": True,
252264
"show_docstring_classes": True,
253265
"show_docstring_functions": True,
254266
"show_docstring_modules": True,
267+
"extensions": [],
268+
"search_paths": [],
255269
}
256270
"""Default configuration values."""

src/griffe2md/_internal/main.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
from typing import IO, TYPE_CHECKING, cast
77

88
import mdformat
9-
from griffe import GriffeLoader, Parser
9+
from griffe import GriffeLoader, Parser, load_extensions
1010
from jinja2 import Environment, FileSystemLoader
1111

1212
from griffe2md._internal import rendering
1313
from griffe2md._internal.config import ConfigDict, default_config
1414

1515
if TYPE_CHECKING:
16-
from griffe import Object
16+
from griffe import DocstringOptions, Object
1717

1818

1919
def _output(text: str, to: IO | str | None = None) -> None:
@@ -122,6 +122,12 @@ def render_object_docs(obj: Object, config: ConfigDict | None = None) -> str:
122122
obj: The Griffe object to render docs for.
123123
config: The rendering configuration.
124124
125+
Warning:
126+
When using this function programmatically,
127+
options such as `docstring_style` and `docstring_options` must be passed
128+
to the Griffe loader so that they are correctly set when loading data.
129+
Check [`griffe.GriffeLoader`][] for more information.
130+
125131
Returns:
126132
Markdown.
127133
"""
@@ -143,7 +149,16 @@ def render_package_docs(package: str, config: ConfigDict | None = None) -> str:
143149
"""
144150
config = cast("ConfigDict", {**default_config, **(config or {})})
145151
parser = config["docstring_style"] and Parser(config["docstring_style"])
146-
loader = GriffeLoader(docstring_parser=parser)
152+
parser_options: DocstringOptions = config["docstring_options"]
153+
extensions = load_extensions(*config["extensions"]) if config["extensions"] else None
154+
loader = GriffeLoader(
155+
extensions=extensions,
156+
search_paths=config["search_paths"] + sys.path,
157+
docstring_parser=parser,
158+
docstring_options=parser_options,
159+
allow_inspection=config["allow_inspection"],
160+
force_inspection=config["force_inspection"],
161+
)
147162
module = loader.load(package)
148163
loader.resolve_aliases(external=True)
149164
return render_object_docs(module, config) # type: ignore[arg-type]

0 commit comments

Comments
 (0)