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
In Pydantic, we prevent direct BaseModel instantiation:
BaseModel()
#> pydantic.errors.PydanticUserError: Pydantic models should inherit from BaseModel, BaseModel cannot be instantiated directly
However, as pydantic-core uses __new__ to create model instances, the following will unexpectedly work:
frompydanticimportBaseModelclassModel(BaseModel):
f: BaseModelm=Model.model_validate({'f': {}})
# m is successfully created. However, because `m.f` is an instance of `BaseModel`, it blows up e.g. when printing the instanceprint(m)
#> AttributeError: 'BaseModel' object has no attribute '__private_attributes__'. Did you mean: '__static_attributes__'?
I'm not sure if a validation error should be used or a different exception (ValueError?), but we should raise something and add hints about why this is happening (most of the time, users expected some BaseModel subclass to actually be used but this isn't possible when validating from an arbitrary mapping — see pydantic/pydantic#11597 (comment)).
The text was updated successfully, but these errors were encountered:
In Pydantic, we prevent direct
BaseModel
instantiation:However, as
pydantic-core
uses__new__
to create model instances, the following will unexpectedly work:I'm not sure if a validation error should be used or a different exception (
ValueError
?), but we should raise something and add hints about why this is happening (most of the time, users expected someBaseModel
subclass to actually be used but this isn't possible when validating from an arbitrary mapping — see pydantic/pydantic#11597 (comment)).The text was updated successfully, but these errors were encountered: