chore(arbitrary-value): update to pyo3 0.27

This commit is contained in:
2026-01-07 01:19:34 -05:00
parent 10bceb55b8
commit 36bfa89548
2 changed files with 95 additions and 16 deletions

View File

@@ -3,11 +3,13 @@ use chrono_tz::Tz;
use ijson::{IArray, INumber, IObject, IString, IValue};
#[cfg(feature = "pyo3")]
use pyo3::{
exceptions::{PyTypeError, PyValueError},
exceptions::{PyException, PyTypeError, PyValueError},
prelude::*,
types::{PyList, PyNone},
};
use snafu::Snafu;
use snafu::{ResultExt, Snafu};
use crate::finite_f64::NotFinite;
use super::{finite_f64::FiniteF64, map::Map, map_key::MapKey};
@@ -73,22 +75,64 @@ impl From<Arbitrary> for IValue {
}
#[cfg(feature = "pyo3")]
impl<'py> FromPyObject<'py> for Arbitrary {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
#[derive(Debug, Snafu)]
pub enum ExtractArbitraryError {
/// error getting the qualified type name when trying to report
/// that an instance of this type cannot be extracted as an [`Arbitrary`]
GetTypeNameError { source: PyErr },
/// error extracting the (successfully retrieved) fully qualified type name as a [`String`]
ExtractTypeNameError { source: PyErr },
/// the float trying to be extracted is not finite, which isn't supported
FloatNotFinite { source: NotFinite },
/// can't extract an arbitrary from a {type_name}
UnsupportedType { type_name: String },
}
#[cfg(feature = "pyo3")]
impl From<ExtractArbitraryError> for PyErr {
fn from(error: ExtractArbitraryError) -> Self {
match &error {
ExtractArbitraryError::GetTypeNameError { .. } => {
PyException::new_err(error.to_string())
}
ExtractArbitraryError::ExtractTypeNameError { .. } => {
PyException::new_err(error.to_string())
}
ExtractArbitraryError::FloatNotFinite { .. } => {
PyValueError::new_err(error.to_string())
}
ExtractArbitraryError::UnsupportedType { .. } => {
PyTypeError::new_err(error.to_string())
}
}
}
}
#[cfg(feature = "pyo3")]
impl<'a, 'py> FromPyObject<'a, 'py> for Arbitrary {
type Error = ExtractArbitraryError;
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
if let Ok(map_key) = ob.extract::<MapKey>() {
Ok(map_key.into())
} else if let Ok(map) = ob.extract() {
Ok(Self::Map(map))
} else if let Ok(f) = ob.extract::<f64>() {
let f = FiniteF64::try_from(f).map_err(|err| PyValueError::new_err(err.to_string()))?;
let f = FiniteF64::try_from(f).context(FloatNotFiniteSnafu)?;
Ok(Self::Float(f))
} else if let Ok(vec) = ob.extract() {
Ok(Self::Array(vec))
} else {
let type_name = ob.get_type().fully_qualified_name()?;
Err(PyTypeError::new_err(format!(
"can't extract an arbitrary from a {type_name}"
)))
let type_name = ob
.get_type()
.fully_qualified_name()
.context(GetTypeNameSnafu)?
.extract()
.context(ExtractTypeNameSnafu)?;
Err(ExtractArbitraryError::UnsupportedType { type_name })
}
}
}