Skip to content

Commit

Permalink
Implement pickling for ValidationError (#1119)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt committed Dec 12, 2023
1 parent a5b3906 commit dfe5027
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/errors/validation_exception.rs
Expand Up @@ -341,6 +341,20 @@ impl ValidationError {
fn __str__(&self, py: Python) -> String {
self.__repr__(py)
}

fn __reduce__(slf: &PyCell<Self>) -> PyResult<(&PyAny, PyObject)> {
let py = slf.py();
let callable = slf.getattr("from_exception_data")?;
let borrow = slf.try_borrow()?;
let args = (
borrow.title.as_ref(py),
borrow.errors(py, include_url_env(py), true, true)?,
borrow.input_type.into_py(py),
borrow.hide_input,
)
.into_py(slf.py());
Ok((callable, args))
}
}

// TODO: is_utf8_char_boundary, floor_char_boundary and ceil_char_boundary
Expand Down
15 changes: 15 additions & 0 deletions tests/test_errors.py
@@ -1,4 +1,5 @@
import enum
import pickle
import re
import sys
from decimal import Decimal
Expand Down Expand Up @@ -1074,3 +1075,17 @@ def test_hide_input_in_json() -> None:

for error in exc_info.value.errors(include_input=False):
assert 'input' not in error


@pytest.mark.skipif(
sys.version_info < (3, 9) and sys.implementation.name == 'pypy',
reason='PyPy before 3.9 cannot pickle this correctly',
)
def test_validation_error_pickle() -> None:
s = SchemaValidator({'type': 'int'})
with pytest.raises(ValidationError) as exc_info:
s.validate_python('definitely not an int')

original = exc_info.value
roundtripped = pickle.loads(pickle.dumps(original))
assert original.errors() == roundtripped.errors()

0 comments on commit dfe5027

Please sign in to comment.