gmr / flatdict

Python module for interacting with nested dicts as a single level dict with delimited keys.
https://flatdict.readthedocs.io
BSD 3-Clause "New" or "Revised" License
111 stars 32 forks source link

Behaviour of flatdict.setdefault is incorrect #31

Closed abmyii closed 5 years ago

abmyii commented 5 years ago

With standard dicts, setdefault doesn't modify an existing key which is False/None (satisfies the condition of not x), whereas a flatdict will:

import flatdict
example = {'test': False}
example.setdefault('test', None)
print(example) # 'test' will still be False

example = flatdict.FlatDict(example)
example.setdefault('test', None)
print(example) # 'test' will now be None

The culprit line is https://github.com/gmr/flatdict/blob/88c043438a174eb82aa7d688fe5af04bc647a72a/flatdict.py#L318 (specifically the or not self.__getitem__(key) part).

abmyii commented 5 years ago

Submitted PR (#32) to fix this issue.