Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants