Skip to content

Commit fa94118

Browse files
committed
Implement pickling for ValidationError
1 parent a5b3906 commit fa94118

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/errors/validation_exception.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,20 @@ impl ValidationError {
341341
fn __str__(&self, py: Python) -> String {
342342
self.__repr__(py)
343343
}
344+
345+
fn __reduce__(slf: &PyCell<Self>) -> PyResult<(&PyAny, PyObject)> {
346+
let py = slf.py();
347+
let callable = slf.getattr("from_exception_data")?;
348+
let borrow = slf.try_borrow()?;
349+
let args = (
350+
borrow.title.as_ref(slf.py()),
351+
borrow.errors(py, include_url_env(py), true, true)?,
352+
borrow.input_type.into_py(py),
353+
borrow.hide_input,
354+
)
355+
.into_py(slf.py());
356+
Ok((callable, args))
357+
}
344358
}
345359

346360
// TODO: is_utf8_char_boundary, floor_char_boundary and ceil_char_boundary

tests/test_errors.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import enum
2+
import pickle
23
import re
34
import sys
45
from decimal import Decimal
@@ -1074,3 +1075,11 @@ def test_hide_input_in_json() -> None:
10741075

10751076
for error in exc_info.value.errors(include_input=False):
10761077
assert 'input' not in error
1078+
1079+
1080+
def test_validation_error_pickle() -> None:
1081+
s = SchemaValidator({'type': 'int'})
1082+
with pytest.raises(ValidationError) as exc_info:
1083+
s.validate_python('definitely not an int')
1084+
1085+
pickle.loads(pickle.dumps(exc_info.value))

0 commit comments

Comments
 (0)