I define the following UserDict, that implements nested lookups (d['key.subkey']):
importcollectionsclassDotDict(collections.UserDict):
def__getitem__(self, key):
subitem=self.dataforsubkeyinkey.split("."):
try:
subitem=subitem[subkey]
exceptKeyError:
raiseKeyError(f"`{key}` not found in configuration") fromNonereturnsubitem
I expect the following to work as it did in Python 3.11, but it does not:
d=DotDict({'one': {'two': 'buckle my shoe'}})
d['one.two'] # worksd.get('one.two') # does not work
I do not see any release notes indicating that __getitem__ is no longer the correct method to override.
Activity
Eclips4 commented on Jun 8, 2023
This also reproduced on the current main.
Seems that in 3.12 and newer versions __getitem__ isn't called in .get
Eclips4 commented on Jun 8, 2023
Also, if you define a __contains__ with the same behavior, it will work on 3.12.
Introduced in #17910
After some research of this issue, I came to the conclusion that the best solution here would be to add documentation of that.
stefanv commented on Jun 28, 2023
IUUC, the proposed fix is to change the implementation to the following:
Not ideal, but it works.
dholth commented on Dec 11, 2023
This also affects me.