Skip to content

Commit bee7f4e

Browse files
committed
feat: Fine-grain configuration
This change allows users to specify recursive options under `members`: ``` ::: some.module options: members: - name: SomeName options: heading: Some Verbose Name members: - ... ``` This change also brings a refactor in how we combine default, global and local configuration. Instead of keeping track of dictionaries to merge them accordingly, we now chain dataclasses together, using a special `UNSET` value to tell whether an option was set or not. Checking against this `UNSET` value lets the chained dataclass know whether it should try to fetch the value from previous (left/up) dataclasses. With the following example chain: ``` { some_option: UNSET } -> { some_option: False } -> { some_option: UNSET } ``` ...trying to get `some_option` from the chain would start with the right-most dataclass, where `some_option` is `UNSET`. Then it would continue on the left, and return `False` since it's not `UNSET`. The left-most dataclass would not be checked since the value was already returned. This change lets us simplify templates, as we don't have to check if we're rendering the root object anymore: the configuration options are chained in a way that prevent options that are only relevant to the root object to be propagated further down to members. Issue-658: mkdocstrings/mkdocstrings#658
1 parent 8428783 commit bee7f4e

File tree

15 files changed

+504
-327
lines changed

15 files changed

+504
-327
lines changed

scripts/griffe_extensions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
class CustomFields(griffe.Extension):
1414
"""Support our custom dataclass fields."""
1515

16+
_custom_field_path = "mkdocstrings_handlers.python._internal.config._Field"
17+
1618
def on_attribute_instance(
1719
self,
1820
*,
@@ -24,11 +26,11 @@ def on_attribute_instance(
2426
if attr.docstring:
2527
return
2628
try:
27-
field: griffe.ExprCall = attr.annotation.slice.elements[1] # type: ignore[union-attr]
29+
field = attr.annotation.slice.elements[1] # type: ignore[union-attr]
2830
except AttributeError:
2931
return
3032

31-
if field.canonical_path == "mkdocstrings_handlers.python._internal.config._Field":
33+
if isinstance(field, griffe.ExprCall) and field.canonical_path == self._custom_field_path:
3234
description = next(
3335
attr.value
3436
for attr in field.arguments

src/mkdocstrings_handlers/python/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
"""Python handler for mkdocstrings."""
22

33
from mkdocstrings_handlers.python._internal.config import (
4+
UNSET,
45
AutoStyleOptions,
6+
ChainedOptions,
57
GoogleStyleOptions,
68
Inventory,
9+
MembersOption,
710
NumpyStyleOptions,
811
PerStyleOptions,
912
PythonConfig,
1013
PythonInputConfig,
1114
PythonInputOptions,
1215
PythonOptions,
16+
RecursiveOptions,
1317
SphinxStyleOptions,
1418
SummaryOption,
19+
Unset,
1520
)
1621
from mkdocstrings_handlers.python._internal.handler import PythonHandler, get_handler
1722
from mkdocstrings_handlers.python._internal.rendering import (
@@ -34,10 +39,13 @@
3439
)
3540

3641
__all__ = [
42+
"UNSET",
3743
"AutoStyleOptions",
3844
"AutorefsHook",
45+
"ChainedOptions",
3946
"GoogleStyleOptions",
4047
"Inventory",
48+
"MembersOption",
4149
"NumpyStyleOptions",
4250
"Order",
4351
"PerStyleOptions",
@@ -46,9 +54,11 @@
4654
"PythonInputConfig",
4755
"PythonInputOptions",
4856
"PythonOptions",
57+
"RecursiveOptions",
4958
"SphinxStyleOptions",
5059
"SummaryOption",
5160
"Tree",
61+
"Unset",
5262
"do_as_attributes_section",
5363
"do_as_classes_section",
5464
"do_as_functions_section",

0 commit comments

Comments
 (0)