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
When using BaseModel.model_validate_json() or BaseModel.model_validate() the optional parameter "context" is typed to be a dict[str, Any].
This context is made available to validators via the ValidationInfo object.
The type of the info.context object is "Any | None" instead of "dict[str,Any] | None".
Example Code
fromtypingimportAnyfrompydanticimportBaseModel, model_validatorfrompydantic_core.core_schemaimportValidationInfoclassModel(BaseModel):
@model_validator(mode="after")defvalidator(self, info: ValidationInfo) ->Any:
info.context# this returns a object of type "Any | None"Model.model_validate_json("{}", context={}) # type of context has to be "dict[str, Any] | None"
Indeed, looks like our type hints could be improved. Would you be interested in opening a PR to fix this issue? Thanks for bringing it to our attention!
Can we keep ValidationInfo's type annotations for context instead of Model.model_validate[_json] please? I.e. change the latter to Any | None rather than the former to a dict?
The change to Model.model_validate[_json] must have been pretty recent - I have code written as far back as Sep 2023 that passes in objects (not dicts) to validation code and a system of strongly typed context objects. It would be a massive pita to convert them to dicts and I'd lose some nice static type safety.
Initial Checks
Description
When using BaseModel.model_validate_json() or BaseModel.model_validate() the optional parameter "context" is typed to be a dict[str, Any].
This context is made available to validators via the ValidationInfo object.
The type of the info.context object is "Any | None" instead of "dict[str,Any] | None".
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: