Skip to content

No exception for missing attributes with __getattr__ #6251

@samuelcolvin

Description

@samuelcolvin

(Apologies if this has already been answered somewhere else, I've looked but couldn't find a duplicate, it's kind of the opposite of #520)

I'm trying to get pydantic to work better with mypy. This is taken from pydantic/pydantic#245.

class Model(BaseModel):
    age: int

m = Model(age=5)
print(m.foobar)

This code obviously raises an AttributeError with python, but mypy doesn't complain since as per #520 the __getattr__ method is defined so mypy can't know if m.foobar exists.

Is there any way to get mypy to fail in a case like this?

The only thing I can think of is to define BaseModel differently based on TYPE_CHECKING so __getattr__ isn't defined in that case. This that the best/only way to go or is there some magic comment or annotation I can add to get mypy to ignore __getattr__?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions