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
Pydantic dataclasses cannot be used as Annotated Validators. The TypeError happens at class the definition.
I wanted to replicate the example of the section As a method on a custom type of the "Types" part of the Concepts but instead of using Python dataclasses, I wanted to use Pydantic dataclasses (to add validation).
I am not sure about all the internals but it looks like some conflicts about the __get_pydantic_core_schema__ that needs to be implemented for the class to be an Annotated Validator and the __get_pydantic_core_schema__ that is used for the definition.
I've read in the Pydantic website that [...] you should try to stick to the built-in constructs like those provided by annotated-types, pydantic.Field, or BeforeValidator and so on., so sorry if this is somehow already known and I should just avoid using Pydantic dataclasses in this specific context.
Example Code
importdataclassesfromtypingimportAnnotated, AnyimportpydanticfrompydanticimportGetCoreSchemaHandler, PositiveFloat, TypeAdapterfrompydantic_coreimportCoreSchema, core_schema@dataclasses.dataclassclassPythonDataclassValidator:
exponent: PositiveFloatdef__call__(self, value):
returnvalue**self.exponentdef__get_pydantic_core_schema__(
self, source_type: Any, handler: GetCoreSchemaHandler
) ->CoreSchema:
"""Return the Pydantic core schema for the custom after validator."""returncore_schema.no_info_after_validator_function(self, handler(source_type))
### Error will happen at the next code line, during the class definition### TypeError: PydanticDataclassValidator.__get_pydantic_core_schema__() missing 1 required positional argument: 'handler'@pydantic.dataclasses.dataclassclassPydanticDataclassValidator:
exponent: PositiveFloatdef__call__(self, value):
returnvalue**self.exponentdef__get_pydantic_core_schema__(
self, source_type: Any, handler: GetCoreSchemaHandler
) ->CoreSchema:
"""Return the Pydantic core schema for the custom after validator."""returncore_schema.no_info_after_validator_function(self, handler(source_type))
# This works as expected if I remove the whole code of the class PydanticDataclassValidator (this similar to the example given in Pydantic's website)PythonType=Annotated[float, PythonDataclassValidator(2)]
print(TypeAdapter(PythonType).validate_python(2) ==4)
PydanticType=Annotated[float, PydanticDataclassValidator(2)]
print(TypeAdapter(PydanticType).validate_python(2) ==4)
Thanks for the question! Indeed, looks like a conflict on the __get_pydantic_core_schema__ front. I think this should be possible, but will definitely take some modification on the internal core schema building front.
I'll take a closer look at this for 2.8 and see if we can come up with a workaround for you!
Initial Checks
Description
Pydantic dataclasses cannot be used as Annotated Validators. The TypeError happens at class the definition.
I wanted to replicate the example of the section As a method on a custom type of the "Types" part of the Concepts but instead of using Python dataclasses, I wanted to use Pydantic dataclasses (to add validation).
I am not sure about all the internals but it looks like some conflicts about the
__get_pydantic_core_schema__
that needs to be implemented for the class to be an Annotated Validator and the__get_pydantic_core_schema__
that is used for the definition.I've read in the Pydantic website that
[...] you should try to stick to the built-in constructs like those provided by annotated-types, pydantic.Field, or BeforeValidator and so on.
, so sorry if this is somehow already known and I should just avoid using Pydantic dataclasses in this specific context.Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: