Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support of sequence protocol for returned lists #730

Merged
merged 2 commits into from
Nov 9, 2022
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
10 changes: 10 additions & 0 deletions releasenotes/notes/fix-sequence-protocol-e95246e864cc850a.yaml
Original file line number Diff line number Diff line change
@@ -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 <https://github.com/Qiskit/rustworkx/issues/696>`__.
2 changes: 1 addition & 1 deletion src/iterators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
6 changes: 6 additions & 0 deletions tests/rustworkx_tests/test_custom_return_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down