From 37b3f635b4602c100ee81f3117bca31275e83ed1 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Wed, 9 Nov 2022 18:44:21 +0000 Subject: [PATCH] Fix support of sequence protocol for returned lists (backport #730) (#735) * Bump pyo3 from 0.17.2 to 0.17.3 (#723) Bumps [pyo3](https://github.com/pyo3/pyo3) from 0.17.2 to 0.17.3. - [Release notes](https://github.com/pyo3/pyo3/releases) - [Changelog](https://github.com/PyO3/pyo3/blob/main/CHANGELOG.md) - [Commits](https://github.com/pyo3/pyo3/compare/v0.17.2...v0.17.3) --- updated-dependencies: - dependency-name: pyo3 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> (cherry picked from commit d91f4888c45b39d69d9e8cbb9e76bc196e9b3a3c) # Conflicts: # Cargo.lock # Cargo.toml * Fix support of sequence protocol for returned lists (#730) * Fix support of sequence protocol for returned lists Add the `sequence` option to PyO3's `pyclass`, so that the `sq_length` slot is implemented [1]. Implementing this method is required for sequence types, or Python C API functions like `PySequence_Size` will fail with an error. The `reversed` built-in method relies on `PySequence_*` methods. A test reversing `NodeIndices` is added to guard against future violations of the sequence protocol. Fixes #696. [1]: https://github.com/PyO3/pyo3/pull/2567 * Add release note Co-authored-by: Matthew Treinish (cherry picked from commit 4592bc0a05fb7cb23d6d647c979931c176656efa) Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Tino Wagner Co-authored-by: Ivan Carvalho --- Cargo.lock | 24 +++++++++---------- Cargo.toml | 2 +- ...ix-sequence-protocol-e95246e864cc850a.yaml | 10 ++++++++ src/iterators.rs | 2 +- .../test_custom_return_types.py | 6 +++++ 5 files changed, 30 insertions(+), 14 deletions(-) create mode 100644 releasenotes/notes/fix-sequence-protocol-e95246e864cc850a.yaml diff --git a/Cargo.lock b/Cargo.lock index 48fa0c56d..22b74c543 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -278,7 +278,7 @@ dependencies = [ "ahash", "libc", "ndarray", - "num-complex 0.2.4", + "num-complex 0.4.1", "num-integer", "num-traits", "pyo3", @@ -352,9 +352,9 @@ dependencies = [ [[package]] name = "pyo3" -version = "0.17.2" +version = "0.17.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "201b6887e5576bf2f945fe65172c1fcbf3fcf285b23e4d71eb171d9736e38d32" +checksum = "268be0c73583c183f2b14052337465768c07726936a260f480f0857cb95ba543" dependencies = [ "cfg-if", "hashbrown", @@ -363,7 +363,7 @@ dependencies = [ "libc", "memoffset", "num-bigint", - "num-complex 0.2.4", + "num-complex 0.4.1", "parking_lot", "pyo3-build-config", "pyo3-ffi", @@ -373,9 +373,9 @@ dependencies = [ [[package]] name = "pyo3-build-config" -version = "0.17.2" +version = "0.17.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf0708c9ed01692635cbf056e286008e5a2927ab1a5e48cdd3aeb1ba5a6fef47" +checksum = "28fcd1e73f06ec85bf3280c48c67e731d8290ad3d730f8be9dc07946923005c8" dependencies = [ "once_cell", "target-lexicon", @@ -383,9 +383,9 @@ dependencies = [ [[package]] name = "pyo3-ffi" -version = "0.17.2" +version = "0.17.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90352dea4f486932b72ddf776264d293f85b79a1d214de1d023927b41461132d" +checksum = "0f6cb136e222e49115b3c51c32792886defbfb0adead26a688142b346a0b9ffc" dependencies = [ "libc", "pyo3-build-config", @@ -393,9 +393,9 @@ dependencies = [ [[package]] name = "pyo3-macros" -version = "0.17.2" +version = "0.17.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7eb24b804a2d9e88bfcc480a5a6dd76f006c1e3edaf064e8250423336e2cd79d" +checksum = "94144a1266e236b1c932682136dc35a9dee8d3589728f68130c7c3861ef96b28" dependencies = [ "proc-macro2", "pyo3-macros-backend", @@ -405,9 +405,9 @@ dependencies = [ [[package]] name = "pyo3-macros-backend" -version = "0.17.2" +version = "0.17.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f22bb49f6a7348c253d7ac67a6875f2dc65f36c2ae64a82c381d528972bea6d6" +checksum = "c8df9be978a2d2f0cdebabb03206ed73b11314701a5bfe71b0d753b81997777f" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 193b2403e..44398438f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,7 +35,7 @@ serde_json = "1.0" rustworkx-core = { path = "rustworkx-core", version = "=0.12.0" } [dependencies.pyo3] -version = "0.17.2" +version = "0.17.3" features = ["extension-module", "hashbrown", "num-bigint", "num-complex", "indexmap"] [dependencies.hashbrown] diff --git a/releasenotes/notes/fix-sequence-protocol-e95246e864cc850a.yaml b/releasenotes/notes/fix-sequence-protocol-e95246e864cc850a.yaml new file mode 100644 index 000000000..4bfb6fe37 --- /dev/null +++ b/releasenotes/notes/fix-sequence-protocol-e95246e864cc850a.yaml @@ -0,0 +1,10 @@ +--- +fixes: + - | + Fixed an issue with the custom sequence return types, + :class:`~.BFSSuccessors`, :class:`~.NodeIndices`, :class:`~.EdgeList`, + :class:`~.WeightedEdgeList`, :class:`~.EdgeIndices`, and :class:`~.Chains` + where they previosuly were missing certain attributes that prevented them + being used as a sequence for certain built-in functions such as + ``reversed()``. + Fixed `#696 `__. diff --git a/src/iterators.rs b/src/iterators.rs index 3a403d492..b6e496cc5 100644 --- a/src/iterators.rs +++ b/src/iterators.rs @@ -475,7 +475,7 @@ impl PyConvertToPyArray for Vec<(usize, usize, PyObject)> { macro_rules! custom_vec_iter_impl { ($name:ident, $data:ident, $T:ty, $doc:literal) => { #[doc = $doc] - #[pyclass(module = "rustworkx")] + #[pyclass(module = "rustworkx", sequence)] #[derive(Clone)] pub struct $name { pub $data: Vec<$T>, diff --git a/tests/rustworkx_tests/test_custom_return_types.py b/tests/rustworkx_tests/test_custom_return_types.py index 8abf87ae2..2362f125b 100644 --- a/tests/rustworkx_tests/test_custom_return_types.py +++ b/tests/rustworkx_tests/test_custom_return_types.py @@ -174,6 +174,12 @@ def test_slices_negatives(self): self.assertEqual([2, 3], slice_return) self.assertEqual([], indices[-1:-2]) + def test_reversed(self): + indices = self.dag.node_indices() + reversed_slice = indices[::-1] + reversed_elems = list(reversed(indices)) + self.assertEqual(reversed_slice, reversed_elems) + def test_numpy_conversion(self): res = self.dag.node_indexes() np.testing.assert_array_equal(np.asarray(res, dtype=np.uintp), np.array([0, 1]))