Skip to content

Commit 6cf34b9

Browse files
authored
feat: Support hiding attribute values
Issue-292: #292 PR-293: #293
1 parent d3b35e1 commit 6cf34b9

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

docs/usage/configuration/members.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,50 @@ def function_d():
519519
////
520520
///
521521

522+
523+
[](){#option-show_attribute_values}
524+
## `show_attribute_values`
525+
526+
- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }**
527+
<!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->
528+
529+
Show initial values of attributes in classes.
530+
531+
```yaml title="in mkdocs.yml (global configuration)"
532+
plugins:
533+
- mkdocstrings:
534+
handlers:
535+
python:
536+
options:
537+
show_attribute_values: true
538+
```
539+
540+
```md title="or in docs/some_page.md (local configuration)"
541+
::: path.to.object
542+
options:
543+
show_attribute_values: true
544+
```
545+
546+
```python title="package/module.py"
547+
class SomeClass:
548+
def __init__(self):
549+
self.some_attr = 1
550+
```
551+
552+
/// admonition | Preview
553+
type: preview
554+
555+
//// tab | With attribute values visible
556+
<h2><code>SomeClass</code></h2>
557+
<p>some_attr = 1</p>
558+
////
559+
560+
//// tab | With attribute values hidden
561+
<h2><code>SomeClass</code></h2>
562+
<p>some_attr</p>
563+
////
564+
///
565+
522566
[](){#option-show_submodules}
523567
## `show_submodules`
524568

src/mkdocstrings_handlers/python/_internal/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,14 @@ class PythonInputOptions:
622622
),
623623
] = False
624624

625+
show_attribute_values: Annotated[
626+
bool,
627+
_Field(
628+
group="members",
629+
description="Show initial values of attributes in classes.",
630+
),
631+
] = True
632+
625633
show_bases: Annotated[
626634
bool,
627635
_Field(

src/mkdocstrings_handlers/python/_internal/rendering.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ def do_format_attribute(
208208
line_length: int,
209209
*,
210210
crossrefs: bool = False, # noqa: ARG001
211+
show_value: bool = True,
211212
) -> str:
212213
"""Format an attribute.
213214
@@ -235,7 +236,7 @@ def do_format_attribute(
235236
backlink_type="returned-by",
236237
)
237238
signature += f": {annotation}"
238-
if attribute.value:
239+
if show_value and attribute.value:
239240
value = template.render(context.parent, expression=attribute.value, signature=True, backlink_type="used-by")
240241
signature += f" = {value}"
241242

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ Context:
5555
{% else %}
5656
{%+ filter highlight(language="python", inline=True) %}
5757
{{ attribute_name }}{% if attribute.annotation and config.show_signature_annotations %}: {{ attribute.annotation }}{% endif %}
58-
{% if attribute.value %} = {{ attribute.value }}{% endif %}
58+
{% if config.show_attribute_values and attribute.value %} = {{ attribute.value }}{% endif %}
5959
{% endfilter %}
6060
{% endif %}
6161
{% endblock heading %}
@@ -79,7 +79,7 @@ Context:
7979
This block renders the signature for the attribute.
8080
-#}
8181
{% if config.separate_signature %}
82-
{% filter format_attribute(attribute, config.line_length, crossrefs=config.signature_crossrefs) %}
82+
{% filter format_attribute(attribute, config.line_length, crossrefs=config.signature_crossrefs, show_value=config.show_attribute_values) %}
8383
{{ attribute.name }}
8484
{% endfilter %}
8585
{% endif %}

0 commit comments

Comments
 (0)