|
29 | 29 |
|
30 | 30 | from .utils import PydanticTransformer, ctx_node, WorkaroundKeyError
|
31 | 31 |
|
32 |
| -from typing import overload |
| 32 | +from typing import overload, TYPE_CHECKING |
| 33 | + |
33 | 34 |
|
34 | 35 | _log = logging.getLogger(__name__)
|
35 | 36 |
|
| 37 | +if TYPE_CHECKING: |
| 38 | + from pydantic import BaseModel |
| 39 | + |
36 | 40 |
|
37 | 41 | def _auto_package(mod: dc.Module) -> list[Section]:
|
38 | 42 | """Create default sections for the given package."""
|
@@ -86,13 +90,24 @@ def _is_external_alias(obj: dc.Alias | dc.Object, mod: dc.Module):
|
86 | 90 | return False
|
87 | 91 |
|
88 | 92 |
|
89 |
| -def _to_simple_dict(el): |
| 93 | +def _to_simple_dict(el: "BaseModel"): |
90 | 94 | # round-trip to json, so we can take advantage of pydantic
|
91 | 95 | # dumping Enums, etc.. There may be a simple way to do
|
92 | 96 | # this in pydantic v2.
|
93 | 97 | return json.loads(el.json(exclude_unset=True))
|
94 | 98 |
|
95 | 99 |
|
| 100 | +def _non_default_entries(el: "BaseModel"): |
| 101 | + field_defaults = {mf.name: mf.default for mf in el.__fields__.values()} |
| 102 | + set_fields = [ |
| 103 | + k for k, v in el if field_defaults[k] is not v if not isinstance(v, MISSING) |
| 104 | + ] |
| 105 | + |
| 106 | + d = el.dict() |
| 107 | + |
| 108 | + return {k: d[k] for k in set_fields} |
| 109 | + |
| 110 | + |
96 | 111 | class BlueprintTransformer(PydanticTransformer):
|
97 | 112 | def __init__(self, get_object=None, parser="numpy"):
|
98 | 113 |
|
@@ -232,7 +247,9 @@ def enter(self, el: Auto):
|
232 | 247 | # auto default overrides
|
233 | 248 | if self.options is not None:
|
234 | 249 | # TODO: is this round-tripping guaranteed by pydantic?
|
235 |
| - el = el.__class__(**{**self.options.dict(), **el.dict()}) |
| 250 | + _option_dict = _non_default_entries(self.options) |
| 251 | + _el_dict = _non_default_entries(el) |
| 252 | + el = el.__class__(**{**_option_dict, **_el_dict}) |
236 | 253 |
|
237 | 254 | # fetching object ----
|
238 | 255 | _log.info(f"Getting object for {path}")
|
|
0 commit comments