Skip to content

Commit 477b9e4

Browse files
committed
fix: Let dataclass implement __init__ method, set extra fields in get_options
With our own `__init__` implementation, unpassed fields would never be set on the instance, triggering attribute errors. Let dataclass do its job, add set extra fields from outside of the class.
1 parent 40067f7 commit 477b9e4

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

src/mkdocstrings_handlers/python/config.py

-9
Original file line numberDiff line numberDiff line change
@@ -843,15 +843,6 @@ def _extract_extra(cls, data: dict[str, Any]) -> tuple[dict[str, Any], dict[str,
843843
copy = data.copy()
844844
return {name: copy.pop(name) for name in data if name not in field_names}, copy
845845

846-
# YORE: Bump 2: Remove block.
847-
def __init__(self, **kwargs: Any) -> None:
848-
"""Initialize the instance."""
849-
extra_fields = self._extract_extra(kwargs)
850-
for name, value in kwargs.items():
851-
object.__setattr__(self, name, value)
852-
if extra_fields:
853-
object.__setattr__(self, "_extra", extra_fields)
854-
855846
@classmethod
856847
def coerce(cls, **data: Any) -> MutableMapping[str, Any]:
857848
"""Coerce data."""

src/mkdocstrings_handlers/python/handler.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -186,12 +186,17 @@ def get_options(self, local_options: Mapping[str, Any]) -> HandlerOptions:
186186

187187
extra = {**self.global_options.get("extra", {}), **local_options.get("extra", {})}
188188
options = {**self.global_options, **local_options, "extra": extra}
189-
# YORE: Bump 2: Replace `, **unknown_extra` with `` within line.
190189
try:
191-
return PythonOptions.from_data(**options, **unknown_extra)
190+
# YORE: Bump 2: Replace `opts =` with `return` within line.
191+
opts = PythonOptions.from_data(**options)
192192
except Exception as error:
193193
raise PluginError(f"Invalid options: {error}") from error
194194

195+
# YORE: Bump 2: Remove block.
196+
for key, value in unknown_extra.items():
197+
object.__setattr__(opts, key, value)
198+
return opts
199+
195200
def collect(self, identifier: str, options: PythonOptions) -> CollectorItem: # noqa: D102
196201
module_name = identifier.split(".", 1)[0]
197202
unknown_module = module_name not in self._modules_collection

0 commit comments

Comments
 (0)