-
Hello, I'm getting this error:
for this code: from datetime import timedelta
from pydantic import BaseModel, Field
class BaseSession(BaseModel):
expire_time: timedelta = timedelta(hours=3)
class CustomSession(BaseSession):
expire_time: timedelta = Field(timedelta(hours=3), alias="expireTime")
# ^^^^^^^^^ missing a default value Is it expected, or is it a bug in Pydantic or Pyright? Isn't Field a default value here? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is expected behavior. It's neither a bug in pydantic nor in pyright. Pydantic provides a non-standard way to specify a default value via a positional argument, as you've done in your code sample. Field specifiers in stdlib Standards-compliant type checkers like pyright do not know about pydantics idiosyncrasies. Their behavior is based on the dataclass_transform mechanism which assumes standard dataclass behaviors. Pydantic also supports the standard dataclass behavior, so if you change your code to the following, it will satisfy both pydantic and standards-based type checkers like pyright. class CustomSession(BaseSession):
expire_time: timedelta = Field(default=timedelta(hours=3), alias="expireTime") |
Beta Was this translation helpful? Give feedback.
This is expected behavior. It's neither a bug in pydantic nor in pyright.
Pydantic provides a non-standard way to specify a default value via a positional argument, as you've done in your code sample. Field specifiers in stdlib
dataclass
do not support this; they require you to specify the default by keyword argumentdefault=
.Standards-compliant type checkers like pyright do not know about pydantics idiosyncrasies. Their behavior is based on the dataclass_transform mechanism which assumes standard dataclass behaviors.
Pydantic also supports the standard dataclass behavior, so if you change your code to the following, it will satisfy both pydantic and standards-based type checkers like py…