Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict.__missing__ implementation leads to surprising behaviour #133

Open
hl037 opened this issue Dec 3, 2020 · 1 comment
Open

Dict.__missing__ implementation leads to surprising behaviour #133

hl037 opened this issue Dec 3, 2020 · 1 comment

Comments

@hl037
Copy link
Contributor

hl037 commented Dec 3, 2020

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)

@Salvakiya
Copy link

has there been any investigation into this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants