Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pickling for ValidationError #1119

Merged
merged 3 commits into from Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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()