Skip to content

Commit 984ef31

Browse files
committed
fixup! refactor: Use dataclasses for configuration/options and automate schema generation
1 parent 656edc9 commit 984ef31

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/mkdocstrings_handlers/python/config.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,9 @@ class PythonInputConfig:
951951
] = field(default_factory=lambda: ["."])
952952

953953
load_external_modules: Annotated[
954-
bool,
954+
bool | None,
955955
Field(description="Whether to always load external modules/packages."),
956-
] = False
956+
] = None
957957

958958
options: Annotated[
959959
PythonInputOptions,
@@ -965,10 +965,31 @@ class PythonInputConfig:
965965
Field(description="The locale to use when translating template strings."),
966966
] = None
967967

968+
@classmethod
969+
def coerce(cls, **data: Any) -> MutableMapping[str, Any]:
970+
"""Coerce data."""
971+
return data
972+
973+
@classmethod
974+
def from_data(cls, **data: Any) -> Self:
975+
"""Create an instance from a dictionary."""
976+
return cls(**cls.coerce(**data))
977+
968978

969979
# YORE: EOL 3.9: Replace `**_dataclass_options` with `frozen=True, kw_only=True` within line.
970980
@dataclass(**_dataclass_options) # type: ignore[call-overload]
971981
class PythonConfig(PythonInputConfig): # type: ignore[override,unused-ignore]
972982
"""Python handler configuration."""
973983

984+
inventories: list[Inventory] = field(default_factory=list)
974985
options: dict[str, Any] = field(default_factory=dict) # type: ignore[assignment]
986+
987+
@classmethod
988+
def coerce(cls, **data: Any) -> MutableMapping[str, Any]:
989+
"""Coerce data."""
990+
if "inventories" in data:
991+
data["inventories"] = [
992+
Inventory(url=inv) if isinstance(inv, str) else Inventory(**inv)
993+
for inv in data["inventories"]
994+
]
995+
return data

src/mkdocstrings_handlers/python/handler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def __init__(self, config: PythonConfig, base_dir: Path, **kwargs: Any) -> None:
138138

139139
def get_inventory_urls(self) -> list[tuple[str, dict[str, Any]]]:
140140
"""Return the URLs of the inventory files to download."""
141-
return [(inv, {}) if isinstance(inv, str) else (inv.url, inv._config) for inv in self.config.inventories]
141+
return [(inv.url, inv._config) for inv in self.config.inventories]
142142

143143
@staticmethod
144144
def load_inventory(
@@ -352,7 +352,7 @@ def get_handler(
352352
warn("The 'import' key is renamed 'inventories' for the Python handler", FutureWarning, stacklevel=1)
353353
handler_config["inventories"] = handler_config.pop("import", [])
354354
return PythonHandler(
355-
config=PythonConfig(**handler_config),
355+
config=PythonConfig.from_data(**handler_config),
356356
base_dir=base_dir,
357357
**kwargs,
358358
)

0 commit comments

Comments
 (0)