Skip to content

Commit 53db04b

Browse files
committed
feat: Add option to unwrap Annotated types
1 parent 1896745 commit 53db04b

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

docs/usage/configuration/signatures.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,3 +331,29 @@ function(param1, param2=None)
331331
<p>Function docstring.</p>
332332
////
333333
///
334+
335+
336+
## `unwrap_annotated`
337+
338+
- **:octicons-package-24: Type [`bool`][] :material-equal: `False`{ title="default value" }**
339+
<!-- - **:octicons-project-template-24: Template :material-null:** (N/A) -->
340+
341+
Whether to unwrap [`Annotated`](https://docs.python.org/3/library/typing.html#typing.Annotated){ .external }
342+
types to show only the type without the annotations.
343+
344+
For example, unwrapping `Annotated[int, Gt(10)]` will render `int`.
345+
346+
```yaml title="in mkdocs.yml (global configuration)"
347+
plugins:
348+
- mkdocstrings:
349+
handlers:
350+
python:
351+
options:
352+
unwrap_annotated: false
353+
```
354+
355+
```md title="or in docs/some_page.md (local configuration)"
356+
::: path.to.module
357+
options:
358+
unwrap_annotated: true
359+
```

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ plugins:
161161
show_symbol_type_toc: true
162162
signature_crossrefs: true
163163
summary: true
164+
unwrap_annotated: true
164165
- git-committers:
165166
enabled: !ENV [DEPLOY, false]
166167
repository: mkdocstrings/python

src/mkdocstrings_handlers/python/handler.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class PythonHandler(BaseHandler):
109109
"annotations_path": "brief",
110110
"preload_modules": None,
111111
"allow_inspection": True,
112+
"unwrap_annotated": False,
112113
}
113114
"""
114115
Attributes: General options:
@@ -180,6 +181,7 @@ class PythonHandler(BaseHandler):
180181
signature_crossrefs (bool): Whether to render cross-references for type annotations in signatures. Default: `False`.
181182
separate_signature (bool): Whether to put the whole signature in a code block below the heading.
182183
If Black is installed, the signature is also formatted using it. Default: `False`.
184+
unwrap_annotated (bool): Whether to unwrap `Annotated` types to show only the type without the annotations. Default: `False`.
183185
"""
184186

185187
def __init__(

src/mkdocstrings_handlers/python/templates/material/_base/expression.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
{%- if signature -%}{{ expression|safe }}{%- else -%}{{ expression }}{%- endif -%}
2626
{%- elif expression.classname == "ExprName" -%}
2727
{{ crossref(expression, annotations_path) }}
28+
{%- elif config.unwrap_annotated and expression.classname == "ExprSubscript" and expression.canonical_path in ("typing.Annotated", "typing_extensions.Annotated") -%}
29+
{{ render(expression.slice.elements[0], annotations_path) }}
2830
{%- elif expression.classname == "ExprAttribute" -%}
2931
{%- if annotations_path == "brief" -%}
3032
{{ render(expression.last, "brief") }}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy