Debian Codegraph @

docs/usage/index.md

raw
1 # Usage
2
3 TIP: **This is the documentation for the NEW Python handler.**
4 To read the documentation for the LEGACY handler,
5 go to the [legacy handler documentation](https://mkdocstrings.github.io/python-legacy).
6
7 ## Installation
8
9 You can install this handler as a *mkdocstrings* extra:
10
11 ```toml title="pyproject.toml"
12 # PEP 621 dependencies declaration
13 # adapt to your dependencies manager
14 [project]
15 dependencies = [
16 "mkdocstrings[python]>=0.18",
17 ]
18 ```
19
20 You can also explicitly depend on the handler:
21
22 ```toml title="pyproject.toml"
23 # PEP 621 dependencies declaration
24 # adapt to your dependencies manager
25 [project]
26 dependencies = [
27 "mkdocstrings-python",
28 ]
29 ```
30
31 The Python handler is the default *mkdocstrings* handler.
32 You can change the default handler,
33 or explicitely set the Python handler as default by defining the `default_handler`
34 configuration option of `mkdocstrings` in `mkdocs.yml`:
35
36 ```yaml title="mkdocs.yml"
37 plugins:
38 - mkdocstrings:
39 default_handler: python
40 ```
41
42 ## Injecting documentation
43
44 With the Python handler installed and configured as default handler,
45 you can inject documentation for a module, class, function, or any other Python object
46 with *mkdocstrings*' [autodoc syntax], in your Markdown pages:
47
48 ```md
49 ::: path.to.object
50 ```
51
52 If another handler was defined as default handler,
53 you can explicitely ask for the Python handler to be used when injecting documentation
54 with the `handler` option:
55
56 ```md
57 ::: path.to.object
58 handler: python
59 ```
60
61 ## Configuration
62
63 When installed, the Python handler becomes the default *mkdocstrings* handler.
64 You can configure it in `mkdocs.yml`:
65
66 ```yaml title="mkdocs.yml"
67 plugins:
68 - mkdocstrings:
69 handlers:
70 python:
71 ... # the Python handler configuration
72 ```
73
74 ### Global-only options
75
76 Some options are **global only**, and go directly under the handler's name.
77
78 [](){#setting-inventories}
79 #### `inventories`
80
81 This option is used to load Sphinx-compatible objects inventories from other
82 documentation sites. For example, you can load the standard library
83 objects inventory like this:
84
85 ```yaml title="mkdocs.yml"
86 plugins:
87 - mkdocstrings:
88 handlers:
89 python:
90 inventories:
91 - https://docs.python.org/3/objects.inv
92 ```
93
94 When loading an inventory, you enable automatic cross-references
95 to other documentation sites like the standard library docs
96 or any third-party package docs. Typically, you want to load
97 the inventories of your project's dependencies, at least those
98 that are used in the public API.
99
100 See [*mkdocstrings*' documentation on inventories][inventories]
101 for more details.
102
103 [inventories]: https://mkdocstrings.github.io/usage/#cross-references-to-other-projects-inventories
104
105 Additionally, the Python handler accepts a `domains` option in the inventory options,
106 which allows to select the inventory domains to load.
107 By default the Python handler only selects the `py` domain (for Python objects).
108 You might find useful to also enable the [`std` domain][std domain]:
109
110 [std domain]: https://www.sphinx-doc.org/en/master/usage/restructuredtext/domains.html#the-standard-domain
111
112 ```yaml title="mkdocs.yml"
113 plugins:
114 - mkdocstrings:
115 handlers:
116 python:
117 inventories:
118 - url: https://docs.python-requests.org/en/master/objects.inv
119 domains: [std, py]
120 ```
121
122 [](){#setting-load_external_modules}
123 #### `load_external_modules`
124
125 This option allows resolving aliases (imports) to any external module.
126 Modules are considered external when they are not part
127 of the package your are injecting documentation for.
128 Setting this option to `True` will tell the handler to resolve aliases recursively
129 when they are made public through the [`__all__`][__all__] variable.
130 By default, the handler will only resolve aliases when they point at a private sibling
131 of the source package, for example aliases going from `ast` to `_ast`.
132 Set `load_external_modules` to `False` to prevent even that.
133
134 WARNING: **Use with caution**
135 This can load a *lot* of modules through [Griffe],
136 slowing down your build or triggering errors that Griffe does not yet handle.
137 **We recommend using the [`preload_modules`][] option instead**,
138 which acts as an include-list rather than as include-all.
139
140 Example:
141
142 ```yaml title="mkdocs.yml"
143 plugins:
144 - mkdocstrings:
145 handlers:
146 python:
147 load_external_modules: true
148 ```
149
150 [__all__]: https://docs.python.org/3/tutorial/modules.html#importing-from-a-package
151
152 [](){#setting-locale}
153 #### ~~`locale`~~
154
155 **Deprecated.** Use mkdocstrings' own `locale` setting.
156
157 ~~The locale to use when translating template strings.~~
158
159 [](){#setting-paths}
160 #### `paths`
161
162 This option is used to provide filesystem paths in which to search for Python modules.
163 Non-absolute paths are computed as relative to MkDocs configuration file. Example:
164
165 ```yaml title="mkdocs.yml"
166 plugins:
167 - mkdocstrings:
168 handlers:
169 python:
170 paths: [src] # search packages in the src folder
171 ```
172
173 More details at [Finding modules](#finding-modules).
174
175 [](){#setting-options}
176 ### Global/local options
177
178 The other options can be used both globally *and* locally, under the `options` key.
179 For example, globally:
180
181 ```yaml title="mkdocs.yml"
182 plugins:
183 - mkdocstrings:
184 handlers:
185 python:
186 options:
187 do_something: true
188 ```
189
190 ...and locally, overriding the global configuration:
191
192 ```md title="docs/some_page.md"
193 ::: package.module.class
194 options:
195 do_something: false
196 ```
197
198 These options affect how the documentation is collected from sources and rendered.
199 See the following tables summarizing the options, and get more details for each option
200 in the following pages:
201
202 - [General options](configuration/general.md): various options that do not fit in the other categories
203 - [Headings options](configuration/headings.md): options related to headings and the table of contents
204 (or sidebar, depending on the theme used)
205 - [Members options](configuration/members.md): options related to filtering or ordering members
206 in the generated documentation
207 - [Docstrings options](configuration/docstrings.md): options related to docstrings (parsing and rendering)
208 - [Signature options](configuration/signatures.md): options related to signatures and type annotations
209
210 ## Finding modules
211
212 There are multiple ways to tell the handler where to find your packages/modules.
213
214 **The recommended method is to use the `paths` option, as it's the only one
215 that works with the `-f` option of MkDocs, allowing to build the documentation
216 from any location on the file system.** Indeed, the paths provided with the
217 `paths` option are computed as relative to the configuration file (mkdocs.yml),
218 so that the current working directory has no impact on the build process:
219 *you can build the docs from any location on your filesystem*.
220
221 ### Using the `paths` option
222
223 TIP: **This is the recommended method.**
224
225 1. mkdocs.yml in root, package in root
226 ```tree
227 root/
228 mkdocs.yml
229 package/
230 ```
231
232 ```yaml title="mkdocs.yml"
233 plugins:
234 - mkdocstrings:
235 handlers:
236 python:
237 paths: [.] # actually not needed, default
238 ```
239
240 1. mkdocs.yml in root, package in subfolder
241 ```tree
242 root/
243 mkdocs.yml
244 src/
245 package/
246 ```
247
248 ```yaml title="mkdocs.yml"
249 plugins:
250 - mkdocstrings:
251 handlers:
252 python:
253 paths: [src]
254 ```
255
256 1. mkdocs.yml in subfolder, package in root
257 ```tree
258 root/
259 docs/
260 mkdocs.yml
261 package/
262 ```
263
264 ```yaml title="mkdocs.yml"
265 plugins:
266 - mkdocstrings:
267 handlers:
268 python:
269 paths: [..]
270 ```
271
272 1. mkdocs.yml in subfolder, package in subfolder
273 ```tree
274 root/
275 docs/
276 mkdocs.yml
277 src/
278 package/
279 ```
280
281 ```yaml title="mkdocs.yml"
282 plugins:
283 - mkdocstrings:
284 handlers:
285 python:
286 paths: [../src]
287 ```
288
289 Except for case 1, which is supported by default, **we strongly recommend
290 setting the path to your packages using this option, even if it works without it**
291 (for example because your project manager automatically adds `src` to PYTHONPATH),
292 to make sure anyone can build your docs from any location on their filesystem.
293
294 ### Using the PYTHONPATH environment variable
295
296 WARNING: **This method has limitations.**
297 This method might work for you, with your current setup,
298 but not for others trying your build your docs with their own setup/environment.
299 We recommend using the [`paths` method](#using-the-paths-option) instead.
300
301 You can take advantage of the usual Python loading mechanisms.
302 In Bash and other shells, you can run your command like this
303 (note the prepended `PYTHONPATH=...`):
304
305 1. mkdocs.yml in root, package in root
306 ```tree
307 root/
308 mkdocs.yml
309 package/
310 ```
311
312 ```bash
313 PYTHONPATH=. mkdocs build # actually not needed, default
314 ```
315
316 1. mkdocs.yml in root, package in subfolder
317 ```tree
318 root/
319 mkdocs.yml
320 src/
321 package/
322 ```
323
324 ```bash
325 PYTHONPATH=src mkdocs build
326 ```
327
328 1. mkdocs.yml in subfolder, package in root
329 ```tree
330 root/
331 docs/
332 mkdocs.yml
333 package/
334 ```
335
336 ```bash
337 PYTHONPATH=. mkdocs build -f docs/mkdocs.yml
338 ```
339
340 1. mkdocs.yml in subfolder, package in subfolder
341 ```tree
342 root/
343 docs/
344 mkdocs.yml
345 src/
346 package/
347 ```
348
349 ```bash
350 PYTHONPATH=src mkdocs build -f docs/mkdocs.yml
351 ```
352
353 ### Installing your package in the current Python environment
354
355 WARNING: **This method has limitations.**
356 This method might work for you, with your current setup,
357 but not for others trying your build your docs with their own setup/environment.
358 We recommend using the [`paths` method](#using-the-paths-option) instead.
359
360 Install your package in the current environment, and run MkDocs:
361
362 /// tab | pip
363 ```bash
364 . venv/bin/activate
365 pip install -e .
366 mkdocs build
367 ```
368 ///
369
370 /// tab | PDM
371 ```bash
372 pdm install
373 pdm run mkdocs build
374 ```
375 ///
376
377 /// tab | Poetry
378 ```bash
379 poetry install
380 poetry run mkdocs build
381 ```
382 ///
383
384 ## Recommended settings
385
386 If you're in a hurry, here is the configuration we recommend for the Python handler.
387
388 ```yaml
389 - mkdocstrings:
390 handlers:
391 python:
392
393 # Where to find your sources, see "Finding modules".
394 paths: [src]
395
396 # Load object inventories to enable cross-references to other projects.
397 inventories:
398 - https://docs.python.org/3/objects.inv
399 # Also load inventories of your dependencies, generally served at
400 # https://docs-url-for-your-dependency/objects.inv.
401
402 options:
403
404 # DOCSTRINGS -------------------------------------------------------------
405 docstring_options:
406 # Discard first line of `__init__` method docstrings,
407 # useful when merging such docstrings into their parent class'.
408 ignore_init_summary: true
409
410 # Tables are generally too large, lists will fix this.
411 docstring_section_style: list
412
413 # CROSS-REFERENCES -------------------------------------------------------
414 # Enable relative crossrefs and scoped crossrefs, see Docstrings options.
415 relative_crossrefs: true # Sponsors only!
416 scoped_crossrefs: true # Sponsors only!
417
418 # Enable cross-references in signatures.
419 signature_crossrefs: true
420
421 # Unwrap actual types from `Annotated` type annotations.
422 unwrap_annotated: true
423
424 # MEMBERS ----------------------------------------------------------------
425 # Only render pulic symbols.
426 filters: public # Sponsors only!
427 # Comment the option otherwise to get the default filters.
428
429 # Show class inherited members.
430 inherited_members: true
431
432 # Render auto-generated summaries of attributes, functions, etc.
433 # at the start of each symbol's documentation.
434 summary: true
435
436 # HEADINGS ---------------------------------------------------------------
437 # For auto-generated pages, one module per page,
438 # make the module heading be the H1 heading of the page.
439 heading_level: 1
440
441 # Render headings for parameters, making them linkable.
442 parameter_headings: true
443
444 # Render headings for type parameters too.
445 type_parameter_headings: true
446
447 # Always show the heading for the symbol you render with `::: id`.
448 show_root_heading: true
449
450 # Only show the name of the symbols you inject render `::: id`.
451 show_root_full_path: false
452
453 # Show the type of symbol (class, function, etc.) in the heading.
454 show_symbol_type_heading: true
455
456 # Show the type of symbol (class, function, etc.) in the table of contents.
457 show_symbol_type_toc: true
458
459 # SIGNATURES -------------------------------------------------------------
460 # Format code to 80 + 10% margin (Black and Ruff defaults)
461 # in signatures and attribute value code blocks.
462 # Needs Black/Ruff installed.
463 line_length: 88
464
465 # Merge signature and docstring of `__init__` methods
466 # into their parent class signature and docstring.
467 merge_init_into_class: true
468
469 # Render signatures and attribute values in a separate code block,
470 # below the symbol heading.
471 separate_signature: true
472
473 # Show type annotations in signatures.
474 show_signature_annotations: true
475
476 # Show type parameters in signatures.
477 show_signature_type_parameters: true
478
479 # OTHER ------------------------------------------------------------------
480 # Show backlinks to other documentation sections within each symbol.
481 backlinks: tree # Sponsors only!
482
483 # Show base classes OR inheritance diagram.
484 show_bases: false
485 show_inheritance_diagram: true # Sponsors only!
486 ```