Skip to content

Commit 950f90e

Browse files
committed
docs: Sort options, document extra and extensions, add warnings on no-effect local options
1 parent 35b48f7 commit 950f90e

File tree

2 files changed

+198
-112
lines changed

2 files changed

+198
-112
lines changed

docs/usage/configuration/docstrings.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ plugins:
2929
docstring_style: numpy
3030
```
3131
32+
WARNING: **The style is applied to the specified object only, not its members.** Local `docstring_style` options (in `:::` instructions) will only be applied to the specified object, and not its members. Instead of changing the style when rendering, we strongly recommend to *set the right style as early as possible*, for example by using the [auto-style](https://mkdocstrings.github.io/griffe/reference/docstrings/#auto-style) (sponsors only), or with a custom Griffe extension
33+
34+
3235
/// admonition | Preview
3336
type: preview
3437

docs/usage/configuration/general.md

Lines changed: 195 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and sometimes the collected data is inaccurate
1818
(depending on the tool that was used to compile the module)
1919
or too low-level/technical for API documentation.
2020

21+
WARNING: **Packages are loaded only once.** When mkdocstrings-python collects data from a Python package (thanks to [Griffe](https://mkdocstrings.github.io/griffe/)), it collects *the entire package* and *caches it*. Next time an object from the same package is rendered, the package is retrieved from the cache and not collected again. The `allow_inspection` option will therefore only have an effect the first time a package is collected, and will do nothing for objects rendered afterwards.
22+
2123
```yaml title="in mkdocs.yml (global configuration)"
2224
plugins:
2325
- mkdocstrings:
@@ -55,6 +57,199 @@ plugins:
5557
////
5658
///
5759
60+
[](){#option-extensions}
61+
## `extensions`
62+
63+
- **:octicons-package-24: Type <code><autoref identifier="list" optional>list</autoref>[<autoref identifier="str" optional>str</autoref> | <autoref identifier="dict" optional>dict</autoref>[<autoref identifier="str" optional>str</autoref>, <autoref identifier="dict" optional>dict</autoref>[<autoref identifier="str" optional>str</autoref>, <autoref identifier="typing.Any" optional>Any</autoref>]]]</code> :material-equal: `[]`{ title="default value" }**
64+
<!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->
65+
66+
The `extensions` option lets you enable [Griffe extensions](https://mkdocstrings.github.io/griffe/extensions/), which enhance or modify the data collected from Python sources (or compiled modules).
67+
68+
Elements in the list can be strings or dictionaries.
69+
70+
Strings denote the path to an extension module, like `griffe_typingdoc`, or to an extension class directly, like `griffe_typingdoc.TypingDocExtension`. When using a module path, all extensions within that module will be loaded and enabled. Strings can also be the path to a Python module, and a class name separated with `:`, like `scripts/griffe_extensions.py` or `scripts/griffe_extensions.py:MyExtension`.
71+
72+
Dictionaries have a single key, which is the module/class path (as a dot-separated qualifier or file path and colon-separated class name, like above), and its value is another dictionary specifying options that will be passed when to class constructors when instantiating extensions.
73+
74+
WARNING: **Packages are loaded only once.** When mkdocstrings-python collects data from a Python package (thanks to [Griffe](https://mkdocstrings.github.io/griffe/)), it collects *the entire package* and *caches it*. Next time an object from the same package is rendered, the package is retrieved from the cache and not collected again. Only the extensions specified the first time the package is loaded will be used. You cannot use a different set of extensions for specific objects rendered afterwards, and you cannot deactivate extensions for objects rendered afterwards either.
75+
76+
```yaml title="in mkdocs.yml (global configuration)"
77+
plugins:
78+
- mkdocstrings:
79+
handlers:
80+
python:
81+
options:
82+
extensions:
83+
- griffe_sphinx
84+
- griffe_pydantic: {schema: true}
85+
- scripts/exts.py:DynamicDocstrings:
86+
paths: [mypkg.mymod.myobj]
87+
```
88+
89+
```md title="or in docs/some_page.md (local configuration)"
90+
::: your_package.your_module.your_func
91+
options:
92+
extensions:
93+
- griffe_typingdoc
94+
```
95+
96+
[](){#option-extra}
97+
## `extra`
98+
99+
- **:octicons-package-24: Type [`dict`][] :material-equal: `{}`{ title="default value" }**
100+
<!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->
101+
102+
The `extra` option lets you inject additional variables into the Jinja context used when rendering templates. You can then use this extra context in your [overridden templates][templates].
103+
104+
Local `extra` options will be merged into the global `extra` option:
105+
106+
```yaml title="in mkdocs.yml (global configuration)"
107+
plugins:
108+
- mkdocstrings:
109+
handlers:
110+
python:
111+
options:
112+
extra:
113+
hello: world
114+
```
115+
116+
```md title="in docs/some_page.md (local configuration)"
117+
::: your_package.your_module.your_func
118+
options:
119+
extra:
120+
foo: bar
121+
```
122+
123+
...will inject both `hello` and `foo` into the Jinja context when rendering `your_package.your_module.your_func`.
124+
125+
> WARNING: Previously, extra options were supported directly under the `options` key.
126+
>
127+
> ```yaml
128+
> plugins:
129+
> - mkdocstrings:
130+
> handlers:
131+
> python:
132+
> options:
133+
> hello: world
134+
> ```
135+
>
136+
> Now that we introduced optional validation of options and automatic JSON schema generation thanks to Pydantic, we require extra options to be put under `options.extra`. Extra options directly under `options` are still supported, but deprecated, and will emit deprecation warnings. Support will be removed in a future version of mkdocstrings-python.
137+
138+
[](){#option-find_stubs_package}
139+
## `find_stubs_package`
140+
141+
- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
142+
<!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->
143+
144+
When looking for documentation specified in [autodoc instructions][autodoc syntax] (`::: identifier`), also look for
145+
the stubs package as defined in [PEP 561](https://peps.python.org/pep-0561/) if it exists. This is useful when
146+
most of your documentation is separately provided by such a package and not inline in your main package.
147+
148+
WARNING: **Packages are loaded only once.** When mkdocstrings-python collects data from a Python package (thanks to [Griffe](https://mkdocstrings.github.io/griffe/)), it collects *the entire package* and *caches it*. Next time an object from the same package is rendered, the package is retrieved from the cache and not collected again. The `find_stubs_package` option will therefore only have an effect the first time a package is collected, and will do nothing for objects rendered afterwards.
149+
150+
```yaml title="in mkdocs.yml (global configuration)"
151+
plugins:
152+
- mkdocstrings:
153+
handlers:
154+
python:
155+
options:
156+
find_stubs_package: true
157+
```
158+
159+
```md title="or in docs/some_page.md (local configuration)"
160+
::: your_package.your_module.your_func
161+
options:
162+
find_stubs_package: true
163+
```
164+
165+
```python title="your_package/your_module.py"
166+
167+
def your_func(a, b):
168+
# Function code
169+
...
170+
171+
# rest of your code
172+
```
173+
174+
```python title="your_package-stubs/your_module.pyi"
175+
176+
def your_func(a: int, b: str):
177+
"""
178+
<Function docstring>
179+
"""
180+
...
181+
182+
# rest of your code
183+
```
184+
185+
/// admonition | Preview
186+
type: preview
187+
188+
//// tab | With find_stubs_package
189+
<h2><code>your_func</code></h2>
190+
<p>Function docstring</p>
191+
////
192+
193+
//// tab | Without find_stubs_package
194+
<h2><code>your_func</code></h2>
195+
////
196+
///
197+
198+
[](){#option-preload_modules}
199+
## `preload_modules`
200+
201+
- **:octicons-package-24: Type <code><autoref identifier="list" optional>list</autoref>[<autoref identifier="str" optional>str</autoref>] | None</code> :material-equal: `None`{ title="default value" }**
202+
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
203+
204+
Pre-load modules that are not specified directly in [autodoc instructions][autodoc syntax] (`::: identifier`).
205+
It is useful when you want to render documentation for a particular member of an object,
206+
and this member is imported from another package than its parent.
207+
208+
For an imported member to be rendered,
209+
you need to add it to the [`__all__`][__all__] attribute of the importing module.
210+
The package from which the imported object originates must be accessible to the handler
211+
(see [Finding modules](../index.md#finding-modules)).
212+
213+
```yaml title="in mkdocs.yml (global configuration)"
214+
plugins:
215+
- mkdocstrings:
216+
handlers:
217+
python:
218+
options:
219+
preload_modules:
220+
- their_package
221+
```
222+
223+
```md title="or in docs/some_page.md (local configuration)"
224+
::: your_package.your_module
225+
options:
226+
preload_modules:
227+
- their_package
228+
```
229+
230+
```python title="your_package/your_module.py"
231+
from their_package.their_module import their_object
232+
233+
__all__ = ["their_object"]
234+
235+
# rest of your code
236+
```
237+
238+
/// admonition | Preview
239+
type: preview
240+
241+
//// tab | With preloaded modules
242+
<h2><code>your_module</code></h2>
243+
<p>Docstring of your module.</p>
244+
<h3><code>their_object</code></h3>
245+
<p>Docstring of their object.</p>
246+
////
247+
248+
//// tab | Without preloaded modules
249+
<h2><code>your_module</code></h2>
250+
<p>Docstring of your module.</p>
251+
////
252+
///
58253
## `show_bases`
59254

60255
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
@@ -221,115 +416,3 @@ def some_function():
221416
<p>Docstring of the function.</p>
222417
////
223418
///
224-
225-
## `preload_modules`
226-
227-
- **:octicons-package-24: Type <code><autoref identifier="list" optional>list</autoref>[<autoref identifier="str" optional>str</autoref>] | None</code> :material-equal: `None`{ title="default value" }**
228-
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
229-
230-
Pre-load modules that are not specified directly in [autodoc instructions][autodoc syntax] (`::: identifier`).
231-
It is useful when you want to render documentation for a particular member of an object,
232-
and this member is imported from another package than its parent.
233-
234-
For an imported member to be rendered,
235-
you need to add it to the [`__all__`][__all__] attribute of the importing module.
236-
The package from which the imported object originates must be accessible to the handler
237-
(see [Finding modules](../index.md#finding-modules)).
238-
239-
```yaml title="in mkdocs.yml (global configuration)"
240-
plugins:
241-
- mkdocstrings:
242-
handlers:
243-
python:
244-
options:
245-
preload_modules:
246-
- their_package
247-
```
248-
249-
```md title="or in docs/some_page.md (local configuration)"
250-
::: your_package.your_module
251-
options:
252-
preload_modules:
253-
- their_package
254-
```
255-
256-
```python title="your_package/your_module.py"
257-
from their_package.their_module import their_object
258-
259-
__all__ = ["their_object"]
260-
261-
# rest of your code
262-
```
263-
264-
/// admonition | Preview
265-
type: preview
266-
267-
//// tab | With preloaded modules
268-
<h2><code>your_module</code></h2>
269-
<p>Docstring of your module.</p>
270-
<h3><code>their_object</code></h3>
271-
<p>Docstring of their object.</p>
272-
////
273-
274-
//// tab | Without preloaded modules
275-
<h2><code>your_module</code></h2>
276-
<p>Docstring of your module.</p>
277-
////
278-
///
279-
280-
## `find_stubs_package`
281-
282-
- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
283-
<!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->
284-
285-
When looking for documentation specified in [autodoc instructions][autodoc syntax] (`::: identifier`), also look for
286-
the stubs package as defined in [PEP 561](https://peps.python.org/pep-0561/) if it exists. This is useful when
287-
most of your documentation is separately provided by such a package and not inline in your main package.
288-
289-
```yaml title="in mkdocs.yml (global configuration)"
290-
plugins:
291-
- mkdocstrings:
292-
handlers:
293-
python:
294-
options:
295-
find_stubs_package: true
296-
```
297-
298-
```md title="or in docs/some_page.md (local configuration)"
299-
::: your_package.your_module.your_func
300-
options:
301-
find_stubs_package: true
302-
```
303-
304-
```python title="your_package/your_module.py"
305-
306-
def your_func(a, b):
307-
# Function code
308-
...
309-
310-
# rest of your code
311-
```
312-
313-
```python title="your_package-stubs/your_module.pyi"
314-
315-
def your_func(a: int, b: str):
316-
"""
317-
<Function docstring>
318-
"""
319-
...
320-
321-
# rest of your code
322-
```
323-
324-
/// admonition | Preview
325-
type: preview
326-
327-
//// tab | With find_stubs_package
328-
<h2><code>your_func</code></h2>
329-
<p>Function docstring</p>
330-
////
331-
332-
//// tab | Without find_stubs_package
333-
<h2><code>your_func</code></h2>
334-
////
335-
///

0 commit comments

Comments
 (0)