8000 fix: Fix separate signature feature · mkdocstrings/python@da6e81c · GitHub
[go: up one dir, main page]

Skip to content

Commit da6e81c

Browse files
committed
fix: Fix separate signature feature
1 parent b34ead0 commit da6e81c

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/mkdocstrings/handlers/python/renderer.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -132,24 +132,27 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore
132132
self.env.filters["crossref"] = self.do_crossref
133133
self.env.filters["multi_crossref"] = self.do_multi_crossref
134134
self.env.filters["order_members"] = self.do_order_members
135-
self.env.filters["format_code"] = self.do_format_code
135+
self.env.filters["format_signature"] = self.do_format_signature
136136

137-
def do_format_code(self, code: str, line_length: int) -> str:
138-
"""Format the code using Black.
137+
def do_format_signature(self, signature: str, line_length: int) -> str:
138+
"""Format a signature using Black.
139139
140140
Parameters:
141-
code: The code to format.
141+
signature: The signature to format.
142142
line_length: The line length to give to Black.
143143
144144
Returns:
145145
The same code, formatted.
146146
"""
147147
from black import Mode, format_str
148148

149-
if len(code) >= line_length:
150-
mode = Mode(line_length=line_length)
151-
return format_str(f"def {code}: pass", mode=mode)[4:-6].strip()
152-
return code
149+
code = signature.strip()
150+
if len(code) < line_length:
151+
return code
152+
mode = Mode(line_length=line_length)
153+
formatted = format_str(f"def {code}: pass", mode=mode)
154+
# remove starting `def ` and trailing `: pass`
155+
return formatted[4:-5].strip()[:-1]
153156

154157
def do_order_members(self, members: Sequence[Object | Alias], order: Order) -> Sequence[Object | Alias]:
155158
"""Order members given an ordering method.

src/mkdocstrings/templates/python/material/_base/function.html

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,7 @@
2323
toc_label=function.name ~ "()") %}
2424

2525
{% if config.separate_signature %}
26-
<details>
27-
<summary>Signature</summary>
28-
{% filter highlight(language="python", inline=False) %}
29-
{% filter format_code(config.line_length) %}
30-
{% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}
31-
{% include "signature.html" with context %}
32-
{% endfilter %}
33-
{% endfilter %}
34-
</details>
26+
{% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}()
3527
{% else %}
3628
{% filter highlight(language="python", inline=True) %}
3729
{% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}
@@ -45,6 +37,15 @@
4537

4638
{% endfilter %}
4739

40+
{% if config.separate_signature %}
41+
{% filter highlight(language="python", inline=False) %}
42+
{% filter format_signature(config.line_length) %}
43+
{% if show_full_path %}{{ function.path }}{% else %}{{ function.name }}{% endif %}
44+
{% include "signature.html" with context %}
45+
{% endfilter %}
46+
{% endfilter %}
47+
{% endif %}
48+
4849
{% else %}
4950
{% if config.show_root_toc_entry %}
5051
{% filter heading(heading_level,

0 commit comments

Comments
 (0)
0