Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/jsonschema-py/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- Improved error message for unknown formats.
- Update `pyo3` to `0.23`.

## [0.26.1] - 2024-10-29

Expand Down
2 changes: 1 addition & 1 deletion crates/jsonschema-py/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ pyo3-build-config = { version = "0.23.1", features = ["resolve-config"] }
jsonschema = { path = "../jsonschema/" }
serde.workspace = true
serde_json.workspace = true
pyo3 = { version = "0.22.2", features = ["extension-module"] }
pyo3 = { version = "0.23.3", features = ["extension-module"] }
pyo3-built = "0.5"
53 changes: 24 additions & 29 deletions crates/jsonschema-py/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,39 +79,33 @@ impl ValidationErrorIter {
}

fn into_py_err(py: Python<'_>, error: jsonschema::ValidationError<'_>) -> PyResult<PyErr> {
let pyerror_type = PyType::new_bound::<ValidationError>(py);
let pyerror_type = PyType::new::<ValidationError>(py);
let message = error.to_string();
let verbose_message = to_error_message(&error, message.clone());
let into_path = |segment: &str| {
if let Ok(idx) = segment.parse::<usize>() {
idx.into_py(py)
idx.into_pyobject(py).and_then(PyObject::try_from)
} else {
segment.into_py(py)
segment.into_pyobject(py).and_then(PyObject::try_from)
}
};
let schema_path = PyList::new_bound(
py,
error
.schema_path
.as_str()
.split('/')
.skip(1)
.map(into_path)
.collect::<Vec<_>>(),
)
.unbind();
let instance_path = PyList::new_bound(
py,
error
.instance_path
.as_str()
.split('/')
.skip(1)
.map(into_path)
.collect::<Vec<_>>(),
)
.unbind();
Ok(PyErr::from_type_bound(
let elements = error
.schema_path
.as_str()
.split('/')
.skip(1)
.map(into_path)
.collect::<Result<Vec<_>, _>>()?;
let schema_path = PyList::new(py, elements)?.unbind();
let elements = error
.instance_path
.as_str()
.split('/')
.skip(1)
.map(into_path)
.collect::<Result<Vec<_>, _>>()?;
let instance_path = PyList::new(py, elements)?.unbind();
Ok(PyErr::from_type(
pyerror_type,
(message, verbose_message, schema_path, instance_path),
))
Expand Down Expand Up @@ -161,8 +155,8 @@ fn make_options(
let callback: Py<PyAny> = callback.clone().unbind();
let call_py_callback = move |value: &str| {
Python::with_gil(|py| {
let value = PyString::new_bound(py, value);
callback.call_bound(py, (value,), None)?.is_truthy(py)
let value = PyString::new(py, value);
callback.call(py, (value,), None)?.is_truthy(py)
})
};
options.with_format(
Expand Down Expand Up @@ -719,14 +713,15 @@ fn jsonschema_rs(py: Python<'_>, module: &Bound<'_, PyModule>) -> PyResult<()> {
module.add_class::<Draft7Validator>()?;
module.add_class::<Draft201909Validator>()?;
module.add_class::<Draft202012Validator>()?;
module.add("ValidationError", py.get_type_bound::<ValidationError>())?;
module.add("ValidationError", py.get_type::<ValidationError>())?;
module.add("Draft4", DRAFT4)?;
module.add("Draft6", DRAFT6)?;
module.add("Draft7", DRAFT7)?;
module.add("Draft201909", DRAFT201909)?;
module.add("Draft202012", DRAFT202012)?;

// Add build metadata to ease triaging incoming issues
#[allow(deprecated)]
module.add("__build__", pyo3_built::pyo3_built!(py, build))?;

Ok(())
Expand Down