From 0d7999f11dc0dc31de8401819b5d7e26dc985f34 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Sat, 30 Aug 2025 10:04:44 +0100 Subject: [PATCH 1/2] release: 0.26 --- CHANGELOG.md | 14 ++++++++ Cargo.toml | 8 ++--- README.md | 2 +- src/de.rs | 40 ++++++++++----------- src/error.rs | 2 +- src/ser.rs | 4 +-- tests/test_custom_types.rs | 52 +++++++++++++--------------- tests/test_with_serde_path_to_err.rs | 10 +++--- 8 files changed, 72 insertions(+), 60 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 16b1080..a21bd55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,17 @@ +## 0.26.0 - 2025-08-30 + +### Packaging +- Bump MSRV to 1.74 +- Update to PyO3 0.26 + +### Changed +- `PythonizeTypes`, `PythonizeMappingType` and `PythonizeNamedMappingType` no longer have a lifetime on the trait, instead the `Builder` type is a GAT. + +## 0.25.0 - 2025-05-23 + +### Packaging +- Update to PyO3 0.25 + ## 0.24.0 - 2025-03-26 ### Packaging diff --git a/Cargo.toml b/Cargo.toml index d636384..c0797e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "pythonize" -version = "0.25.0" +version = "0.26.0" authors = ["David Hewitt <1939362+davidhewitt@users.noreply.github.com>"] edition = "2021" -rust-version = "1.65" +rust-version = "1.74" license = "MIT" description = "Serde Serializer & Deserializer from Rust <--> Python, backed by PyO3." homepage = "https://github.com/davidhewitt/pythonize" @@ -13,11 +13,11 @@ documentation = "https://docs.rs/crate/pythonize/" [dependencies] serde = { version = "1.0", default-features = false, features = ["std"] } -pyo3 = { version = "0.25", default-features = false } +pyo3 = { version = "0.26", default-features = false } [dev-dependencies] serde = { version = "1.0", default-features = false, features = ["derive"] } -pyo3 = { version = "0.25", default-features = false, features = ["auto-initialize", "macros", "py-clone"] } +pyo3 = { version = "0.26", default-features = false, features = ["auto-initialize", "macros", "py-clone"] } serde_json = "1.0" serde_bytes = "0.11" maplit = "1.0.2" diff --git a/README.md b/README.md index 441f29f..2667523 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ let sample = Sample { bar: None }; -Python::with_gil(|py| { +Python::attach(|py| { // Rust -> Python let obj = pythonize(py, &sample).unwrap(); diff --git a/src/de.rs b/src/de.rs index 2860e48..a30dbca 100644 --- a/src/de.rs +++ b/src/de.rs @@ -24,7 +24,7 @@ impl<'a, 'py> Depythonizer<'a, 'py> { } fn sequence_access(&self, expected_len: Option) -> Result> { - let seq = self.input.downcast::()?; + let seq = self.input.cast::()?; let len = self.input.len()?; match expected_len { @@ -36,10 +36,10 @@ impl<'a, 'py> Depythonizer<'a, 'py> { } fn set_access(&self) -> Result> { - match self.input.downcast::() { + match self.input.cast::() { Ok(set) => Ok(PySetAsSequence::from_set(set)), Err(e) => { - if let Ok(f) = self.input.downcast::() { + if let Ok(f) = self.input.cast::() { Ok(PySetAsSequence::from_frozenset(f)) } else { Err(e.into()) @@ -49,7 +49,7 @@ impl<'a, 'py> Depythonizer<'a, 'py> { } fn dict_access(&self) -> Result> { - PyMappingAccess::new(self.input.downcast()?) + PyMappingAccess::new(self.input.cast()?) } fn deserialize_any_int<'de, V>(&self, int: &Bound<'_, PyInt>, visitor: V) -> Result @@ -111,7 +111,7 @@ impl<'de> de::Deserializer<'de> for &'_ mut Depythonizer<'_, '_> { self.deserialize_unit(visitor) } else if obj.is_instance_of::() { self.deserialize_bool(visitor) - } else if let Ok(x) = obj.downcast::() { + } else if let Ok(x) = obj.cast::() { self.deserialize_any_int(x, visitor) } else if obj.is_instance_of::() || obj.is_instance_of::() { self.deserialize_tuple(obj.len()?, visitor) @@ -128,9 +128,9 @@ impl<'de> de::Deserializer<'de> for &'_ mut Depythonizer<'_, '_> { self.deserialize_f64(visitor) } else if obj.is_instance_of::() || obj.is_instance_of::() { self.deserialize_seq(visitor) - } else if obj.downcast::().is_ok() { + } else if obj.cast::().is_ok() { self.deserialize_tuple(obj.len()?, visitor) - } else if obj.downcast::().is_ok() { + } else if obj.cast::().is_ok() { self.deserialize_map(visitor) } else { Err(obj.get_type().qualname().map_or_else( @@ -151,7 +151,7 @@ impl<'de> de::Deserializer<'de> for &'_ mut Depythonizer<'_, '_> { where V: de::Visitor<'de>, { - let s = self.input.downcast::()?.to_cow()?; + let s = self.input.cast::()?.to_cow()?; if s.len() != 1 { return Err(PythonizeError::invalid_length_char()); } @@ -175,7 +175,7 @@ impl<'de> de::Deserializer<'de> for &'_ mut Depythonizer<'_, '_> { where V: de::Visitor<'de>, { - let s = self.input.downcast::()?; + let s = self.input.cast::()?; visitor.visit_str(&s.to_cow()?) } @@ -190,7 +190,7 @@ impl<'de> de::Deserializer<'de> for &'_ mut Depythonizer<'_, '_> { where V: de::Visitor<'de>, { - let b = self.input.downcast::()?; + let b = self.input.cast::()?; visitor.visit_bytes(b.as_bytes()) } @@ -303,9 +303,9 @@ impl<'de> de::Deserializer<'de> for &'_ mut Depythonizer<'_, '_> { V: de::Visitor<'de>, { let item = &self.input; - if let Ok(s) = item.downcast::() { + if let Ok(s) = item.cast::() { visitor.visit_enum(s.to_cow()?.into_deserializer()) - } else if let Ok(m) = item.downcast::() { + } else if let Ok(m) = item.cast::() { // Get the enum variant from the mapping key if m.len()? != 1 { return Err(PythonizeError::invalid_length_enum()); @@ -313,7 +313,7 @@ impl<'de> de::Deserializer<'de> for &'_ mut Depythonizer<'_, '_> { let variant: Bound = m .keys()? .get_item(0)? - .downcast_into::() + .cast_into::() .map_err(|_| PythonizeError::dict_key_not_string())?; let value = m.get_item(&variant)?; visitor.visit_enum(PyEnumAccess::new(&value, variant)) @@ -328,7 +328,7 @@ impl<'de> de::Deserializer<'de> for &'_ mut Depythonizer<'_, '_> { { let s = self .input - .downcast::() + .cast::() .map_err(|_| PythonizeError::dict_key_not_string())?; visitor.visit_str(&s.to_cow()?) } @@ -528,7 +528,7 @@ mod test { where T: de::DeserializeOwned + PartialEq + std::fmt::Debug, { - Python::with_gil(|py| { + Python::attach(|py| { let obj = py.eval(code, None, None).unwrap(); let actual: T = depythonize(&obj).unwrap(); assert_eq!(&actual, expected); @@ -585,7 +585,7 @@ mod test { let code = c_str!("{'foo': 'Foo'}"); - Python::with_gil(|py| { + Python::attach(|py| { let locals = PyDict::new(py); let obj = py.eval(code, None, Some(&locals)).unwrap(); assert!(matches!( @@ -613,7 +613,7 @@ mod test { let code = c_str!("('cat', -10.05, 'foo')"); - Python::with_gil(|py| { + Python::attach(|py| { let locals = PyDict::new(py); let obj = py.eval(code, None, Some(&locals)).unwrap(); assert!(matches!( @@ -825,7 +825,7 @@ mod test { #[test] fn test_int_limits() { - Python::with_gil(|py| { + Python::attach(|py| { // serde_json::Value supports u64 and i64 as maximum sizes let _: serde_json::Value = depythonize(&u8::MAX.into_pyobject(py).unwrap()).unwrap(); let _: serde_json::Value = depythonize(&u8::MIN.into_pyobject(py).unwrap()).unwrap(); @@ -857,7 +857,7 @@ mod test { #[test] fn test_deserialize_bytes() { - Python::with_gil(|py| { + Python::attach(|py| { let obj = PyBytes::new(py, "hello".as_bytes()); let actual: Vec = depythonize(&obj).unwrap(); assert_eq!(actual, b"hello"); @@ -874,7 +874,7 @@ mod test { #[test] fn test_unknown_type() { - Python::with_gil(|py| { + Python::attach(|py| { let obj = py .import("decimal") .unwrap() diff --git a/src/error.rs b/src/error.rs index 1bcc556..7828a71 100644 --- a/src/error.rs +++ b/src/error.rs @@ -73,7 +73,7 @@ pub enum ErrorImpl { Message(String), /// A Python type not supported by the deserializer UnsupportedType(String), - /// A `PyAny` object that failed to downcast to an expected Python type + /// A `PyAny` object that failed to cast to an expected Python type UnexpectedType(String), /// Dict keys should be strings to deserialize to struct fields DictKeyNotString, diff --git a/src/ser.rs b/src/ser.rs index dce22e3..c8e6dd1 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -632,7 +632,7 @@ mod test { where T: Serialize, { - Python::with_gil(|py| -> PyResult<()> { + Python::attach(|py| -> PyResult<()> { let obj = pythonize(py, &src)?; let locals = PyDict::new(py); @@ -845,7 +845,7 @@ mod test { // serde treats &[u8] as a sequence of integers due to lack of specialization test_ser(b"foo", "[102,111,111]"); - Python::with_gil(|py| { + Python::attach(|py| { assert!(pythonize(py, serde_bytes::Bytes::new(b"foo")) .expect("bytes will always serialize successfully") .eq(&PyBytes::new(py, b"foo")) diff --git a/tests/test_custom_types.rs b/tests/test_custom_types.rs index 32c5768..27888d0 100644 --- a/tests/test_custom_types.rs +++ b/tests/test_custom_types.rs @@ -4,7 +4,7 @@ use pyo3::{ exceptions::{PyIndexError, PyKeyError}, prelude::*, types::{PyDict, PyMapping, PySequence, PyTuple}, - BoundObject, + IntoPyObjectExt, }; use pythonize::{ depythonize, pythonize_custom, PythonizeListType, PythonizeMappingType, @@ -15,7 +15,7 @@ use serde_json::{json, Value}; #[pyclass(sequence)] struct CustomList { - items: Vec, + items: Vec>, } #[pymethods] @@ -24,7 +24,7 @@ impl CustomList { self.items.len() } - fn __getitem__(&self, idx: isize) -> PyResult { + fn __getitem__(&self, idx: isize) -> PyResult> { self.items .get(idx as usize) .cloned() @@ -46,14 +46,12 @@ impl PythonizeListType for CustomList { CustomList { items: elements .into_iter() - .map(|item| item.into_pyobject(py).map(|x| x.into_any().unbind())) - .collect::, T::Error>>() - .map_err(Into::into)?, + .map(|item| item.into_py_any(py)) + .collect::>()?, }, - )? - .into_any(); + )?; - Ok(unsafe { sequence.downcast_into_unchecked() }) + Ok(unsafe { sequence.cast_into_unchecked() }) } } @@ -66,7 +64,7 @@ impl<'py> PythonizeTypes for PythonizeCustomList { #[test] fn test_custom_list() { - Python::with_gil(|py| { + Python::attach(|py| { PySequence::register::(py).unwrap(); let serialized = pythonize_custom::(py, &json!([1, 2, 3])).unwrap(); assert!(serialized.is_instance_of::()); @@ -78,7 +76,7 @@ fn test_custom_list() { #[pyclass(mapping)] struct CustomDict { - items: HashMap, + items: HashMap>, } #[pymethods] @@ -87,14 +85,14 @@ impl CustomDict { self.items.len() } - fn __getitem__(&self, key: String) -> PyResult { + fn __getitem__(&self, key: String) -> PyResult> { self.items .get(&key) .cloned() .ok_or_else(|| PyKeyError::new_err(key)) } - fn __setitem__(&mut self, key: String, value: PyObject) { + fn __setitem__(&mut self, key: String, value: Py) { self.items.insert(key, value); } @@ -102,7 +100,7 @@ impl CustomDict { self.items.keys().collect() } - fn values(&self) -> Vec { + fn values(&self) -> Vec> { self.items.values().cloned().collect() } } @@ -124,11 +122,11 @@ impl PythonizeMappingType for CustomDict { key: Bound<'py, PyAny>, value: Bound<'py, PyAny>, ) -> PyResult<()> { - unsafe { builder.downcast_unchecked::() }.set_item(key, value) + unsafe { builder.cast_unchecked::() }.set_item(key, value) } fn finish<'py>(builder: Self::Builder<'py>) -> PyResult> { - Ok(unsafe { builder.into_any().downcast_into_unchecked() }) + Ok(unsafe { builder.cast_into_unchecked() }) } } @@ -141,7 +139,7 @@ impl<'py> PythonizeTypes for PythonizeCustomDict { #[test] fn test_custom_dict() { - Python::with_gil(|py| { + Python::attach(|py| { PyMapping::register::(py).unwrap(); let serialized = pythonize_custom::(py, &json!({ "hello": 1, "world": 2 })) @@ -155,7 +153,7 @@ fn test_custom_dict() { #[test] fn test_tuple() { - Python::with_gil(|py| { + Python::attach(|py| { PyMapping::register::(py).unwrap(); let serialized = pythonize_custom::(py, &json!([1, 2, 3, 4])).unwrap(); @@ -169,7 +167,7 @@ fn test_tuple() { #[test] fn test_pythonizer_can_be_created() { // https://github.com/davidhewitt/pythonize/pull/56 - Python::with_gil(|py| { + Python::attach(|py| { let sample = json!({ "hello": 1, "world": 2 }); assert!(sample .serialize(Pythonizer::new(py)) @@ -186,7 +184,7 @@ fn test_pythonizer_can_be_created() { #[pyclass(mapping)] struct NamedCustomDict { name: String, - items: HashMap, + items: HashMap>, } #[pymethods] @@ -195,14 +193,14 @@ impl NamedCustomDict { self.items.len() } - fn __getitem__(&self, key: String) -> PyResult { + fn __getitem__(&self, key: String) -> PyResult> { self.items .get(&key) .cloned() .ok_or_else(|| PyKeyError::new_err(key)) } - fn __setitem__(&mut self, key: String, value: PyObject) { + fn __setitem__(&mut self, key: String, value: Py) { self.items.insert(key, value); } @@ -210,7 +208,7 @@ impl NamedCustomDict { self.items.keys().collect() } - fn values(&self) -> Vec { + fn values(&self) -> Vec> { self.items.values().cloned().collect() } } @@ -237,11 +235,11 @@ impl PythonizeNamedMappingType for NamedCustomDict { name: Bound<'py, pyo3::types::PyString>, value: Bound<'py, PyAny>, ) -> PyResult<()> { - unsafe { builder.downcast_unchecked::() }.set_item(name, value) + unsafe { builder.cast_unchecked::() }.set_item(name, value) } fn finish<'py>(builder: Self::Builder<'py>) -> PyResult> { - Ok(unsafe { builder.into_any().downcast_into_unchecked() }) + Ok(unsafe { builder.cast_into_unchecked() }) } } @@ -260,7 +258,7 @@ struct Struct { #[test] fn test_custom_unnamed_dict() { - Python::with_gil(|py| { + Python::attach(|py| { PyMapping::register::(py).unwrap(); let serialized = pythonize_custom::(py, &Struct { hello: 1, world: 2 }).unwrap(); @@ -273,7 +271,7 @@ fn test_custom_unnamed_dict() { #[test] fn test_custom_named_dict() { - Python::with_gil(|py| { + Python::attach(|py| { PyMapping::register::(py).unwrap(); let serialized = pythonize_custom::(py, &Struct { hello: 1, world: 2 }) diff --git a/tests/test_with_serde_path_to_err.rs b/tests/test_with_serde_path_to_err.rs index 9c3b688..d92c4d1 100644 --- a/tests/test_with_serde_path_to_err.rs +++ b/tests/test_with_serde_path_to_err.rs @@ -40,7 +40,7 @@ impl Serialize for CannotSerialize { #[test] fn test_de_valid() { - Python::with_gil(|py| { + Python::attach(|py| { let pyroot = PyDict::new(py); pyroot.set_item("root_key", "root_value").unwrap(); @@ -82,7 +82,7 @@ fn test_de_valid() { #[test] fn test_de_invalid() { - Python::with_gil(|py| { + Python::attach(|py| { let pyroot = PyDict::new(py); pyroot.set_item("root_key", "root_value").unwrap(); @@ -106,7 +106,7 @@ fn test_de_invalid() { #[test] fn test_ser_valid() { - Python::with_gil(|py| { + Python::attach(|py| { let root = Root { root_key: String::from("root_value"), root_map: BTreeMap::from([ @@ -128,7 +128,7 @@ fn test_ser_valid() { let ser = pythonize::Pythonizer::>::from(py); let pyroot: Bound<'_, PyAny> = serde_path_to_error::serialize(&root, ser).unwrap(); - let pyroot = pyroot.downcast::().unwrap(); + let pyroot = pyroot.cast::().unwrap(); assert_eq!(pyroot.len(), 2); let root_value: String = pyroot @@ -181,7 +181,7 @@ fn test_ser_valid() { #[test] fn test_ser_invalid() { - Python::with_gil(|py| { + Python::attach(|py| { let root = Root { root_key: String::from("root_value"), root_map: BTreeMap::from([ From a663a99d4041bf6723e790005a468be81ab40dd9 Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Sat, 30 Aug 2025 10:07:04 +0100 Subject: [PATCH 2/2] use trusted publishing for release --- .github/workflows/release.yml | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5f21b46..f538d3d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,18 +4,29 @@ on: push: tags: - "v*" + workflow_dispatch: + inputs: + version: + description: The version to build jobs: release: + permissions: + id-token: write + runs-on: ubuntu-latest environment: release steps: - - name: Checkout repository - uses: actions/checkout@v4 + - uses: actions/checkout@v5 + with: + # The tag to build or the tag received by the tag event + ref: ${{ github.event.inputs.version || github.ref }} + persist-credentials: false - - uses: dtolnay/rust-toolchain@stable + - uses: rust-lang/crates-io-auth-action@v1 + id: auth - name: Publish to crates.io run: cargo publish env: - CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }} + CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}