From 46fb931b62470c790040006d9d7f66b835542fba Mon Sep 17 00:00:00 2001 From: bswck Date: Wed, 9 Jul 2025 21:13:41 +0200 Subject: [PATCH] Support hiding attribute values --- docs/usage/configuration/members.md | 44 +++++++++++++++++++ .../python/_internal/config.py | 8 ++++ .../python/_internal/rendering.py | 3 +- .../material/_base/attribute.html.jinja | 4 +- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/docs/usage/configuration/members.md b/docs/usage/configuration/members.md index 7a5069a1..4afdd310 100644 --- a/docs/usage/configuration/members.md +++ b/docs/usage/configuration/members.md @@ -519,6 +519,50 @@ def function_d(): //// /// + +[](){#option-show_attribute_values} +## `show_attribute_values` + +- **:octicons-package-24: Type [`bool`][] :material-equal: `True`{ title="default value" }** + + +Show initial values of attributes in classes. + +```yaml title="in mkdocs.yml (global configuration)" +plugins: +- mkdocstrings: + handlers: + python: + options: + show_attribute_values: true +``` + +```md title="or in docs/some_page.md (local configuration)" +::: path.to.object + options: + show_attribute_values: true +``` + +```python title="package/module.py" +class SomeClass: + def __init__(self): + self.some_attr = 1 +``` + +/// admonition | Preview + type: preview + +//// tab | With attribute values visible +

SomeClass

+

some_attr = 1

+//// + +//// tab | With attribute values hidden +

SomeClass

+

some_attr

+//// +/// + [](){#option-show_submodules} ## `show_submodules` diff --git a/src/mkdocstrings_handlers/python/_internal/config.py b/src/mkdocstrings_handlers/python/_internal/config.py index 210f8fe2..6f8132e6 100644 --- a/src/mkdocstrings_handlers/python/_internal/config.py +++ b/src/mkdocstrings_handlers/python/_internal/config.py @@ -614,6 +614,14 @@ class PythonInputOptions: ), ] = False + show_attribute_values: Annotated[ + bool, + _Field( + group="members", + description="Show initial values of attributes in classes.", + ), + ] = True + show_bases: Annotated[ bool, _Field( diff --git a/src/mkdocstrings_handlers/python/_internal/rendering.py b/src/mkdocstrings_handlers/python/_internal/rendering.py index ce701114..70eacb36 100644 --- a/src/mkdocstrings_handlers/python/_internal/rendering.py +++ b/src/mkdocstrings_handlers/python/_internal/rendering.py @@ -208,6 +208,7 @@ def do_format_attribute( line_length: int, *, crossrefs: bool = False, # noqa: ARG001 + show_value: bool = True, ) -> str: """Format an attribute. @@ -235,7 +236,7 @@ def do_format_attribute( backlink_type="returned-by", ) signature += f": {annotation}" - if attribute.value: + if show_value and attribute.value: value = template.render(context.parent, expression=attribute.value, signature=True, backlink_type="used-by") signature += f" = {value}" diff --git a/src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja b/src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja index 519590e5..5832c8bd 100644 --- a/src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja +++ b/src/mkdocstrings_handlers/python/templates/material/_base/attribute.html.jinja @@ -55,7 +55,7 @@ Context: {% else %} {%+ filter highlight(language="python", inline=True) %} {{ attribute_name }}{% if attribute.annotation and config.show_signature_annotations %}: {{ attribute.annotation }}{% endif %} - {% if attribute.value %} = {{ attribute.value }}{% endif %} + {% if config.show_attribute_values and attribute.value %} = {{ attribute.value }}{% endif %} {% endfilter %} {% endif %} {% endblock heading %} @@ -79,7 +79,7 @@ Context: This block renders the signature for the attribute. -#} {% if config.separate_signature %} - {% filter format_attribute(attribute, config.line_length, crossrefs=config.signature_crossrefs) %} + {% filter format_attribute(attribute, config.line_length, crossrefs=config.signature_crossrefs, show_value=config.show_attribute_values) %} {{ attribute.name }} {% endfilter %} {% endif %}