You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
All the __parent, __key and __missing__ implementation details lead to surprising behaviors, like this one :
In [1]: from addict import Dict
In [2]: d = Dict()
In [3]: a1 = d.a
In [4]: a2 = d.a
In [5]: a1.b = 1
In [6]: a1.c = 2
In [7]: d
Out[7]: {'a': {'b': 1, 'c': 2}}
In [8]: a2.d = 3
In [9]: d
Out[9]: {'a': {'d': 3}}
In [10]:
Since I really enjoy this library, I think it may be a good idea to start a discussion on how we could solve the problem.
I though about re-implementing __missing__ with a cache using weakref.WeakValueDictionary for "pending potential child dict". The Idea is to always return the same instance with __missing__, but keeping the current behavior to "really" add it only when a nested key is set (with __setitem__ on the child dict).
Proceeding this way would keep the current behavior of discarding the nested Dict if not used, but avoiding the strange behavior I exposed.
What do you think ? (since I am not a definitive expert with addict, I prefer check with you if there are any stuff I missed, or if there are future plans that would either make this impossible either solve the problem, before implementing it myself)
The text was updated successfully, but these errors were encountered:
All the
__parent
,__key
and__missing__
implementation details lead to surprising behaviors, like this one :Since I really enjoy this library, I think it may be a good idea to start a discussion on how we could solve the problem.
I though about re-implementing
__missing__
with a cache usingweakref.WeakValueDictionary
for "pending potential child dict". The Idea is to always return the same instance with__missing__
, but keeping the current behavior to "really" add it only when a nested key is set (with__setitem__
on the child dict).Proceeding this way would keep the current behavior of discarding the nested Dict if not used, but avoiding the strange behavior I exposed.
What do you think ? (since I am not a definitive expert with addict, I prefer check with you if there are any stuff I missed, or if there are future plans that would either make this impossible either solve the problem, before implementing it myself)
The text was updated successfully, but these errors were encountered: