mkdocstrings-python 0.10.1__py3-none-any.whl → 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -84,6 +84,7 @@ class PythonHandler(BaseHandler):
84
84
  "show_if_no_docstring": False,
85
85
  "show_signature": True,
86
86
  "show_signature_annotations": False,
87
+ "signature_crossrefs": False,
87
88
  "separate_signature": False,
88
89
  "line_length": 60,
89
90
  "merge_init_into_class": False,
@@ -169,6 +170,7 @@ class PythonHandler(BaseHandler):
169
170
  line_length (int): Maximum line length when formatting code/signatures. Default: `60`.
170
171
  show_signature (bool): Show methods and functions signatures. Default: `True`.
171
172
  show_signature_annotations (bool): Show the type annotations in methods and functions signatures. Default: `False`.
173
+ signature_crossrefs (bool): Whether to render cross-references for type annotations in signatures. Default: `False`.
172
174
  separate_signature (bool): Whether to put the whole signature in a code block below the heading.
173
175
  If Black is installed, the signature is also formatted using it. Default: `False`.
174
176
  """
@@ -295,7 +297,8 @@ class PythonHandler(BaseHandler):
295
297
  mutabled_config = dict(copy.deepcopy(config))
296
298
  final_config = ChainMap(mutabled_config, self.default_config)
297
299
 
298
- template = self.env.get_template(f"{data.kind.value}.html")
300
+ template_name = rendering.do_get_template(data)
301
+ template = self.env.get_template(template_name)
299
302
 
300
303
  # Heading level is a "state" variable, that will change at each step
301
304
  # of the rendering recursion. Therefore, it's easier to use it as a plain value
@@ -314,6 +317,9 @@ class PythonHandler(BaseHandler):
314
317
  (re.compile(filtr.lstrip("!")), filtr.startswith("!")) for filtr in final_config["filters"]
315
318
  ]
316
319
 
320
+ # TODO: goal reached: remove once `signature_crossrefs` feature becomes public
321
+ final_config["signature_crossrefs"] = False
322
+
317
323
  return template.render(
318
324
  **{"config": final_config, data.kind.value: data, "heading_level": heading_level, "root": True},
319
325
  )
@@ -329,6 +335,8 @@ class PythonHandler(BaseHandler):
329
335
  self.env.filters["format_code"] = rendering.do_format_code
330
336
  self.env.filters["format_signature"] = rendering.do_format_signature
331
337
  self.env.filters["filter_objects"] = rendering.do_filter_objects
338
+ self.env.filters["stash_crossref"] = lambda ref, length: ref
339
+ self.env.filters["get_template"] = rendering.do_get_template
332
340
 
333
341
  def get_anchors(self, data: CollectorItem) -> set[str]: # noqa: D102 (ignore missing docstring)
334
342
  try:
@@ -8,11 +8,13 @@ import sys
8
8
  from functools import lru_cache
9
9
  from typing import TYPE_CHECKING, Any, Callable, Match, Pattern, Sequence
10
10
 
11
+ from jinja2 import pass_context
11
12
  from markupsafe import Markup
12
13
  from mkdocstrings.loggers import get_logger
13
14
 
14
15
  if TYPE_CHECKING:
15
- from griffe.dataclasses import Alias, Object
16
+ from griffe.dataclasses import Alias, Function, Object
17
+ from jinja2.runtime import Context
16
18
  from mkdocstrings.handlers.base import CollectorItem
17
19
 
18
20
  logger = get_logger(__name__)
@@ -60,26 +62,17 @@ def do_format_code(code: str, line_length: int) -> str:
60
62
  return formatter(code, line_length)
61
63
 
62
64
 
63
- def do_format_signature(signature: str, line_length: int) -> str:
64
- """Format a signature using Black.
65
-
66
- Parameters:
67
- signature: The signature to format.
68
- line_length: The line length to give to Black.
69
-
70
- Returns:
71
- The same code, formatted.
72
- """
73
- code = signature.strip()
74
- if len(code) < line_length:
75
- return code
65
+ def _format_signature(name: Markup, signature: str, line_length: int) -> str:
66
+ name = str(name).strip() # type: ignore[assignment]
67
+ signature = signature.strip()
68
+ if len(name + signature) < line_length:
69
+ return name + signature
76
70
 
77
71
  # Black cannot format names with dots, so we replace
78
72
  # the whole name with a string of equal length
79
- name_length = code.index("(")
80
- name = code[:name_length]
73
+ name_length = len(name)
81
74
  formatter = _get_black_formatter()
82
- formatable = f"def {'x' * name_length}{code[name_length:]}: pass"
75
+ formatable = f"def {'x' * name_length}{signature}: pass"
83
76
  formatted = formatter(formatable, line_length)
84
77
 
85
78
  # We put back the original name
@@ -87,6 +80,33 @@ def do_format_signature(signature: str, line_length: int) -> str:
87
80
  return name + formatted[4:-5].strip()[name_length:-1]
88
81
 
89
82
 
83
+ @pass_context
84
+ def do_format_signature(
85
+ context: Context,
86
+ callable_path: Markup,
87
+ function: Function,
88
+ line_length: int,
89
+ *,
90
+ crossrefs: bool = False, # noqa: ARG001
91
+ ) -> str:
92
+ """Format a signature using Black.
93
+
94
+ Parameters:
95
+ callable_path: The path of the callable we render the signature of.
96
+ line_length: The line length to give to Black.
97
+ crossrefs: Whether to cross-reference types in the signature.
98
+
99
+ Returns:
100
+ The same code, formatted.
101
+ """
102
+ env = context.environment
103
+ template = env.get_template("signature.html")
104
+ signature = template.render(context.parent, function=function)
105
+ signature = _format_signature(callable_path, signature, line_length)
106
+ signature = str(env.filters["highlight"](signature, language="python", inline=False))
107
+ return signature
108
+
109
+
90
110
  def do_order_members(
91
111
  members: Sequence[Object | Alias],
92
112
  order: Order,
@@ -228,3 +248,16 @@ def _get_black_formatter() -> Callable[[str, int], str]:
228
248
  return format_str(code, mode=mode)
229
249
 
230
250
  return formatter
251
+
252
+
253
+ def do_get_template(obj: Object) -> str:
254
+ """Get the template name used to render an object.
255
+
256
+ Parameters:
257
+ obj: A Griffe object.
258
+
259
+ Returns:
260
+ A template name.
261
+ """
262
+ extra_data = getattr(obj, "extra", {}).get("mkdocstrings", {})
263
+ return extra_data.get("template", "") or f"{obj.kind.value}.html"
@@ -27,7 +27,7 @@
27
27
  {% with heading_level = heading_level + extra_level %}
28
28
  {% for attribute in attributes|order_members(config.members_order, members_list) %}
29
29
  {% if not attribute.is_alias or attribute.is_explicitely_exported %}
30
- {% include "attribute.html" with context %}
30
+ {% include attribute|get_template with context %}
31
31
  {% endif %}
32
32
  {% endfor %}
33
33
  {% endwith %}
@@ -42,7 +42,7 @@
42
42
  {% with heading_level = heading_level + extra_level %}
43
43
  {% for class in classes|order_members(config.members_order, members_list) %}
44
44
  {% if not class.is_alias or class.is_explicitely_exported %}
45
- {% include "class.html" with context %}
45
+ {% include class|get_template with context %}
46
46
  {% endif %}
47
47
  {% endfor %}
48
48
  {% endwith %}
@@ -58,7 +58,7 @@
58
58
  {% for function in functions|order_members(config.members_order, members_list) %}
59
59
  {% if not (obj.kind.value == "class" and function.name == "__init__" and config.merge_init_into_class) %}
60
60
  {% if not function.is_alias or function.is_explicitely_exported %}
61
- {% include "function.html" with context %}
61
+ {% include function|get_template with context %}
62
62
  {% endif %}
63
63
  {% endif %}
64
64
  {% endfor %}
@@ -75,7 +75,7 @@
75
75
  {% with heading_level = heading_level + extra_level %}
76
76
  {% for module in modules|order_members(config.members_order, members_list) %}
77
77
  {% if not module.is_alias or module.is_explicitely_exported %}
78
- {% include "module.html" with context %}
78
+ {% include module|get_template with context %}
79
79
  {% endif %}
80
80
  {% endfor %}
81
81
  {% endwith %}
@@ -91,26 +91,26 @@
91
91
  filter_objects(filters=config.filters, members_list=members_list, keep_no_docstrings=config.show_if_no_docstring)|
92
92
  order_members(config.members_order, members_list) %}
93
93
 
94
- {% if not (obj.kind.value == "class" and child.name == "__init__" and config.merge_init_into_class) %}
94
+ {% if not (obj.is_class and child.name == "__init__" and config.merge_init_into_class) %}
95
95
 
96
- {% if child.kind.value == "attribute" %}
96
+ {% if child.is_attribute %}
97
97
  {% with attribute = child %}
98
- {% include "attribute.html" with context %}
98
+ {% include attribute|get_template with context %}
99
99
  {% endwith %}
100
100
 
101
- {% elif child.kind.value == "class" %}
101
+ {% elif child.is_class %}
102
102
  {% with class = child %}
103
- {% include "class.html" with context %}
103
+ {% include class|get_template with context %}
104
104
  {% endwith %}
105
105
 
106
- {% elif child.kind.value == "function" %}
106
+ {% elif child.is_function %}
107
107
  {% with function = child %}
108
- {% include "function.html" with context %}
108
+ {% include function|get_template with context %}
109
109
  {% endwith %}
110
110
 
111
- {% elif child.kind.value == "module" and config.show_submodules %}
111
+ {% elif child.is_module and config.show_submodules %}
112
112
  {% with module = child %}
113
- {% include "module.html" with context %}
113
+ {% include module|get_template with context %}
114
114
  {% endwith %}
115
115
 
116
116
  {% endif %}
@@ -43,11 +43,8 @@
43
43
  {% if config.separate_signature and config.merge_init_into_class %}
44
44
  {% if "__init__" in class.members %}
45
45
  {% with function = class.members["__init__"] %}
46
- {% filter highlight(language="python", inline=False) %}
47
- {% filter format_signature(config.line_length) %}
48
- {% if show_full_path %}{{ class.path }}{% else %}{{ class.name }}{% endif %}
49
- {% include "signature.html" with context %}
50
- {% endfilter %}
46
+ {% filter format_signature(function, config.line_length, crossrefs=config.signature_crossrefs) %}
47
+ {% if show_full_path %}{{ class.path }}{% else %}{{ class.name }}{% endif %}
51
48
  {% endfilter %}
52
49
  {% endwith %}
53
50
  {% endif %}
@@ -7,6 +7,8 @@
7
7
  {{ original_expression }}
8
8
  {%- else -%}
9
9
  {%- with annotation = original_expression|attr(config.annotations_path) -%}
10
- <span data-autorefs-optional{% if annotation != original_expression.full %}-hover{% endif %}="{{ original_expression.full }}">{{ annotation }}</span>
10
+ {%- filter stash_crossref(length=annotation|length) -%}
11
+ <span data-autorefs-optional{% if annotation != original_expression.full %}-hover{% endif %}="{{ original_expression.full }}">{{ annotation }}</span>
12
+ {%- endfilter -%}
11
13
  {%- endwith -%}
12
14
  {%- endif -%}
@@ -37,11 +37,8 @@
37
37
  {% endfilter %}
38
38
 
39
39
  {% if config.separate_signature %}
40
- {% filter highlight(language="python", inline=False) %}
41
- {% filter format_signature(config.line_length) %}
42
- {% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}
43
- {% include "signature.html" with context %}
44
- {% endfilter %}
40
+ {% filter format_signature(function, config.line_length, crossrefs=config.signature_crossrefs) %}
41
+ {% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}
45
42
  {% endfilter %}
46
43
  {% endif %}
47
44
 
@@ -2,7 +2,14 @@
2
2
  {{ log.debug("Rendering signature") }}
3
3
  {%- with -%}
4
4
 
5
- {%- set ns = namespace(has_pos_only=False, render_pos_only_separator=True, render_kw_only_separator=True, equal="=") -%}
5
+ {%- set ns = namespace(
6
+ has_pos_only=False,
7
+ render_pos_only_separator=True,
8
+ render_kw_only_separator=True,
9
+ annotation="",
10
+ equal="=",
11
+ )
12
+ -%}
6
13
 
7
14
  {%- if config.show_signature_annotations -%}
8
15
  {%- set ns.equal = " = " -%}
@@ -24,7 +31,13 @@
24
31
  {%- endif -%}
25
32
 
26
33
  {%- if config.show_signature_annotations and parameter.annotation is not none -%}
27
- {%- set annotation = ": " + parameter.annotation|safe -%}
34
+ {%- if config.separate_signature and config.signature_crossrefs -%}
35
+ {%- with expression = parameter.annotation -%}
36
+ {%- set ns.annotation -%}: {% include "expression.html" with context %}{%- endset -%}
37
+ {%- endwith -%}
38
+ {%- else -%}
39
+ {%- set ns.annotation = ": " + parameter.annotation|safe -%}
40
+ {%- endif -%}
28
41
  {%- endif -%}
29
42
 
30
43
  {%- if parameter.default is not none and parameter.kind.value != "variadic positional" and parameter.kind.value != "variadic keyword" -%}
@@ -36,13 +49,18 @@
36
49
  {%- endif -%}
37
50
 
38
51
  {% if parameter.kind.value == "variadic positional" %}*{% elif parameter.kind.value == "variadic keyword" %}**{% endif -%}
39
- {{ parameter.name }}{{ annotation }}{{ default }}
52
+ {{ parameter.name }}{{ ns.annotation }}{{ default }}
40
53
  {%- if not loop.last %}, {% endif -%}
41
54
 
42
55
  {%- endif -%}
43
56
  {%- endfor -%}
44
57
  )
45
- {%- if config.show_signature_annotations and function.annotation %} -> {{ function.annotation|safe }}{%- endif -%}
58
+ {%- if config.show_signature_annotations and function.annotation %} -> {% if config.separate_signature and config.signature_crossrefs -%}
59
+ {%- with expression = function.annotation %}{% include "expression.html" with context %}{%- endwith -%}
60
+ {%- else -%}
61
+ {{ function.annotation|safe }}
62
+ {%- endif -%}
63
+ {%- endif -%}
46
64
 
47
65
  {%- endwith -%}
48
66
  {%- endif -%}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mkdocstrings-python
3
- Version: 0.10.1
3
+ Version: 1.1.0
4
4
  Summary: A Python handler for mkdocstrings.
5
5
  Author-Email: Timothée Mazzucotelli <pawamoy@pm.me>
6
6
  License: ISC
@@ -25,8 +25,8 @@ Project-URL: Changelog, https://mkdocstrings.github.io/python/changelog
25
25
  Project-URL: Repository, https://github.com/mkdocstrings/python
26
26
  Project-URL: Issues, https://github.com/mkdocstrings/python/issues
27
27
  Project-URL: Discussions, https://github.com/mkdocstrings/python/discussions
28
- Project-URL: Gitter, https://gitter.im/python/community
29
- Project-URL: Funding, https://github.com/sponsors/mkdocstrings
28
+ Project-URL: Gitter, https://gitter.im/mkdocstrings/python
29
+ Project-URL: Funding, https://github.com/sponsors/pawamoy
30
30
  Requires-Python: >=3.7
31
31
  Requires-Dist: mkdocstrings>=0.20
32
32
  Requires-Dist: griffe>=0.24
@@ -1,10 +1,10 @@
1
1
  mkdocstrings_handlers/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  mkdocstrings_handlers/python/__init__.py,sha256=CJT3hmg9HpEMy5dbq2jL3Y8unO6UOHND7yCLojwQrS8,326
3
- mkdocstrings_handlers/python/handler.py,sha256=IllqOAKlYL2-XKtiEmUWQOiXM3VU2DMCi9J77JTSLgM,17613
4
- mkdocstrings_handlers/python/rendering.py,sha256=YuB7Gy6YfVxzczPrxHumzOos7FHPI2tqomV4hUPaJLY,6777
3
+ mkdocstrings_handlers/python/handler.py,sha256=VKDSxsSCFMNsTJSox4VBHLFkqaHk4A7fzUQVli2OxK0,18098
4
+ mkdocstrings_handlers/python/rendering.py,sha256=hjj64VBYJrQlVFF3jxwkla7nG5al6rXl0YH4AsHwPiY,7871
5
5
  mkdocstrings_handlers/python/templates/material/_base/attribute.html,sha256=JXn5axcd2ssBb02kzs9OVnKvo-UINOIC1ivsGpflIdc,2532
6
- mkdocstrings_handlers/python/templates/material/_base/children.html,sha256=-FKVAh62bJyqZ3T1R1lgtaRmIsZPnnuAfv-Lov1Hz50,5053
7
- mkdocstrings_handlers/python/templates/material/_base/class.html,sha256=z4eD-2rqRGsLQd6LdGOAVP52WAHWjEjvA6ZY8_R0ULY,4186
6
+ mkdocstrings_handlers/python/templates/material/_base/children.html,sha256=NOf0LuqOXOg3SVEwwP8ARndWjZrk8q2o-1Ajrrp4dG0,5036
7
+ mkdocstrings_handlers/python/templates/material/_base/class.html,sha256=wtTLXZl8gvOicdfxOCQaFt_59bn754C1k2ILbE4XvlE,4078
8
8
  mkdocstrings_handlers/python/templates/material/_base/docstring.html,sha256=LZu_zGw1YyGHN1fq-loPN9eKyYwC3rDWMyQbh31hD20,1740
9
9
  mkdocstrings_handlers/python/templates/material/_base/docstring/admonition.html,sha256=wiD2FtMnGrFMZJDilURnNqzIu8bOR0zcMErUVUqbDPo,274
10
10
  mkdocstrings_handlers/python/templates/material/_base/docstring/attributes.html,sha256=NwDp0mIyCnI43glTtFUbU7g0F0QismxyGPa7KV0ia3c,2465
@@ -16,11 +16,11 @@ mkdocstrings_handlers/python/templates/material/_base/docstring/receives.html,sh
16
16
  mkdocstrings_handlers/python/templates/material/_base/docstring/returns.html,sha256=EXaLJoz2vtQWVPAtQt2kbIw8xP3Y001vv9SIZvbZaMk,3081
17
17
  mkdocstrings_handlers/python/templates/material/_base/docstring/warns.html,sha256=8J7heEPD8IGNE-lr7mXAezB8RRWZ-YoUUdGnFl9FEpE,2115
18
18
  mkdocstrings_handlers/python/templates/material/_base/docstring/yields.html,sha256=43aDbTwzyGm4xyUf_p1T02MzsSHV_sNny2Se7KvPaqs,3051
19
- mkdocstrings_handlers/python/templates/material/_base/expression.html,sha256=UJ-o1AkXvcb8aaCxz2fxLb1U1oR2lqz0z4Fn7CgWa8k,590
20
- mkdocstrings_handlers/python/templates/material/_base/function.html,sha256=rYo2A7aPfZVtGklF7NlxOHR_9tTJIpHx_8lwR1bziXM,2532
19
+ mkdocstrings_handlers/python/templates/material/_base/expression.html,sha256=up-Z_v7N7MAmj_UKfR8Xc9TJHnBggIxcQaMXmubzRt8,674
20
+ mkdocstrings_handlers/python/templates/material/_base/function.html,sha256=_3-qihPbyZn9ijyUdJzfHSa1wA9NzCVzMx14Pq404EU,2436
21
21
  mkdocstrings_handlers/python/templates/material/_base/labels.html,sha256=Y85VylttZ7lYqtCjIjG3UjMaBNHWEiHEmfFMF6nyv6U,245
22
22
  mkdocstrings_handlers/python/templates/material/_base/module.html,sha256=M3tjXBpTriBekJdCgPhkmaqPsDblNai-aO85vPH9paA,1852
23
- mkdocstrings_handlers/python/templates/material/_base/signature.html,sha256=8FyNKyaFGyIBAkvln6ROKWsBHLDwp8rKErJaQIOL71E,2055
23
+ mkdocstrings_handlers/python/templates/material/_base/signature.html,sha256=KcdvKEAhGeJvA7qVHo0wzGhcyEKxTGHepLf13C0gT1A,2698
24
24
  mkdocstrings_handlers/python/templates/material/attribute.html,sha256=Zbo0SMrqjdlUhXunXpy_VENWITviBDLt8UBdF5EnVb0,37
25
25
  mkdocstrings_handlers/python/templates/material/children.html,sha256=xLnVaXevA0HRffVdOK0INULbekvbOYV9vKQ_zXBLyK0,36
26
26
  mkdocstrings_handlers/python/templates/material/class.html,sha256=BGYm-uj8AEDiUZtNxr6ji7I1xFrlAiyOB7cOwM7gzEM,33
@@ -53,7 +53,7 @@ mkdocstrings_handlers/python/templates/readthedocs/parameters.html,sha256=c0pfsj
53
53
  mkdocstrings_handlers/python/templates/readthedocs/returns.html,sha256=1rvLz7n3xHkhc82mOCJdjgRpGGIFdJqhmiVpv9pdxBE,494
54
54
  mkdocstrings_handlers/python/templates/readthedocs/style.css,sha256=FNcAj6sgr1H8cg9MCkqlOMoYeM2EWExIRVRCmilL9YY,600
55
55
  mkdocstrings_handlers/python/templates/readthedocs/yields.html,sha256=QLl6geeYfOHJ3xwmuRl-rAScei0DtIxFmlSgVO53Hhc,490
56
- mkdocstrings_python-0.10.1.dist-info/METADATA,sha256=qnnpRtY8xeR5RcKLsyLbPBO05ap1W7rW5j_C4LSmUzE,5750
57
- mkdocstrings_python-0.10.1.dist-info/WHEEL,sha256=7dGFtUmOf30dPBLpGD2z643cxg89joO7p3JHBAwDv6E,90
58
- mkdocstrings_python-0.10.1.dist-info/licenses/LICENSE,sha256=JGb4pdPEM8TTjjhr-uNCO7oXkiVrwG5Pz0JamcjNF_s,754
59
- mkdocstrings_python-0.10.1.dist-info/RECORD,,
56
+ mkdocstrings_python-1.1.0.dist-info/METADATA,sha256=aLxaWwtWx69109-NNM1s2QMyr7TnhWnC0fuZjWUub4c,5747
57
+ mkdocstrings_python-1.1.0.dist-info/WHEEL,sha256=m6kf1fJuo48rT4uxh0SeA_FsGq51BpCh_8VNBKVwYQA,90
58
+ mkdocstrings_python-1.1.0.dist-info/licenses/LICENSE,sha256=JGb4pdPEM8TTjjhr-uNCO7oXkiVrwG5Pz0JamcjNF_s,754
59
+ mkdocstrings_python-1.1.0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: pdm-backend (2.0.6)
2
+ Generator: pdm-backend (2.0.7)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any