Allow setting dictionary items in MagicDict by setting attributes
- Aug 23, 2021
-
-
Requires !124 (merged), closes #81 (closed)
A slightly annoying issue when writing tests for workers that should handle both Element and CachedElement: setting an attribute on a MagicDict does not set its item.
>>> e = Element()
>>> e.id = '…'
>>> e["id"]
KeyError: 'id'
Since CachedElement does not have key-based access (e["id"]
), if they set attributes on an Element, they might not be visible as dictionary keys. This changes the MagicDict behavior to just always set dictionary keys; the MagicDict's __getattr__
can retrieve those keys anyway, so it works transparently.