Skip to content

Commit

Permalink
fix conflicts when key name is items, keys, values, etc.
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, 2024
1 parent e16dcb2 commit 0ace091
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion chanfig/flat_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,11 @@ def __post_init__(self, *args, **kwargs) -> None:
pass

def __getattribute__(self, name: Any) -> Any:
if (name not in ("getattr",) and not (name.startswith("__") and name.endswith("__"))) and name in self:
if (
(name not in ("getattr",) and not (name.startswith("__") and name.endswith("__")))
and name not in dir(self.__class__)
and name in self
):
return self.get(name)
return super().__getattribute__(name)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_flat_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ def test_construct_namespace(self):
d = FlatDict(parser.parse_args(["--name", "chang", "--seed", "1013"]))
assert d.name == "chang"
assert d.seed == 1013

def test_conflicts(self):
d = FlatDict(keys=0, values=1, items=2)
p = {"keys": 0, "values": 1, "items": 2}
assert d["keys"] == 0
assert d["values"] == 1
assert d["items"] == 2
assert d.keys() == p.keys()
assert list(d.values()) == list(p.values()) # dict_values can't be compared directly
assert d.items() == p.items()

0 comments on commit 0ace091

Please sign in to comment.