Skip to content

Commit

Permalink
fix to_dict does not traverse through dict container
Browse files Browse the repository at this point in the history
Signed-off-by: Zhiyuan Chen <[email protected]>
  • Loading branch information
ZhiyuanChen committed May 20, 2023
1 parent 8e4c061 commit 87a9585
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions chanfig/flat_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def to_dict(obj: Any) -> Mapping[str, Any]: # pylint: disable=R0911
r"""
Convert an object to a dict.
Note that when converting a `set` object, it may be converted to a `tuple` object if its values is not hashable.
Args:
obj: Object to be converted.
Expand All @@ -76,16 +78,21 @@ def to_dict(obj: Any) -> Mapping[str, Any]: # pylint: disable=R0911
1
>>> to_dict(FlatDict(a=[[[[[FlatDict(b=1)]]]]]))
{'a': [[[[[{'b': 1}]]]]]}
>>> to_dict(FlatDict(a={FlatDict(b=1)}))
{'a': ({'b': 1},)}
"""

if isinstance(obj, FlatDict):
if isinstance(obj, Mapping):
return {k: to_dict(v) for k, v in obj.items()}
if isinstance(obj, list):
return [to_dict(v) for v in obj] # type: ignore
if isinstance(obj, tuple):
return tuple(to_dict(v) for v in obj) # type: ignore
if isinstance(obj, set):
return {to_dict(v) for v in obj} # type: ignore
try:
return set(to_dict(v) for v in obj) # type: ignore
except TypeError:
return tuple(to_dict(v) for v in obj) # type: ignore
if isinstance(obj, Variable):
return obj.value
return obj
Expand Down

0 comments on commit 87a9585

Please sign in to comment.