MCPcopy
hub / github.com/pydantic/pydantic / test_config_validation_error_cause

Function test_config_validation_error_cause

tests/test_config.py:545–569  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

543
544@pytest.mark.skipif(sys.version_info < (3, 11), reason='requires backport pre 3.11, fully tested in pydantic core')
545def test_config_validation_error_cause():
546 class Foo(BaseModel):
547 foo: int
548
549 @field_validator('foo')
550 def check_foo(cls, v):
551 assert v > 5, 'Must be greater than 5'
552
553 # Should be disabled by default:
554 with pytest.raises(ValidationError) as exc_info:
555 Foo(foo=4)
556 assert exc_info.value.__cause__ is None
557
558 Foo.model_config = ConfigDict(validation_error_cause=True)
559 Foo.model_rebuild(force=True)
560 with pytest.raises(ValidationError) as exc_info:
561 Foo(foo=4)
562 # Confirm python error attached as a cause, and error location specified in a note:
563 assert exc_info.value.__cause__ is not None
564 assert isinstance(exc_info.value.__cause__, ExceptionGroup) # noqa: F821
565 assert len(exc_info.value.__cause__.exceptions) == 1
566 src_exc = exc_info.value.__cause__.exceptions[0]
567 assert repr(src_exc) == "AssertionError('Must be greater than 5\\nassert 4 > 5')"
568 assert len(src_exc.__notes__) == 1
569 assert src_exc.__notes__[0] == '\nPydantic: cause of loc: foo'
570
571
572def test_config_defaults_match():

Callers

nothing calls this directly

Calls 3

ConfigDictClass · 0.90
model_rebuildMethod · 0.80
FooClass · 0.70

Tested by

no test coverage detected