Skip to content

Commit

Permalink
fix set/merge a cached_property
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiyuan Chen <[email protected]>
  • Loading branch information
ZhiyuanChen committed Jul 18, 2023
1 parent a81604e commit 82c3468
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions chanfig/nested_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
from os import PathLike
from typing import TYPE_CHECKING, Any, Callable, Iterable, Iterator, Mapping

try:
from functools import cached_property
except ImportError:
try:
from backports.cached_property import cached_property # type: ignore
except ImportError:
cached_property = property # type: ignore

from .default_dict import DefaultDict
from .flat_dict import FlatDict
from .utils import _K, _V, Null, PathStr
Expand Down Expand Up @@ -406,7 +414,7 @@ def set( # pylint: disable=W0221
try:
while isinstance(name, str) and delimiter in name:
name, rest = name.split(delimiter, 1)
if name in dir(self) and isinstance(getattr(self.__class__, name), property):
if name in dir(self) and isinstance(getattr(self.__class__, name), (property, cached_property)):
self, name = getattr(self, name), rest
elif name not in self and isinstance(self, Mapping):
default = (
Expand Down Expand Up @@ -562,7 +570,7 @@ def _merge(this: FlatDict, that: Iterable, overwrite: bool = True) -> Mapping:
this.set(key, value, convert_mapping=True)
elif overwrite:
this[key] = value
elif key in dir(this) and isinstance(getattr(this.__class__, key), property):
elif key in dir(this) and isinstance(getattr(this.__class__, key), (property, cached_property)):
getattr(this, key).merge(value, overwrite=overwrite)
elif overwrite or key not in this:
if isinstance(this, NestedDict):
Expand Down

0 comments on commit 82c3468

Please sign in to comment.