You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
or a validation error while instantiating Child or setting parent_base_model field. I tried setting various model_config options and they did not have any impact (incl. validate_assignment=True and c.parent_base_model=p)
The parent_base_model attribute is of type BaseModel, and since BaseModel does not include a data attribute, the data attribute was ignored in the validation. You can refer to the documentation on extra fields for more information: link.
If this use case is related to the extra fields behavior, then it's not obvious for me.
Did you mean that if I call Child(parent_base_model=p) with an instance of a subclass, only the fields of the predecessor are used during serialization and fields from the subclass are implicitly "extra" fields from this point of view?
Also, setting extra="forbid" and strict=True makes no difference.
from pydantic import *
class Parent(BaseModel):
model_config = ConfigDict(
extra="forbid",
strict=True,
)
data: str = Field(default="DEFAULT")
class Child(BaseModel):
model_config = ConfigDict(
extra="forbid",
strict=True,
)
parent: Parent | None = Field(default=None)
parent_base_model: BaseModel | None = Field(default=None)
p = Parent()
c = Child(parent=p, parent_base_model=p)
print(c.dict())
Initial Checks
Description
When I define a field with
BaseModel
type and assign an instance of a model that inherits fromBaseModel
, the value gets serialized as{}
.Actual result from the reproducer:
Expected result:
or a validation error while instantiating
Child
or settingparent_base_model
field. I tried setting variousmodel_config
options and they did not have any impact (incl.validate_assignment=True
andc.parent_base_model=p
)Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: