Skip to content
Merged
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
44 changes: 44 additions & 0 deletions docs/usage/configuration/members.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" }**
<!-- - **:octicons-project-template-24: Template :material-null:** (contained in [`class.html`][class template]) -->

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
<h2><code>SomeClass</code></h2>
<p>some_attr = 1</p>
////

//// tab | With attribute values hidden
<h2><code>SomeClass</code></h2>
<p>some_attr</p>
////
///

[](){#option-show_submodules}
## `show_submodules`

Expand Down
8 changes: 8 additions & 0 deletions src/mkdocstrings_handlers/python/_internal/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/mkdocstrings_handlers/python/_internal/rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def do_format_attribute(
line_length: int,
*,
crossrefs: bool = False, # noqa: ARG001
show_value: bool = True,
) -> str:
"""Format an attribute.

Expand Down Expand Up @@ -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}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand All @@ -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 %}
Expand Down
Loading