Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/mkdocstrings_handlers/python/_internal/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. #}
<code>{{ class_name }}</code>
{% endif %}
{% endblock heading %}
Expand All @@ -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 %}
<div class="doc-overloads">
{% for overload in function.overloads %}
{% filter format_signature(overload, config.line_length, annotations=True, crossrefs=config.signature_crossrefs) %}
{% filter format_signature(overload, config.line_length, annotations=True, crossrefs=config.signature_crossrefs, klass=class) %}
{{ class.name }}
{% endfilter %}
{% endfor %}
</div>
{% endif %}
{% if config.separate_signature and not (config.show_overloads and function.overloads and config.overloads_only) %}
{% 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 %}
{% endif %}
{% endwith %}
{% elif config.separate_signature and 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 %}
{% endblock signature %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,15 @@ Context:
{% elif config.merge_init_into_class and "__init__" in class.all_members %}
{% with function = class.all_members["__init__"] %}
{%+ filter highlight(language="python", inline=True) %}
{{ class_name }}{% include "signature.html.jinja" with context %}
{{ class_name -}}
{%- include "type_parameters.html.jinja" with context -%}
{%- 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 %}
<code>{{ class_name }}</code>
{% endif %}
Expand All @@ -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 %}
Expand Down
49 changes: 49 additions & 0 deletions tests/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
[
Expand Down
Loading