python-frozendict
python-frozendict copied to clipboard
Nested dicts are not supported
trafficstars
Can nested dictionaries be supported? Is this a bug or expected behavior to only support shallow dictionaries?
def test_frozen_dicts():
orig_sub = {'subkey': 'sA'}
orig_top = {'topkey': 'tA', 'child': orig_sub}
frozen = frozendict(orig_top)
print(f"before: {frozen}")
orig_top['topkey'] = 'tB'
orig_sub['subkey'] = 'sB'
print(f"after: {frozen}")
>>> test_frozen_dicts()
before: <frozendict {'topkey': 'tA', 'child': {'subkey': 'sA'}}>
after: <frozendict {'topkey': 'tA', 'child': {'subkey': 'sB'}}>
Expected: All nested dictionaries are also frozen.
Thanks.
I guess we can just deepcopy the dict.
frozendict(copy.deepcopy(orig_top))
It would still probably be worth providing that functionality in the constructor (eg: bool flag), since this can be an error prone default.