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
Annotating a field as ResponseModel[Any] (where ResponseModel is a generic BaseModel) fails to validate values of type ResponseModel and ResponseModel[Product]; it only validates values of ResponseModel[Any].
On the other hand annotating the field as ResponseModel validates all three cases, however mypy gives Missing type parameters for generic type "ResponseModel".
Questions:
Is the ResponseModel[Any] validation working as expected or it's a bug?
Is it safe to # type: ignore the second version that seems to be working?
Example Code
fromtypingimportAny, Generic, TypeVarfrompydanticimportBaseModel, ValidationErrorT=TypeVar("T")
classResponseModel(BaseModel, Generic[T]):
content: TclassProduct(BaseModel):
name: strprice: floatclassOrder(BaseModel):
id: intproduct: ResponseModel[Any]
# replacing the previous annotation with the following succeeds at runtime but fails type checking# product: ResponseModelproduct=Product(name="Apple", price=0.5)
response1: ResponseModel[Any] =ResponseModel[Any](content=product)
response2: ResponseModel[Any] =ResponseModel(content=product)
response3: ResponseModel[Any] =ResponseModel[Product](content=product)
forresponseinresponse1, response2, response3:
try:
order=Order(id=1, product=response)
print(f"{response!r} succeeded")
exceptValidationError:
print(f"{response!r} failed")
Thanks for your questions. This comment might help to add some context regarding some of the nuances with validation and parametrized generics: #8884 (comment).
The case with Any is interesting. This does seem a bit buggy to me. Specifically, this works:
fromtypingimportAny, Generic, TypeVarfrompydanticimportBaseModel, ValidationErrorT=TypeVar("T")
classResponseModel(BaseModel, Generic[T]):
content: TclassProduct(BaseModel):
name: strprice: floatclassOrder(BaseModel):
id: intproduct: ResponseModel[Any]
# replacing the previous annotation with the following succeeds at runtime but fails type checking# product: ResponseModelproduct=Product(name="Apple", price=0.5)
response1: ResponseModel[Any] =ResponseModel[Any](content=product)
response2: ResponseModel[Any] =ResponseModel(content=product)
response3: ResponseModel[Any] =ResponseModel[Product](content=product)
forresponseinresponse1, response2, response3:
try:
order=Order(id=1, product=response.model_dump())
print(f"{response!r} succeeded")
exceptValidationError:
print(f"{response!r} failed")
I'm going to chat with @dmontagu, our resident generics expert, about this and will get back to you!
Initial Checks
Description
Annotating a field as
ResponseModel[Any]
(whereResponseModel
is a generic BaseModel) fails to validate values of typeResponseModel
andResponseModel[Product]
; it only validates values ofResponseModel[Any]
.On the other hand annotating the field as
ResponseModel
validates all three cases, however mypy givesMissing type parameters for generic type "ResponseModel"
.Questions:
ResponseModel[Any]
validation working as expected or it's a bug?# type: ignore
the second version that seems to be working?Example Code
Output:
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: