Closed brianmaissy closed 5 years ago
That's definitely undesired behavior, and the suggested fix sounds appropriate, a PR would be welcome.
This does point out a general shortcoming in __setitem__
though, where it doesn't simple set in all cases, sometimes storing a copy instead, causing pretty much the same unexpected behavior as you highlighted with setdefault
:
>>> outer = TrackedDict()
>>> inner = {}
>>> outer['inner'] = inner
>>> inner['foo'] = 'bar'
>>> outer
{'inner': {}}
Ideas on how to deal with this inconsistency would also be appreciated.
In the following code:
the
__setitem__
will convert the dict to a TrackedDict, butsetdefault
will still return the original dict, so operations onfoo
will not be saved to the TrackedDict.I'd be happy to open a PR with a fix (just override setdefault in TrackedDict to convert the
default
parameter and then callsuper().setdefault
)