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

Why is Django ninja silencing AttributeError in resolve_* methods? #1412

Open
flaiming opened this issue Feb 21, 2025 · 2 comments
Open

Why is Django ninja silencing AttributeError in resolve_* methods? #1412

flaiming opened this issue Feb 21, 2025 · 2 comments

Comments

@flaiming
Copy link

Let's consider this example:

class BookOut(Scheme):
    authors: list[str] = list()

    @staticmethod
    def resolve_authors(obj):
        authors = []
        for author in obj.authors.all():
           # this will cause AttributeError
            autrhos.append(author.nonexisting_attribute)
        return authors

When there is AttributeError in the resolver AND the authors fields has default value, the error will be silenced and there will be empty list in result.authors. Why is that? Personally I would like to know if there are any errors occurring in resolve_* method, disregarding if the field has default or not.

@vitalik
Copy link
Owner

vitalik commented Feb 21, 2025

@flaiming

well that actually happens on pydantic side - it treats attribute error as missing value:

from pydantic import BaseModel


class Some(BaseModel):
    model_config = {'from_attributes': True}
    a: int | None = None


class SomeObject:
    @property
    def a(self):
        raise AttributeError('hack')


obj = SomeObject()
print(Some.model_validate(obj))

# outputs `a=None`

@flaiming
Copy link
Author

@vitalik Thanks for answer. So, is there some way how to tell Pydantic to not do that?

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

No branches or pull requests

2 participants