python-frozendict icon indicating copy to clipboard operation
python-frozendict copied to clipboard

Nested dicts are not supported

Open raul-openai opened this issue 4 years ago • 2 comments
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.

raul-openai avatar Jan 06 '21 00:01 raul-openai

I guess we can just deepcopy the dict.

frozendict(copy.deepcopy(orig_top))

raul-openai avatar Jan 06 '21 00:01 raul-openai

It would still probably be worth providing that functionality in the constructor (eg: bool flag), since this can be an error prone default.

raul-openai avatar Jan 06 '21 00:01 raul-openai