databases icon indicating copy to clipboard operation
databases copied to clipboard

Ensure __getattr__ matches __getitem__'s behaviour

Open circlingthesun opened this issue 2 years ago • 1 comments

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.

circlingthesun avatar Aug 15 '22 10:08 circlingthesun

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

JGoutin avatar Sep 22 '22 16:09 JGoutin