Skip to content

Commit

Permalink
Fix support of sequence protocol for returned lists
Browse files Browse the repository at this point in the history
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 Qiskit#696.

[1]: PyO3/pyo3#2567
  • Loading branch information
tuxu committed Nov 7, 2022
1 parent 53f0e31 commit f95aecd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/iterators.rs
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
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

0 comments on commit f95aecd

Please sign in to comment.