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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
# Signatures options
[](){#option-annotations_path}
## `annotations_path`
- **:octicons-package-24: Type [`str`][] :material-equal: `"brief"`{ title="default value" }**
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
The verbosity for annotations path.
Possible values:
- `brief` (recommended): render only the last component of each type path, not their full paths.
For example, it will render `Sequence[Path]` and not `typing.Sequence[pathlib.Path]`.
Brief annotations will cross-reference the right object anyway,
and show the full path in a tooltip when hovering them.
- `source`: render annotations as written in the source. For example if you imported `typing` as `t`,
it will render `typing.Sequence` as `t.Sequence`. Each part will cross-reference the relevant object:
`t` will link to the `typing` module and `Sequence` will link to the `Sequence` type.
- `full`: render annotations with their full path (the opposite of brief).
For example if you import `Sequence` and `Pattern` from `typing` and annoate something using
`Sequence[Pattern]`, it will render as `typing.Sequence[typing.Pattern]`, with each part
cross-referencing the corresponding object.
```yaml title="in mkdocs.yml (global configuration)"
plugins:
- mkdocstrings:
handlers:
python:
options:
annotations_path: brief
```
```md title="or in docs/some_page.md (local configuration)"
::: path.to.module
options:
annotations_path: source
```
/// admonition | Preview
type: preview
//// tab | Brief annotations
```python
import markdown
import markupsafe
def convert(text: str, md: markdown.Markdown) -> markupsafe.Markup:
"""Convert text to Markdown.
Parameters:
text: The text to convert.
md: A Markdown instance.
Returns:
Converted markup.
"""
return markupsafe.Markup(md.convert(text))
```
<h2><code>convert(text, md)</code></h2>
<p>Convert text to Markdown.</p>
<p><b>Parameters:</b></p>
**Type** | **Description** | **Default**
---------- | ------------------------ | -----------
[`str`][] | The text to convert. | *required*
[`Markdown`](#ref-to-markdown){ .external title="markdown.Markdown" } | A Markdown instance. | *required*
<p><b>Returns:</b></p>
**Type** | **Name** | **Description**
---------- | ----------- | ---------------
[`Markup`](#ref-to-markup){ .external title="markupsafe.Markup" } | `text` | Converted markup.
////
//// tab | Source annotations
```python
import markdown
from markupsafe import Markup
def convert(text: str, md: markdown.Markdown) -> Markup:
"""Convert text to Markdown.
Parameters:
text: The text to convert.
md: A Markdown instance.
Returns:
Converted markup.
"""
return Markup(md.convert(text))
```
<h2><code>convert(text, md)</code></h2>
<p>Convert text to Markdown.</p>
<p><b>Parameters:</b></p>
**Type** | **Description** | **Default**
---------- | ------------------------ | -----------
[`str`][] | The text to convert. | *required*
<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*
<p><b>Returns:</b></p>
**Type** | **Name** | **Description**
---------- | ----------- | ---------------
[`Markup`](#ref-to-markup){ .external title="markupsafe.Markup" } | `text` | Converted markup.
////
//// tab | Full annotations
```python
from markdown import Markdown
from markupsafe import Markup
def convert(text: str, md: Markdown) -> Markup:
"""Convert text to Markdown.
Parameters:
text: The text to convert.
md: A Markdown instance.
Returns:
Converted markup.
"""
return Markup(md.convert(text))
```
<h2><code>convert(text, md)</code></h2>
<p>Convert text to Markdown.</p>
<p><b>Parameters:</b></p>
**Type** | **Description** | **Default**
---------- | ------------------------ | -----------
[`str`][] | The text to convert. | *required*
<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*
<p><b>Returns:</b></p>
**Type** | **Name** | **Description**
---------- | ----------- | ---------------
<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.
////
///
[](){#option-line_length}
## `line_length`
- **:octicons-package-24: Type [`int`][] :material-equal: `60`{ title="default value" }**
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
Maximum line length when formatting code/signatures.
When separating signatures from headings with the [`separate_signature`][] option,
the Python handler will try to format the signatures using a formatter and
the specified line length.
The handler will automatically try to format using :
1. [Black]
2. [Ruff]
If a formatter is not found, the handler issues an INFO log once.
```yaml title="in mkdocs.yml (global configuration)"
plugins:
- mkdocstrings:
handlers:
python:
options:
separate_signature: true
line_length: 60
```
```md title="or in docs/some_page.md (local configuration)"
::: path.to.module
options:
separate_signature: true
line_length: 80
```
/// admonition | Preview
type: preview
//// tab | Line length 60
<h2>long_function_name</h2>
<pre><code>long_function_name(
long_parameter_1="hello",
long_parameter_2="world",
)</code></pre>
////
//// tab | Line length 80
<h2>long_function_name</h2>
<pre><code>long_function_name(long_parameter_1="hello", long_parameter_2="world")</code></pre>
////
///
[](){#option-modernize_annotations}
## `modernize_annotations`
[:octicons-heart-fill-24:{ .pulse } Sponsors only](../../insiders/index.md){ .insiders } —
[:octicons-tag-24: Insiders 1.8.0](../../insiders/changelog.md#1.8.0) —
**This feature also requires
[Griffe Insiders](https://mkdocstrings.github.io/griffe/insiders/)
to be installed.**
- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
<!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->
Modernize annotations with latest features and PEPs of the Python language.
The Python language keeps evolving, and often library developers
must continue to support a few minor versions of Python.
Therefore they cannot use some features that were introduced
in the latest versions.
Yet this doesn't mean they can't enjoy latest features in their docs:
Griffe allows to "modernize" expressions, for example
by replacing `typing.Union` with [PEP 604][pep-604] type unions `|`.
Thanks to this, mkdocstrings' Python handler
can automatically transform type annotations into their modern equivalent.
This improves consistency in your docs, and shows users
how to use your code with the latest features of the language.
[pep-604]: https://peps.python.org/pep-0604/
Modernizations applied:
- `typing.Dict[A, B]` becomes `dict[A, B]`
- `typing.List[A]` becomes `list[A]`
- `typing.Set[A]` becomes `set[A]`
- `typing.Tuple[A]` becomes `tuple[A]`
- `typing.Union[A, B]` becomes `A | B`
- `typing.Optional[A]` becomes `A | None`
```yaml title="in mkdocs.yml (global configuration)"
plugins:
- mkdocstrings:
handlers:
python:
options:
modernize_annotations: true
```
```md title="or in docs/some_page.md (local configuration)"
::: path.to.object
options:
modernize_annotations: false
```
/// admonition | Preview
type: preview
```python
--8<-- "docs/snippets/package/modern.py"
```
//// tab | Unchanged annotations
```md exec="on"
::: package.modern.example
options:
modernize_annotations: false
show_symbol_type_heading: false
show_labels: false
```
////
//// tab | Modernized annotations
```md exec="on"
::: package.modern.example
options:
modernize_annotations: true
show_symbol_type_heading: false
show_labels: false
```
////
///
[](){#option-show_signature}
## `show_signature`
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
Show methods and functions signatures.
Without it, just the function/method name is rendered.
```yaml title="in mkdocs.yml (global configuration)"
plugins:
- mkdocstrings:
handlers:
python:
options:
show_signature: true
```
```md title="or in docs/some_page.md (local configuration)"
::: path.to.module
options:
show_signature: false
```
/// admonition | Preview
type: preview
//// tab | With signature
<h2><code>function(param1, param2=None)</code></h2>
<p>Function docstring.</p>
////
//// tab | Without signature
<h2><code>function</code></h2>
<p>Function docstring.</p>
////
///
[](){#option-show_signature_annotations}
## `show_signature_annotations`
- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
Show the type annotations in methods and functions signatures.
Since the heading can become quite long when annotations are rendered,
it is usually best to [separate the signature][separate_signature] from the heading.
```yaml title="in mkdocs.yml (global configuration)"
plugins:
- mkdocstrings:
handlers:
python:
options:
separate_signature: true
show_signature_annotations: true
```
```md title="or in docs/some_page.md (local configuration)"
::: path.to.module
options:
separate_signature: true
show_signature_annotations: false
```
/// admonition | Preview
type: preview
//// tab | With signature annotations
<h2>function</h2>
```python
function(
param1: list[int | float],
param2: bool | None = None,
) -> float
```
<p>Function docstring.</p>
////
//// tab | Without signature annotations
<h2>function</h2>
```python
function(param1, param2=None)
```
<p>Function docstring.</p>
////
///
[](){#option-separate_signature}
## `separate_signature`
- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
Whether to put the whole signature in a code block below the heading.
When separating signatures from headings,
the Python handler will try to format the signatures using a formatter and
the specified [line length][line_length].
The handler will automatically try to format using :
1. [Black]
2. [Ruff]
If a formatter is not found, the handler issues an INFO log once.
```yaml title="in mkdocs.yml (global configuration)"
plugins:
- mkdocstrings:
handlers:
python:
options:
separate_signature: false
```
```md title="or in docs/some_page.md (local configuration)"
::: path.to.module
options:
separate_signature: true
```
/// admonition | Preview
type: preview
//// tab | With separate signature
<h2>function</h2>
```python
function(param1, param2=None)
```
<p>Function docstring.</p>
////
//// tab | Without separate signature
<h2><code>function(param1, param2=None)</code></h2>
<p>Function docstring.</p>
////
///
[](){#option-show_overloads}
## `show_overloads`
Whether to render function / method overloads.
```yaml title="in mkdocs.yml (global configuration)"
plugins:
- mkdocstrings:
handlers:
python:
options:
show_overloads: true
```
```md title="or in docs/some_page.md (local configuration)"
::: path.to.module
options:
show_overloads: false
```
/// admonition | Preview
type: preview
//// tab | With overloads
<h2>function</h2>
```python
@overload
function(param1: int): ...
@overload
function(param1: str): ...
function(param1: str | int)
```
Function docstring.
////
//// tab | Without overloads
<h2>function</h2>
```python
function(param1: str | int)
```
Function docstring.
////
///
[](){#option-signature_crossrefs}
## `signature_crossrefs`
[:octicons-tag-24: Insiders 1.0.0](../../insiders/changelog.md#1.0.0)
Whether to render cross-references for type annotations in signatures.
When signatures are separated from headings with the [`separate_signature`][] option
and type annotations are shown with the [`show_signature_annotations`][] option,
this option will render a cross-reference (link) for each type annotation in the signature.
```yaml title="in mkdocs.yml (global configuration)"
plugins:
- mkdocstrings:
handlers:
python:
options:
separate_signature: true
show_signature_annotations: true
signature_crossrefs: false
```
```md title="or in docs/some_page.md (local configuration)"
::: path.to.module
options:
separate_signature: true
show_signature_annotations: true
signature_crossrefs: true
```
/// admonition | Preview
type: preview
//// tab | With signature cross-references
<h2>do_format_code</h2>
<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">-></span> <span class="n"><a class="autorefs autorefs-external" href="https://docs.python.org/3/library/stdtypes.html#str">str</a></span>
</code></pre></div>
<p>Function docstring.</p>
////
//// tab | Without signature cross-references
<h2>do_format_code</h2>
<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">-></span> <span class="nb">str</span>
</code></pre></div>
<p>Function docstring.</p>
////
///
[](){#option-unwrap_annotated}
## `unwrap_annotated`
- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
Whether to unwrap [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated){ .external }
types to show only the type without the annotations.
For example, unwrapping `Annotated[int, Gt(10)]` will render `int`.
```yaml title="in mkdocs.yml (global configuration)"
plugins:
- mkdocstrings:
handlers:
python:
options:
unwrap_annotated: false
```
```md title="or in docs/some_page.md (local configuration)"
::: path.to.module
options:
unwrap_annotated: true
```
|