Skip to content

Commit ba8079b

Browse files
authored
allow subclassing of ValidationError (#556)
1 parent 414745c commit ba8079b

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

src/errors/validation_exception.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use super::location::Location;
2121
use super::types::{ErrorMode, ErrorType};
2222
use super::ValError;
2323

24-
#[pyclass(extends=PyValueError, module="pydantic_core._pydantic_core")]
24+
#[pyclass(subclass, extends=PyValueError, module="pydantic_core._pydantic_core")]
2525
#[derive(Clone)]
2626
#[cfg_attr(debug_assertions, derive(Debug))]
2727
pub struct ValidationError {

tests/test_errors.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,3 +620,14 @@ def test_loc_with_dots():
620620
" Input should be a valid integer, unable to parse string as an integer "
621621
"[type=int_parsing, input_value='x', input_type=str]"
622622
)
623+
624+
625+
def subclass_validation_error():
626+
class MyValidationError(ValidationError):
627+
def __init__(self, title: str, errors, error_mode='python'):
628+
self.body = 123
629+
super().__init__(title, errors, error_mode)
630+
631+
e = MyValidationError('testing', [])
632+
assert e.body == 123
633+
assert e.title == 'testing'

0 commit comments

Comments
 (0)