MCPcopy
hub / github.com/pydantic/pydantic / test_on_validate_python_on_error

Function test_on_validate_python_on_error

tests/test_plugins.py:167–214  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

165
166
167def 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
217def test_stateful_plugin() -> None:

Callers

nothing calls this directly

Calls 5

install_pluginFunction · 0.85
model_validateMethod · 0.80
model_validate_jsonMethod · 0.80
PluginClass · 0.70
model_dumpMethod · 0.45

Tested by

no test coverage detected