-
Notifications
You must be signed in to change notification settings - Fork 0
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
Convert assigned mapping objects to Config #6
Comments
Note and some thoughts for myself/maintainers in the bright future ... So, it turns out, First try in implementation of this ticketHere is how the code looked when def __getitem__(self, item):
if isinstance(item, str) and "." in item:
key, subkey = item.split(".", maxsplit=1)
return dict.__getitem__(self, key).__getitem__(subkey)
else:
return dict.__getitem__(self, item)
def __getattr__(self, attr):
return dict.__getitem__(self, attr)
def __setitem__(self, item, value):
value = self._parse(value) # newly added to implement this ticket
if isinstance(item, str) and "." in item:
key, subkey = item.split(".", maxsplit=1)
dict.__getitem__(self, key).__setitem__(subkey, value)
else:
dict.__setitem__(self, item, value)
def __setattr__(self, attr, value):
value = self._parse(value) # newly added to implement this ticket
dict.__setitem__(self, attr, value) I've added the cfg = Config()
cfg["f2"]["g3"]["h3"] = 123
assert cfg["f2.g3.h3"] == 123 DebuggingWithout newly added line it all works as it did before this ticket. With the value parsing it stops working. I tried to debug this for a few hours with no luck. Here is what happens during assignment of
HypothesisAt this point I started to suspect that there is something about the empty generated Now, what if, CPython uses this reference inside this new object generated with factory to later properly upsert it into the parent Obviously, when I temper with the I tried to look into defaultdict implementation in CPython to understand its behavior but that's beyond my capabilities 😅 What if, instead, we base the And, surprise surprise, it works 🎉 Updated codeHere is how it looks after. Notice the def __getitem__(self, item):
if isinstance(item, str) and "." in item:
key, subkey = item.split(".", maxsplit=1)
return self._dd_getitem(key).__getitem__(subkey)
else:
return self._dd_getitem(item)
def __getattr__(self, attr):
return self._dd_getitem(attr)
def __setitem__(self, item, value):
value = self._parse(value)
if isinstance(item, str) and "." in item:
key, subkey = item.split(".", maxsplit=1)
self._dd_getitem(key).__setitem__(subkey, value)
else:
dict.__setitem__(self, item, value)
def __setattr__(self, attr, value):
value = self._parse(value)
dict.__setitem__(self, attr, value)
def _dd_getitem(self, item):
if item not in self:
dict.__setitem__(self, item, Config())
return dict.__getitem__(self, item) |
When assigning values to existing or new keys, do we miss converting them to Config object? We don't miss it when we use
merge
method.The dictionary should be converted to
Config
object upon assignment.The text was updated successfully, but these errors were encountered: