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

@@ -10,6 +10,7 @@ use pyo3::{
prelude::*,
types::{PyNone, PyTuple},
};
use snafu::{ResultExt, Snafu};
use super::arbitrary::{Arbitrary, MapKeyFromArbitraryError};
@@ -43,9 +44,40 @@ impl Display for MapKey {
}
#[cfg(feature = "pyo3")]
impl<'py> FromPyObject<'py> for MapKey {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
if let Ok(_none) = ob.downcast::<PyNone>() {
#[derive(Debug, Snafu)]
pub enum ExtractMapKeyError {
/// 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 },
/// can't extract a map key from a {type_name}
UnsupportedType { type_name: String },
}
#[cfg(feature = "pyo3")]
impl From<ExtractMapKeyError> for PyErr {
fn from(error: ExtractMapKeyError) -> Self {
use pyo3::exceptions::PyException;
match &error {
ExtractMapKeyError::GetTypeNameError { .. } => PyException::new_err(error.to_string()),
ExtractMapKeyError::ExtractTypeNameError { .. } => {
PyException::new_err(error.to_string())
}
ExtractMapKeyError::UnsupportedType { .. } => PyTypeError::new_err(error.to_string()),
}
}
}
#[cfg(feature = "pyo3")]
impl<'a, 'py> FromPyObject<'a, 'py> for MapKey {
type Error = ExtractMapKeyError;
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
if let Ok(_none) = ob.cast::<PyNone>() {
Ok(Self::Null)
} else if let Ok(b) = ob.extract() {
Ok(Self::Bool(b))
@@ -56,10 +88,13 @@ impl<'py> FromPyObject<'py> for MapKey {
} else if let Ok(tuple) = ob.extract() {
Ok(Self::Tuple(tuple))
} else {
let type_name = ob.get_type().fully_qualified_name()?;
Err(PyTypeError::new_err(format!(
"can't extract a map key from a {type_name}"
)))
let type_name = ob
.get_type()
.fully_qualified_name()
.context(GetTypeNameSnafu)?
.extract()
.context(ExtractTypeNameSnafu)?;
Err(ExtractMapKeyError::UnsupportedType { type_name })
}
}
}