Skip to content

Commit 8f0ade2

Browse files
committed
refactor: Add a format_attribute filter, preparing for cross-refs in attribute signatures
1 parent d56ebcc commit 8f0ade2

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

src/mkdocstrings_handlers/python/handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ def update_env(self, md: Markdown, config: dict) -> None: # noqa: D102 (ignore
352352
self.env.filters["order_members"] = rendering.do_order_members
353353
self.env.filters["format_code"] = rendering.do_format_code
354354
self.env.filters["format_signature"] = rendering.do_format_signature
355+
self.env.filters["format_attribute"] = rendering.do_format_attribute
355356
self.env.filters["filter_objects"] = rendering.do_filter_objects
356357
self.env.filters["stash_crossref"] = lambda ref, length: ref
357358
self.env.filters["get_template"] = rendering.do_get_template

src/mkdocstrings_handlers/python/rendering.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from mkdocstrings.loggers import get_logger
1515

1616
if TYPE_CHECKING:
17-
from griffe.dataclasses import Alias, Function, Object
17+
from griffe.dataclasses import Alias, Attribute, Function, Object
1818
from jinja2.runtime import Context
1919
from mkdocstrings.handlers.base import CollectorItem
2020

@@ -107,6 +107,45 @@ def do_format_signature(
107107
return str(env.filters["highlight"](signature, language="python", inline=False))
108108

109109

110+
@pass_context
111+
def do_format_attribute(
112+
context: Context,
113+
attribute_path: Markup,
114+
attribute: Attribute,
115+
line_length: int,
116+
*,
117+
crossrefs: bool = False, # noqa: ARG001
118+
) -> str:
119+
"""Format an attribute using Black.
120+
121+
Parameters:
122+
attribute_path: The path of the callable we render the signature of.
123+
attribute: The attribute we render the signature of.
124+
line_length: The line length to give to Black.
125+
crossrefs: Whether to cross-reference types in the signature.
126+
127+
Returns:
128+
The same code, formatted.
129+
"""
130+
env = context.environment
131+
annotations = context.parent["config"]["show_signature_annotations"]
132+
133+
signature = str(attribute_path).strip()
134+
if annotations and attribute.annotation:
135+
signature += f": {attribute.annotation}"
136+
if attribute.value:
137+
signature += f" = {attribute.value}"
138+
139+
signature = do_format_code(signature, line_length)
140+
return str(
141+
env.filters["highlight"](
142+
Markup.escape(signature),
143+
language="python",
144+
inline=False,
145+
),
146+
)
147+
148+
110149
def do_order_members(
111150
members: Sequence[Object | Alias],
112151
order: Order,

src/mkdocstrings_handlers/python/templates/material/_base/attribute.html

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@
4444

4545
{% block signature scoped %}
4646
{% if config.separate_signature %}
47-
{% filter highlight(language="python", inline=False) %}
48-
{% filter format_code(config.line_length) %}
49-
{{ attribute.name }}{% if attribute.annotation %}: {{ attribute.annotation|safe }}{% endif %}
50-
{% if attribute.value %} = {{ attribute.value|safe }}{% endif %}
51-
{% endfilter %}
47+
{% filter format_attribute(attribute, config.line_length, crossrefs=config.signature_crossrefs) %}
48+
{{ attribute.name }}
5249
{% endfilter %}
5350
{% endif %}
5451
{% endblock signature %}

0 commit comments

Comments
 (0)