File tree Expand file tree Collapse file tree 4 files changed +31
-0
lines changed
src/mkdocstrings_handlers/python Expand file tree Collapse file tree 4 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -331,3 +331,29 @@ function(param1, param2=None)
331
331
<p>Function docstring.</p>
332
332
////
333
333
///
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
+ ` ` `
Original file line number Diff line number Diff line change @@ -161,6 +161,7 @@ plugins:
161
161
show_symbol_type_toc : true
162
162
signature_crossrefs : true
163
163
summary : true
164
+ unwrap_annotated : true
164
165
- git-committers :
165
166
enabled : !ENV [DEPLOY, false]
166
167
repository : mkdocstrings/python
Original file line number Diff line number Diff line change @@ -109,6 +109,7 @@ class PythonHandler(BaseHandler):
109
109
"annotations_path" : "brief" ,
110
110
"preload_modules" : None ,
111
111
"allow_inspection" : True ,
112
+ "unwrap_annotated" : False ,
112
113
}
113
114
"""
114
115
Attributes: General options:
@@ -180,6 +181,7 @@ class PythonHandler(BaseHandler):
180
181
signature_crossrefs (bool): Whether to render cross-references for type annotations in signatures. Default: `False`.
181
182
separate_signature (bool): Whether to put the whole signature in a code block below the heading.
182
183
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`.
183
185
"""
184
186
185
187
def __init__ (
Original file line number Diff line number Diff line change 25
25
{%- if signature -%}{{ expression|safe }}{%- else -%}{{ expression }}{%- endif -%}
26
26
{%- elif expression.classname == "ExprName" -%}
27
27
{{ 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) }}
28
30
{%- elif expression.classname == "ExprAttribute" -%}
29
31
{%- if annotations_path == "brief" -%}
30
32
{{ render(expression.last, "brief") }}
You can’t perform that action at this time.
0 commit comments