()
| 165 | |
| 166 | |
| 167 | def test_on_validate_python_on_error() -> None: |
| 168 | class CustomOnValidatePython(ValidatePythonHandlerProtocol): |
| 169 | def on_enter( |
| 170 | self, |
| 171 | input: Any, |
| 172 | *, |
| 173 | strict: bool | None = None, |
| 174 | extra: ExtraValues | None = None, |
| 175 | from_attributes: bool | None = None, |
| 176 | context: Any | None = None, |
| 177 | self_instance: Any | None = None, |
| 178 | by_alias: bool | None = None, |
| 179 | by_name: bool | None = None, |
| 180 | ) -> None: |
| 181 | assert input == {'a': 'potato'} |
| 182 | assert strict is None |
| 183 | assert extra is None |
| 184 | assert context is None |
| 185 | assert self_instance is None |
| 186 | |
| 187 | def on_error(self, error: ValidationError) -> None: |
| 188 | assert error.title == 'Model' |
| 189 | assert error.errors(include_url=False) == [ |
| 190 | { |
| 191 | 'input': 'potato', |
| 192 | 'loc': ('a',), |
| 193 | 'msg': 'Input should be a valid integer, unable to parse string as an integer', |
| 194 | 'type': 'int_parsing', |
| 195 | }, |
| 196 | ] |
| 197 | |
| 198 | class Plugin(PydanticPluginProtocol): |
| 199 | def new_schema_validator(self, schema, schema_type, schema_type_path, schema_kind, config, plugin_settings): |
| 200 | assert config == {'title': 'Model'} |
| 201 | assert plugin_settings == {'observe': 'all'} |
| 202 | assert schema_type.__name__ == 'Model' |
| 203 | assert schema_kind == 'BaseModel' |
| 204 | return CustomOnValidatePython(), None, None |
| 205 | |
| 206 | plugin = Plugin() |
| 207 | with install_plugin(plugin): |
| 208 | |
| 209 | class Model(BaseModel, plugin_settings={'observe': 'all'}): |
| 210 | a: int |
| 211 | |
| 212 | with contextlib.suppress(ValidationError): |
| 213 | Model.model_validate({'a': 'potato'}) |
| 214 | assert Model.model_validate_json('{"a": 1}').model_dump() == {'a': 1} |
| 215 | |
| 216 | |
| 217 | def test_stateful_plugin() -> None: |
nothing calls this directly
no test coverage detected