Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Ensure __getattr__ matches __getitem__'s behaviour #507

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

circlingthesun
Copy link
Contributor

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.

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.
@JGoutin
Copy link

JGoutin commented Sep 22, 2022

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants