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 1 commit
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(slf.py()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be

Suggested change
borrow.title.as_ref(slf.py()),
borrow.title.as_ref(py),

like the rest?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes 👍

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
9 changes: 9 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,11 @@ def test_hide_input_in_json() -> None:

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


def test_validation_error_pickle() -> None:
s = SchemaValidator({'type': 'int'})
with pytest.raises(ValidationError) as exc_info:
s.validate_python('definitely not an int')

pickle.loads(pickle.dumps(exc_info.value))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this test that the unpickled value is equivalent to the original?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll check .errors are equivalent, as I want to steer clear from implementing equality for the whole objects (as that's a much bigger change).