Debian Codegraph @

docs/usage/configuration/signatures.md

raw
1 # Signatures options
2
3 [](){#option-annotations_path}
4 ## `annotations_path`
5
6 - **:octicons-package-24: Type [`str`][] :material-equal: `"brief"`{ title="default value" }**
7 <!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
8
9 The verbosity for annotations path.
10
11 Possible values:
12
13 - `brief` (recommended): render only the last component of each type path, not their full paths.
14 For example, it will render `Sequence[Path]` and not `typing.Sequence[pathlib.Path]`.
15 Brief annotations will cross-reference the right object anyway,
16 and show the full path in a tooltip when hovering them.
17 - `source`: render annotations as written in the source. For example if you imported `typing` as `t`,
18 it will render `typing.Sequence` as `t.Sequence`. Each part will cross-reference the relevant object:
19 `t` will link to the `typing` module and `Sequence` will link to the `Sequence` type.
20 - `full`: render annotations with their full path (the opposite of brief).
21 For example if you import `Sequence` and `Pattern` from `typing` and annoate something using
22 `Sequence[Pattern]`, it will render as `typing.Sequence[typing.Pattern]`, with each part
23 cross-referencing the corresponding object.
24
25 ```yaml title="in mkdocs.yml (global configuration)"
26 plugins:
27 - mkdocstrings:
28 handlers:
29 python:
30 options:
31 annotations_path: brief
32 ```
33
34 ```md title="or in docs/some_page.md (local configuration)"
35 ::: path.to.module
36 options:
37 annotations_path: source
38 ```
39
40
41 /// admonition | Preview
42 type: preview
43
44 //// tab | Brief annotations
45 ```python
46 import markdown
47 import markupsafe
48
49
50 def convert(text: str, md: markdown.Markdown) -> markupsafe.Markup:
51 """Convert text to Markdown.
52
53 Parameters:
54 text: The text to convert.
55 md: A Markdown instance.
56
57 Returns:
58 Converted markup.
59 """
60 return markupsafe.Markup(md.convert(text))
61 ```
62
63 <h2><code>convert(text, md)</code></h2>
64 <p>Convert text to Markdown.</p>
65 <p><b>Parameters:</b></p>
66
67 [](){#ref-to-markdown}
68
69 **Type** | **Description** | **Default**
70 ---------- | ------------------------ | -----------
71 [`str`][] | The text to convert. | *required*
72 [`Markdown`](#ref-to-markdown){ .external title="markdown.Markdown" } | A Markdown instance. | *required*
73
74 <p><b>Returns:</b></p>
75
76 [](){#ref-to-markup}
77
78 **Type** | **Name** | **Description**
79 ---------- | ----------- | ---------------
80 [`Markup`](#ref-to-markup){ .external title="markupsafe.Markup" } | `text` | Converted markup.
81 ////
82
83 //// tab | Source annotations
84 ```python
85 import markdown
86 from markupsafe import Markup
87
88
89 def convert(text: str, md: markdown.Markdown) -> Markup:
90 """Convert text to Markdown.
91
92 Parameters:
93 text: The text to convert.
94 md: A Markdown instance.
95
96 Returns:
97 Converted markup.
98 """
99 return Markup(md.convert(text))
100 ```
101
102 <h2><code>convert(text, md)</code></h2>
103 <p>Convert text to Markdown.</p>
104 <p><b>Parameters:</b></p>
105
106 **Type** | **Description** | **Default**
107 ---------- | ------------------------ | -----------
108 [`str`][] | The text to convert. | *required*
109 <code><a class="external" href="#ref-to-markdown">markdown</a>.<a class="external" href="#ref-to-Markdown" title="markdown.Markdown">Markdown</a></code> | A Markdown instance. | *required*
110
111 <p><b>Returns:</b></p>
112
113 **Type** | **Name** | **Description**
114 ---------- | ----------- | ---------------
115 [`Markup`](#ref-to-markup){ .external title="markupsafe.Markup" } | `text` | Converted markup.
116 ////
117
118 //// tab | Full annotations
119 ```python
120 from markdown import Markdown
121 from markupsafe import Markup
122
123
124 def convert(text: str, md: Markdown) -> Markup:
125 """Convert text to Markdown.
126
127 Parameters:
128 text: The text to convert.
129 md: A Markdown instance.
130
131 Returns:
132 Converted markup.
133 """
134 return Markup(md.convert(text))
135 ```
136
137 <h2><code>convert(text, md)</code></h2>
138 <p>Convert text to Markdown.</p>
139 <p><b>Parameters:</b></p>
140
141 **Type** | **Description** | **Default**
142 ---------- | ------------------------ | -----------
143 [`str`][] | The text to convert. | *required*
144 <code><a class="external" href="#ref-to-markdown">markdown</a>.<a class="external" href="#ref-to-Markdown" title="markdown.Markdown">Markdown</a></code> | A Markdown instance. | *required*
145
146 <p><b>Returns:</b></p>
147
148 **Type** | **Name** | **Description**
149 ---------- | ----------- | ---------------
150 <code><a class="external" href="#ref-to-markupsafe">markupsafe</a>.<a class="external" href="#ref-to-Markup" title="markupsafe.Markup">Markup</a></code> | `text` | Converted markup.
151 ////
152 ///
153
154 [](){#option-line_length}
155 ## `line_length`
156
157 - **:octicons-package-24: Type [`int`][] :material-equal: `60`{ title="default value" }**
158 <!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
159
160 Maximum line length when formatting code/signatures.
161
162 When separating signatures from headings with the [`separate_signature`][] option,
163 the Python handler will try to format the signatures using a formatter and
164 the specified line length.
165
166 The handler will automatically try to format using :
167
168 1. [Black]
169 2. [Ruff]
170
171 If a formatter is not found, the handler issues an INFO log once.
172
173 ```yaml title="in mkdocs.yml (global configuration)"
174 plugins:
175 - mkdocstrings:
176 handlers:
177 python:
178 options:
179 separate_signature: true
180 line_length: 60
181 ```
182
183 ```md title="or in docs/some_page.md (local configuration)"
184 ::: path.to.module
185 options:
186 separate_signature: true
187 line_length: 80
188 ```
189
190 /// admonition | Preview
191 type: preview
192
193 //// tab | Line length 60
194 <h2>long_function_name</h2>
195 <pre><code>long_function_name(
196 long_parameter_1="hello",
197 long_parameter_2="world",
198 )</code></pre>
199 ////
200
201 //// tab | Line length 80
202 <h2>long_function_name</h2>
203 <pre><code>long_function_name(long_parameter_1="hello", long_parameter_2="world")</code></pre>
204 ////
205 ///
206
207 [](){#option-modernize_annotations}
208 ## `modernize_annotations`
209
210 - **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
211 <!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->
212
213 Modernize annotations with latest features and PEPs of the Python language.
214
215 The Python language keeps evolving, and often library developers
216 must continue to support a few minor versions of Python.
217 Therefore they cannot use some features that were introduced
218 in the latest versions.
219
220 Yet this doesn't mean they can't enjoy latest features in their docs:
221 Griffe allows to "modernize" expressions, for example
222 by replacing `typing.Union` with [PEP 604][pep-604] type unions `|`.
223 Thanks to this, mkdocstrings' Python handler
224 can automatically transform type annotations into their modern equivalent.
225 This improves consistency in your docs, and shows users
226 how to use your code with the latest features of the language.
227
228 [pep-604]: https://peps.python.org/pep-0604/
229
230 Modernizations applied:
231
232 - `typing.Dict[A, B]` becomes `dict[A, B]`
233 - `typing.List[A]` becomes `list[A]`
234 - `typing.Set[A]` becomes `set[A]`
235 - `typing.Tuple[A]` becomes `tuple[A]`
236 - `typing.Union[A, B]` becomes `A | B`
237 - `typing.Optional[A]` becomes `A | None`
238
239 ```yaml title="in mkdocs.yml (global configuration)"
240 plugins:
241 - mkdocstrings:
242 handlers:
243 python:
244 options:
245 modernize_annotations: true
246 ```
247
248 ```md title="or in docs/some_page.md (local configuration)"
249 ::: path.to.object
250 options:
251 modernize_annotations: false
252 ```
253
254 /// admonition | Preview
255 type: preview
256
257 ```python
258 --8<-- "docs/snippets/package/modern.py"
259 ```
260
261 //// tab | Unchanged annotations
262
263 ```md exec="on"
264 ::: package.modern.example
265 options:
266 modernize_annotations: false
267 show_symbol_type_heading: false
268 show_labels: false
269 ```
270
271 ////
272
273 //// tab | Modernized annotations
274
275 ```md exec="on"
276 ::: package.modern.example
277 options:
278 modernize_annotations: true
279 show_symbol_type_heading: false
280 show_labels: false
281 ```
282
283 ////
284
285 ///
286
287 [](){#option-overloads_only}
288 ## `overloads_only`
289
290 - **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
291
292 Whether to hide the implementation signature if the overloads are shown with [`show_overloads`][].
293
294 ```yaml title="in mkdocs.yml (global configuration)"
295 plugins:
296 - mkdocstrings:
297 handlers:
298 python:
299 options:
300 overloads_only: false
301 ```
302
303 ```md title="or in docs/some_page.md (local configuration)"
304 ::: path.to.module
305 options:
306 overloads_only: true
307 ```
308
309 /// admonition | Preview
310 type: preview
311 //// tab | With overloads only
312 <h2>function</h2>
313
314 ```python
315 @overload
316 function(param1: int): ...
317 @overload
318 function(param1: str): ...
319 ```
320 Function docstring.
321
322 ////
323 //// tab | Without overloads only
324 <h2>function</h2>
325
326 ```python
327 @overload
328 function(param1: int): ...
329 @overload
330 function(param1: str): ...
331 function(param1: str | int)
332 ```
333 Function docstring.
334
335 ////
336
337 ///
338
339 [](){#option-show_signature}
340 ## `show_signature`
341
342 - **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
343 <!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
344
345 Show methods and functions signatures.
346
347 Without it, just the function/method name is rendered.
348
349 ```yaml title="in mkdocs.yml (global configuration)"
350 plugins:
351 - mkdocstrings:
352 handlers:
353 python:
354 options:
355 show_signature: true
356 ```
357
358 ```md title="or in docs/some_page.md (local configuration)"
359 ::: path.to.module
360 options:
361 show_signature: false
362 ```
363
364 /// admonition | Preview
365 type: preview
366
367 //// tab | With signature
368 <h2><code>function(param1, param2=None)</code></h2>
369 <p>Function docstring.</p>
370 ////
371
372 //// tab | Without signature
373 <h2><code>function</code></h2>
374 <p>Function docstring.</p>
375 ////
376 ///
377
378 [](){#option-show_signature_annotations}
379 ## `show_signature_annotations`
380
381 - **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
382 <!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
383
384 Show the type annotations in methods and functions signatures.
385
386 Since the heading can become quite long when annotations are rendered,
387 it is usually best to [separate the signature][separate_signature] from the heading.
388
389 ```yaml title="in mkdocs.yml (global configuration)"
390 plugins:
391 - mkdocstrings:
392 handlers:
393 python:
394 options:
395 separate_signature: true
396 show_signature_annotations: true
397 ```
398
399 ```md title="or in docs/some_page.md (local configuration)"
400 ::: path.to.module
401 options:
402 separate_signature: true
403 show_signature_annotations: false
404 ```
405
406 /// admonition | Preview
407 type: preview
408
409 //// tab | With signature annotations
410 <h2>function</h2>
411
412 ```python
413 function(
414 param1: list[int | float],
415 param2: bool | None = None,
416 ) -> float
417 ```
418
419 <p>Function docstring.</p>
420 ////
421
422 //// tab | Without signature annotations
423 <h2>function</h2>
424
425 ```python
426 function(param1, param2=None)
427 ```
428
429 <p>Function docstring.</p>
430 ////
431 ///
432
433 [](){#option-show_signature_type_parameters}
434 ## `show_signature_type_parameters`
435
436 - **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
437 <!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
438
439 Show the type parameters in generic classes, methods, functions and type aliases
440 signatures.
441
442 Since the heading can become quite long when type parameters are rendered, it is
443 usually best to [separate the signature][separate_signature] from the heading.
444
445 ```yaml title="in mkdocs.yml (global configuration)"
446 plugins:
447 - mkdocstrings:
448 handlers:
449 python:
450 options:
451 separate_signature: true
452 show_signature_annotations: true
453 show_signature_type_parameters: true
454 ```
455
456 ```md title="or in docs/some_page.md (local configuration)"
457 ::: path.to.module
458 options:
459 separate_signature: true
460 show_signature_annotations: true
461 show_signature_type_parameters: false
462 ```
463
464 /// admonition | Preview
465 type: preview
466
467 //// tab | With signature type parameters
468 <h2>function</h2>
469
470 ```python
471 function[T, *R](param: T) -> tuple[*R]
472 ```
473
474 <p>Function docstring.</p>
475 ////
476
477 //// tab | Without signature type parameters
478 <h2>function</h2>
479
480 ```python
481 function(param: T) -> tuple[*R]
482 ```
483
484 <p>Function docstring.</p>
485 ////
486 ///
487
488 [](){#option-separate_signature}
489 ## `separate_signature`
490
491 - **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
492 <!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
493
494 Whether to put the whole signature in a code block below the heading.
495
496 When separating signatures from headings,
497 the Python handler will try to format the signatures using a formatter and
498 the specified [line length][line_length].
499
500 The handler will automatically try to format using :
501
502 1. [Black]
503 2. [Ruff]
504
505 If a formatter is not found, the handler issues an INFO log once.
506
507 ```yaml title="in mkdocs.yml (global configuration)"
508 plugins:
509 - mkdocstrings:
510 handlers:
511 python:
512 options:
513 separate_signature: false
514 ```
515
516 ```md title="or in docs/some_page.md (local configuration)"
517 ::: path.to.module
518 options:
519 separate_signature: true
520 ```
521
522 /// admonition | Preview
523 type: preview
524
525 //// tab | With separate signature
526 <h2>function</h2>
527
528 ```python
529 function(param1, param2=None)
530 ```
531
532 <p>Function docstring.</p>
533 ////
534
535 //// tab | Without separate signature
536 <h2><code>function(param1, param2=None)</code></h2>
537 <p>Function docstring.</p>
538 ////
539 ///
540
541 [](){#option-show_attribute_values}
542 ## `show_attribute_values`
543
544 - **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
545 <!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->
546
547 Show initial values of attributes in classes.
548
549 ```yaml title="in mkdocs.yml (global configuration)"
550 plugins:
551 - mkdocstrings:
552 handlers:
553 python:
554 options:
555 show_attribute_values: true
556 ```
557
558 ```md title="or in docs/some_page.md (local configuration)"
559 ::: path.to.object
560 options:
561 show_attribute_values: true
562 ```
563
564 ```python title="package/module.py"
565 class SomeClass:
566 def __init__(self):
567 self.some_attr = 1
568 ```
569
570 /// admonition | Preview
571 type: preview
572
573 //// tab | With attribute values visible
574 <h2><code>SomeClass</code></h2>
575 <p>some_attr = 1</p>
576 ////
577
578 //// tab | With attribute values hidden
579 <h2><code>SomeClass</code></h2>
580 <p>some_attr</p>
581 ////
582 ///
583
584 [](){#option-show_overloads}
585 ## `show_overloads`
586
587 - **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
588
589 Whether to render function / method overloads.
590
591 ```yaml title="in mkdocs.yml (global configuration)"
592 plugins:
593 - mkdocstrings:
594 handlers:
595 python:
596 options:
597 show_overloads: true
598 ```
599
600 ```md title="or in docs/some_page.md (local configuration)"
601 ::: path.to.module
602 options:
603 show_overloads: false
604 ```
605
606 /// admonition | Preview
607 type: preview
608 //// tab | With overloads
609 <h2>function</h2>
610
611
612 ```python
613 @overload
614 function(param1: int): ...
615
616 @overload
617 function(param1: str): ...
618
619 function(param1: str | int)
620 ```
621 Function docstring.
622
623 ////
624 //// tab | Without overloads
625 <h2>function</h2>
626
627 ```python
628 function(param1: str | int)
629 ```
630 Function docstring.
631
632 ////
633 ///
634
635 [](){#option-signature_crossrefs}
636 ## `signature_crossrefs`
637
638 Whether to render cross-references for type annotations in signatures.
639
640 When signatures are separated from headings with the [`separate_signature`][] option
641 and type annotations are shown with the [`show_signature_annotations`][] option,
642 this option will render a cross-reference (link) for each type annotation in the signature.
643
644 ```yaml title="in mkdocs.yml (global configuration)"
645 plugins:
646 - mkdocstrings:
647 handlers:
648 python:
649 options:
650 separate_signature: true
651 show_signature_annotations: true
652 signature_crossrefs: false
653 ```
654
655 ```md title="or in docs/some_page.md (local configuration)"
656 ::: path.to.module
657 options:
658 separate_signature: true
659 show_signature_annotations: true
660 signature_crossrefs: true
661 ```
662
663 /// admonition | Preview
664 type: preview
665
666 //// tab | With signature cross-references
667 <h2>do_format_code</h2>
668 <div class="highlight"><pre><code><span class="n">do_format_code</span><span class="p">(</span><span class="n">code</span><span class="p">:</span> <span class="n"><a class="autorefs autorefs-external" href="https://docs.python.org/3/library/stdtypes.html#str">str</a></span><span class="p">,</span> <span class="n">line_length</span><span class="p">:</span> <span class="n"><a class="autorefs autorefs-external" href="https://docs.python.org/3/library/functions.html#int">int</a></span><span class="p">)</span> <span class="o">-&gt;</span> <span class="n"><a class="autorefs autorefs-external" href="https://docs.python.org/3/library/stdtypes.html#str">str</a></span>
669 </code></pre></div>
670 <p>Function docstring.</p>
671 ////
672
673 //// tab | Without signature cross-references
674 <h2>do_format_code</h2>
675 <div class="highlight"><pre><code><span class="n">do_format_code</span><span class="p">(</span><span class="n">code</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">line_length</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> <span class="nb">str</span>
676 </code></pre></div>
677 <p>Function docstring.</p>
678 ////
679 ///
680
681 [](){#option-unwrap_annotated}
682 ## `unwrap_annotated`
683
684 - **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
685 <!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
686
687 Whether to unwrap [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated){ .external }
688 types to show only the type without the annotations.
689
690 For example, unwrapping `Annotated[int, Gt(10)]` will render `int`.
691
692 ```yaml title="in mkdocs.yml (global configuration)"
693 plugins:
694 - mkdocstrings:
695 handlers:
696 python:
697 options:
698 unwrap_annotated: false
699 ```
700
701 ```md title="or in docs/some_page.md (local configuration)"
702 ::: path.to.module
703 options:
704 unwrap_annotated: true
705 ```