diff --git a/src/mkdocstrings_handlers/python/_internal/rendering.py b/src/mkdocstrings_handlers/python/_internal/rendering.py
index 4c7be1b..b2af11e 100644
--- a/src/mkdocstrings_handlers/python/_internal/rendering.py
+++ b/src/mkdocstrings_handlers/python/_internal/rendering.py
@@ -157,21 +157,23 @@ def _format_signature(name: Markup, signature: str, line_length: int) -> str:
def do_format_signature(
context: Context,
callable_path: Markup,
- function: Function,
+ function: Function | None,
line_length: int,
*,
annotations: bool | None = None,
crossrefs: bool = False, # noqa: ARG001
+ klass: Class | None = None,
) -> str:
"""Format a signature.
Parameters:
context: Jinja context, passed automatically.
callable_path: The path of the callable we render the signature of.
- function: The function we render the signature of.
+ function: The function we render the signature of, if any.
line_length: The line length.
annotations: Whether to show type annotations.
crossrefs: Whether to cross-reference types in the signature.
+ klass: The class whose type parameters to show instead of the constructor's.
Returns:
The same code, formatted.
@@ -186,10 +188,13 @@ def do_format_signature(
new_context = dict(context.parent)
new_context["config"] = replace(new_context["config"], show_signature_annotations=annotations)
- signature = type_params_template.render(context.parent, obj=function, signature=True)
- signature += signature_template.render(new_context, function=function, signature=True)
-
- signature = _format_signature(callable_path, signature, line_length)
+ obj = klass if klass is not None and klass.is_generic else function
+ signature = type_params_template.render(context.parent, obj=obj, signature=True)
+ if function is None:
+ signature = str(callable_path).strip() + signature
+ else:
+ signature += signature_template.render(new_context, function=function, signature=True)
+ signature = _format_signature(callable_path, signature, line_length)
signature = str(
env.filters["highlight"](
Markup.escape(signature),
diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja
index 55b7a7c..c0c5384 100644
--- a/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja
+++ b/src/mkdocstrings_handlers/python/templates/material/_base/class.html.jinja
@@ -59,14 +59,17 @@ Context:
{% with function = all_members["__init__"] %}
{%+ filter highlight(language="python", inline=True) %}
{{ class_name -}}
- {%- with obj = function -%}
+ {%- with obj = class if class.is_generic else function -%}
{%- include "type_parameters.html.jinja" with context -%}
{%- endwith -%}
{%- include "signature.html.jinja" with context -%}
{% endfilter %}
{% endwith %}
+ {% elif class.is_generic and config.show_signature_type_parameters %}
+ {%+ filter highlight(language="python", inline=True) -%}
+ {{ class_name }}{% include "type_parameters.html.jinja" with context %}
+ {%- endfilter %}
{% else %}
- {# TODO: Maybe render type parameters here. #}
{{ class_name }}
{% endif %}
{% endblock heading %}
@@ -90,29 +93,30 @@ Context:
Overloads of the `__init__` method are rendered if `merge_init_into_class` is enabled.
The actual `__init__` method signature is only rendered if `separate_signature` is also enabled.
- If the class is generic, but the `__init__` method isn't or `merge_init_into_class` is disabled,
- the class signature is rendered if `separate_signature` and `show_signature_type_parameters` are enabled.
-
- If the `__init__` method or any overloads are generic, they are rendered as methods if
- `merge_init_into_class`, `separate_signature` and `show_signature_type_parameters` are enabled.
+ Generic class type parameters are included in the merged signatures, or rendered separately
+ if there is no merged constructor and both `separate_signature` and `show_signature_type_parameters` are enabled.
-#}
{% if config.merge_init_into_class and "__init__" in all_members %}
{% with function = all_members["__init__"] %}
{% if function.overloads and config.show_overloads %}
{{ class_name }}
{% endif %}
@@ -77,13 +83,17 @@ Context:
This block renders the signature for the class.
-#}
- {% if config.separate_signature and config.merge_init_into_class %}
- {% if "__init__" in class.all_members %}
+ {% if config.separate_signature %}
+ {% if config.merge_init_into_class and "__init__" in class.all_members %}
{% with function = class.all_members["__init__"] %}
- {% filter format_signature(function, config.line_length, crossrefs=config.signature_crossrefs) %}
+ {% filter format_signature(function, config.line_length, crossrefs=config.signature_crossrefs, klass=class) %}
{{ class.name }}
{% endfilter %}
{% endwith %}
+ {% elif class.is_generic and config.show_signature_type_parameters %}
+ {% filter format_signature(none, config.line_length, crossrefs=config.signature_crossrefs, klass=class) %}
+ {{ class.name }}
+ {% endfilter %}
{% endif %}
{% endif %}
{% endblock signature %}
diff --git a/tests/test_handler.py b/tests/test_handler.py
index eabb211..ff5245f 100644
--- a/tests/test_handler.py
+++ b/tests/test_handler.py
@@ -279,6 +279,55 @@ def __init__(self):
assert targets == (["str"] if signature_crossrefs else [])
+@pytest.mark.skipif(sys.version_info < (3, 12), reason="requires PEP 695 syntax")
+@pytest.mark.parametrize("mkdocs_conf", [{"theme": "material"}, {"theme": "readthedocs"}], indirect=True)
+@pytest.mark.parametrize("separate_signature", [False, True])
+@pytest.mark.parametrize("show_type_parameters", [False, True])
+@pytest.mark.parametrize("merge_init", [False, True])
+def test_class_signature_type_parameters(
+ handler: PythonHandler,
+ separate_signature: bool,
+ show_type_parameters: bool,
+ merge_init: bool,
+) -> None:
+ """Show class type parameters with or without a merged constructor."""
+ code = """
+ class Box[T]:
+ pass
+
+ class Pair[T]:
+ def __init__(self, first: T, second: T):
+ pass
+ """
+ options = handler.get_options(
+ {
+ "merge_init_into_class": merge_init,
+ "separate_signature": separate_signature,
+ "show_root_full_path": False,
+ "show_root_heading": True,
+ "show_signature_type_parameters": show_type_parameters,
+ "show_source": False,
+ },
+ )
+ selector = ".doc-signature" if separate_signature else ".doc-heading"
+ type_parameters = "[T]" if show_type_parameters else ""
+ with temporary_visited_module(code) as module:
+ box_html = bs4.BeautifulSoup(handler.render(module["Box"], options), "html.parser")
+ pair_html = bs4.BeautifulSoup(handler.render(module["Pair"], options), "html.parser")
+
+ box_signature = box_html.select_one(selector)
+ pair_signature = pair_html.select_one(selector)
+ for name, signature, constructor in (
+ ("Box", box_signature, ""),
+ ("Pair", pair_signature, "(first,second)" if merge_init else ""),
+ ):
+ if separate_signature and not (show_type_parameters or constructor):
+ assert signature is None
+ else:
+ assert signature is not None
+ assert signature.get_text(strip=True) == f"{name}{type_parameters}{constructor}"
+
+
@pytest.mark.parametrize(
("handler", "merge_init_into_class"),
[