Skip to content

Commit 15a2e7f

Browse files
committed
fix: Render class type parameters in signatures
1 parent f5b9e25 commit 15a2e7f

4 files changed

Lines changed: 87 additions & 19 deletions

File tree

‎src/mkdocstrings_handlers/python/_internal/rendering.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,21 +157,23 @@ def _format_signature(name: Markup, signature: str, line_length: int) -> str:
157157
def do_format_signature(
158158
context: Context,
159159
callable_path: Markup,
160-
function: Function,
160+
function: Function | None,
161161
line_length: int,
162162
*,
163163
annotations: bool | None = None,
164164
crossrefs: bool = False, # noqa: ARG001
165+
klass: Class | None = None,
165166
) -> str:
166167
"""Format a signature.
167168
168169
Parameters:
169170
context: Jinja context, passed automatically.
170171
callable_path: The path of the callable we render the signature of.
171-
function: The function we render the signature of.
172+
function: The function we render the signature of, if any.
172173
line_length: The line length.
173174
annotations: Whether to show type annotations.
174175
crossrefs: Whether to cross-reference types in the signature.
176+
klass: The class whose type parameters to show instead of the constructor's.
175177
176178
Returns:
177179
The same code, formatted.
@@ -186,10 +188,13 @@ def do_format_signature(
186188
new_context = dict(context.parent)
187189
new_context["config"] = replace(new_context["config"], show_signature_annotations=annotations)
188190

189-
signature = type_params_template.render(context.parent, obj=function, signature=True)
190-
signature += signature_template.render(new_context, function=function, signature=True)
191-
192-
signature = _format_signature(callable_path, signature, line_length)
191+
obj = klass if klass is not None and klass.is_generic else function
192+
signature = type_params_template.render(context.parent, obj=obj, signature=True)
193+
if function is None:
194+
signature = str(callable_path).strip() + signature
195+
else:
196+
signature += signature_template.render(new_context, function=function, signature=True)
197+
signature = _format_signature(callable_path, signature, line_length)
193198
signature = str(
194199
env.filters["highlight"](
195200
Markup.escape(signature),

‎src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja‎

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,17 @@ Context:
5959
{% with function = all_members["__init__"] %}
6060
{%+ filter highlight(language="python", inline=True) %}
6161
{{ class_name -}}
62-
{%- with obj = function -%}
62+
{%- with obj = class if class.is_generic else function -%}
6363
{%- include "type_parameters.html.jinja" with context -%}
6464
{%- endwith -%}
6565
{%- include "signature.html.jinja" with context -%}
6666
{% endfilter %}
6767
{% endwith %}
68+
{% elif class.is_generic and config.show_signature_type_parameters %}
69+
{%+ filter highlight(language="python", inline=True) -%}
70+
{{ class_name }}{% include "type_parameters.html.jinja" with context %}
71+
{%- endfilter %}
6872
{% else %}
69-
{# TODO: Maybe render type parameters here. #}
7073
<code>{{ class_name }}</code>
7174
{% endif %}
7275
{% endblock heading %}
@@ -90,29 +93,30 @@ Context:
9093
Overloads of the `__init__` method are rendered if `merge_init_into_class` is enabled.
9194
The actual `__init__` method signature is only rendered if `separate_signature` is also enabled.
9295
93-
If the class is generic, but the `__init__` method isn't or `merge_init_into_class` is disabled,
94-
the class signature is rendered if `separate_signature` and `show_signature_type_parameters` are enabled.
95-
96-
If the `__init__` method or any overloads are generic, they are rendered as methods if
97-
`merge_init_into_class`, `separate_signature` and `show_signature_type_parameters` are enabled.
96+
Generic class type parameters are included in the merged signatures, or rendered separately
97+
if there is no merged constructor and both `separate_signature` and `show_signature_type_parameters` are enabled.
9898
-#}
9999
{% if config.merge_init_into_class and "__init__" in all_members %}
100100
{% with function = all_members["__init__"] %}
101101
{% if function.overloads and config.show_overloads %}
102102
<div class="doc-overloads">
103103
{% for overload in function.overloads %}
104-
{% filter format_signature(overload, config.line_length, annotations=True, crossrefs=config.signature_crossrefs) %}
104+
{% filter format_signature(overload, config.line_length, annotations=True, crossrefs=config.signature_crossrefs, klass=class) %}
105105
{{ class.name }}
106106
{% endfilter %}
107107
{% endfor %}
108108
</div>
109109
{% endif %}
110110
{% if config.separate_signature and not (config.show_overloads and function.overloads and config.overloads_only) %}
111-
{% filter format_signature(function, config.line_length, crossrefs=config.signature_crossrefs) %}
111+
{% filter format_signature(function, config.line_length, crossrefs=config.signature_crossrefs, klass=class) %}
112112
{{ class.name }}
113113
{% endfilter %}
114114
{% endif %}
115115
{% endwith %}
116+
{% elif config.separate_signature and class.is_generic and config.show_signature_type_parameters %}
117+
{% filter format_signature(none, config.line_length, crossrefs=config.signature_crossrefs, klass=class) %}
118+
{{ class.name }}
119+
{% endfilter %}
116120
{% endif %}
117121
{% endblock signature %}
118122

‎src/mkdocstrings_handlers/python/templates/readthedocs/_base/class.html.jinja‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,15 @@ Context:
5252
{% elif config.merge_init_into_class and "__init__" in class.all_members %}
5353
{% with function = class.all_members["__init__"] %}
5454
{%+ filter highlight(language="python", inline=True) %}
55-
{{ class_name }}{% include "signature.html.jinja" with context %}
55+
{{ class_name -}}
56+
{%- include "type_parameters.html.jinja" with context -%}
57+
{%- include "signature.html.jinja" with context -%}
5658
{% endfilter %}
5759
{% endwith %}
60+
{% elif class.is_generic and config.show_signature_type_parameters %}
61+
{%+ filter highlight(language="python", inline=True) -%}
62+
{{ class_name }}{% include "type_parameters.html.jinja" with context %}
63+
{%- endfilter %}
5864
{% else %}
5965
<code>{{ class_name }}</code>
6066
{% endif %}
@@ -77,13 +83,17 @@ Context:
7783
7884
This block renders the signature for the class.
7985
-#}
80-
{% if config.separate_signature and config.merge_init_into_class %}
81-
{% if "__init__" in class.all_members %}
86+
{% if config.separate_signature %}
87+
{% if config.merge_init_into_class and "__init__" in class.all_members %}
8288
{% with function = class.all_members["__init__"] %}
83-
{% filter format_signature(function, config.line_length, crossrefs=config.signature_crossrefs) %}
89+
{% filter format_signature(function, config.line_length, crossrefs=config.signature_crossrefs, klass=class) %}
8490
{{ class.name }}
8591
{% endfilter %}
8692
{% endwith %}
93+
{% elif class.is_generic and config.show_signature_type_parameters %}
94+
{% filter format_signature(none, config.line_length, crossrefs=config.signature_crossrefs, klass=class) %}
95+
{{ class.name }}
96+
{% endfilter %}
8797
{% endif %}
8898
{% endif %}
8999
{% endblock signature %}

‎tests/test_handler.py‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,55 @@ def __init__(self):
279279
assert targets == (["str"] if signature_crossrefs else [])
280280

281281

282+
@pytest.mark.skipif(sys.version_info < (3, 12), reason="requires PEP 695 syntax")
283+
@pytest.mark.parametrize("mkdocs_conf", [{"theme": "material"}, {"theme": "readthedocs"}], indirect=True)
284+
@pytest.mark.parametrize("separate_signature", [False, True])
285+
@pytest.mark.parametrize("show_type_parameters", [False, True])
286+
@pytest.mark.parametrize("merge_init", [False, True])
287+
def test_class_signature_type_parameters(
288+
handler: PythonHandler,
289+
separate_signature: bool,
290+
show_type_parameters: bool,
291+
merge_init: bool,
292+
) -> None:
293+
"""Show class type parameters with or without a merged constructor."""
294+
code = """
295+
class Box[T]:
296+
pass
297+
298+
class Pair[T]:
299+
def __init__(self, first: T, second: T):
300+
pass
301+
"""
302+
options = handler.get_options(
303+
{
304+
"merge_init_into_class": merge_init,
305+
"separate_signature": separate_signature,
306+
"show_root_full_path": False,
307+
"show_root_heading": True,
308+
"show_signature_type_parameters": show_type_parameters,
309+
"show_source": False,
310+
},
311+
)
312+
selector = ".doc-signature" if separate_signature else ".doc-heading"
313+
type_parameters = "[T]" if show_type_parameters else ""
314+
with temporary_visited_module(code) as module:
315+
box_html = bs4.BeautifulSoup(handler.render(module["Box"], options), "html.parser")
316+
pair_html = bs4.BeautifulSoup(handler.render(module["Pair"], options), "html.parser")
317+
318+
box_signature = box_html.select_one(selector)
319+
pair_signature = pair_html.select_one(selector)
320+
for name, signature, constructor in (
321+
("Box", box_signature, ""),
322+
("Pair", pair_signature, "(first,second)" if merge_init else ""),
323+
):
324+
if separate_signature and not (show_type_parameters or constructor):
325+
assert signature is None
326+
else:
327+
assert signature is not None
328+
assert signature.get_text(strip=True) == f"{name}{type_parameters}{constructor}"
329+
330+
282331
@pytest.mark.parametrize(
283332
("handler", "merge_init_into_class"),
284333
[

0 commit comments

Comments
 (0)