Skip to content

Commit

Permalink
fix merge property in rare cases
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiyuan Chen <[email protected]>
  • Loading branch information
ZhiyuanChen committed Dec 29, 2023
1 parent 81420ad commit 2d4d8e8
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions chanfig/nested_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,7 @@ def _merge(this: FlatDict, that: Iterable, overwrite: bool = True) -> Mapping:
return this
if isinstance(that, Mapping):
that = that.items()
context = this.converting() if isinstance(this, NestedDict) else nullcontext()
with context:
with this.converting() if isinstance(this, NestedDict) else nullcontext():
for key, value in that:
if key in this and isinstance(this[key], Mapping):
if isinstance(value, Mapping):
Expand All @@ -709,13 +708,21 @@ def _merge(this: FlatDict, that: Iterable, overwrite: bool = True) -> Mapping:
this.set(key, value)
else:
this[key] = value
elif key in dir(this) and isinstance(getattr(this.__class__, key), (property, cached_property)):
getattr(this, key).merge(value, overwrite=overwrite)
elif key in dir(this) and isinstance(getattr(this.__class__, key, None), (property, cached_property)):
if isinstance(getattr(this, key, None), FlatDict):
getattr(this, key).merge(value, overwrite=overwrite)
else:
setattr(this, key, value)
elif overwrite or key not in this:
if isinstance(this, NestedDict):
this.set(key, value)
else:
this[key] = value
else:
raise ValueError(
f"""{key} is not merged.
This is likely to be a bug, please report at https://github.com/ZhiyuanChen/CHANfiG/issues."""
)
return this

def intersect(self, other: Mapping | Iterable | PathStr, recursive: bool = True) -> Self: # pylint: disable=W0221
Expand Down

0 comments on commit 2d4d8e8

Please sign in to comment.