Does Pydantic work with Pyre? ("Uninitialized attribute" when type checking BaseModel)

224 Views Asked by At

Problem:

I'm using Pydantic to structure my code, and Pyre (aka pyre-check) to type check. In the following sample, the code works and mypy doesn't complain but Pyre gives error:

Uninitialized attribute [13]: Attribute first is declared in class Name to have type str but is never initialized.

Code:

from __future__ import annotations
from pydantic import BaseModel

class Name(BaseModel):
    first: str

n = Name(first="test")

print(n)

Is Pyre incompatible with Pydantic, or is this user error on my part?

I understand Pyre wants to see the attribute initialized (for example first: str = "Bob") but setting such an equality would indicate to Pydantic that the field is optional (it's not).

Other solutions I've considered and discarded:

  • Pyre doesn't complain if I make Name a dataclass (but then I lose Pydantic features)
  • Adding Field to each attribute eg first: str = Field(..., alias="first_name") - this seems hacky and labor intensive (there are many such BaseModel classes in my code)

Thanks for your help!

0

There are 0 best solutions below