databases
databases copied to clipboard
Ensure __getattr__ matches __getitem__'s behaviour
The __getitem__
function does extra deserialisation that's not happening in getattr. As an example referencing a JSONB field with getitem gets you a dict
whereas __getattr__
results in a str
. This PR aligns their behaviour.
Also, the current __getattr__
returns None
when accessing to a non existing attribute instead of raising AttributeError
, like Python standard behavior, or SQLAlchemy Row
.
This can be also fixed in this PR by doing the following (Else KeyError
will be risen):
def __getattr__(self, name: str) -> typing.Any:
try:
return self.__getitem__(name)
except KeyError as e:
raise AttributeError(e.args[0]) from e