Skip to content

Commit c6be13e

Browse files
jakelishmanraynelfss
authored andcommitted
Update to PyO3 0.27 (Qiskit#15213)
* Update PyO3 dependencies * Switch uses of `Bound::downcast*` to corresponding `cast*` The new `cast` variants are available on both `Bound` and `Borrowed`, and have slightly different semantics around the returned errors. * Switch to new `FromPyObject` signature The new form of the trait uses `Borrowed`, causing it to need another lifetime parameter, and an explicit `Error` type. Where appropriate / easy, I switched the error types away from the complete `PyErr`, even if most of our handling of those errors does eventually convert them into `PyErr` for now. In places where extraction delegates to other methods, particularly where those methods are logical mirrors to `FromPyObject`, this commit also updates the signatures to use `Borrowed` instead of `&Bound`. In a couple of places, the extraction being in terms of `Borrowed` meant that trait implementations that are only accessible on `Bound` (for example `impl IntoIterator for &Bound<PyDict>`) needed some dereferencing inserted to re-satisfy the bounds. * Switch to undeprecated `import_exception` * Revert failure message change
1 parent 3393a30 commit c6be13e

55 files changed

Lines changed: 443 additions & 368 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 16 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ license = "Apache-2.0"
1717
bytemuck = "1.24"
1818
bitfield-struct = "0.12.1"
1919
indexmap.version = "2.10.0"
20-
hashbrown.version = "0.15.2"
20+
hashbrown.version = "0.15.5"
2121
num-bigint = "0.4"
2222
num-complex = "0.4"
2323
nalgebra = "0.33"
24-
numpy = "0.26"
24+
numpy = "0.27"
2525
ndarray = "0.16"
2626
smallvec = "1.15"
2727
thiserror = "2.0"
@@ -44,7 +44,7 @@ uuid = { version = "1.18", features = ["v4", "fast-rng"], default-features = fal
4444
# distributions). We only activate that feature when building the C extension module; we still need
4545
# it disabled for Rust-only tests to avoid linker errors with it not being loaded. See
4646
# https://pyo3.rs/main/features#extension-module for more.
47-
pyo3 = { version = "0.26", features = ["abi3-py39"] }
47+
pyo3 = { version = "0.27", features = ["abi3-py39"] }
4848

4949
# These are our own crates.
5050
qiskit-accelerate = { path = "crates/accelerate" }

crates/circuit/src/bit.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,28 @@ where
8686
self.index as usize,
8787
self.registers
8888
.into_pyobject(py)?
89-
.downcast_into::<PyList>()?
89+
.cast_into::<PyList>()?
9090
.unbind(),
9191
)
9292
.into_pyobject(py)
9393
}
9494
}
9595

96-
impl<'py, R> FromPyObject<'py> for BitLocations<R>
96+
impl<'a, 'py, R> FromPyObject<'a, 'py> for BitLocations<R>
9797
where
98-
R: Debug + Clone + Register + for<'a> IntoPyObject<'a> + for<'a> FromPyObject<'a>,
98+
R: Debug
99+
+ Clone
100+
+ Register
101+
+ IntoPyObject<'py>
102+
+ for<'b> FromPyObject<'b, 'py, Error: Into<PyErr>>,
99103
{
100-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
101-
let ob_down = ob.downcast::<PyBitLocations>()?.borrow();
104+
type Error = PyErr;
105+
106+
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, PyErr> {
107+
let ob_down = ob.cast::<PyBitLocations>()?.borrow();
102108
Ok(Self {
103109
index: ob_down.index as u32,
104-
registers: ob_down.registers.extract(ob.py())?,
110+
registers: ob_down.registers.bind(ob.py()).extract()?,
105111
})
106112
}
107113
}
@@ -509,9 +515,11 @@ macro_rules! create_bit_object {
509515
}
510516
}
511517

512-
impl<'py> FromPyObject<'py> for $bit_struct {
513-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
514-
Ok(ob.downcast::<$pybit_struct>()?.borrow().0.clone())
518+
impl<'a, 'py> FromPyObject<'a, 'py> for $bit_struct {
519+
type Error = ::pyo3::CastError<'a, 'py>;
520+
521+
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
522+
ob.cast::<$pybit_struct>().map(|ob| ob.borrow().0.clone())
515523
}
516524
}
517525
// The owning impl of `IntoPyObject` needs to be done manually, to better handle
@@ -629,9 +637,11 @@ macro_rules! create_bit_object {
629637
}
630638
}
631639

632-
impl<'py> FromPyObject<'py> for $reg_struct {
633-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
634-
Ok(ob.downcast::<$pyreg_struct>()?.borrow().0.clone())
640+
impl<'a, 'py> FromPyObject<'a, 'py> for $reg_struct {
641+
type Error = ::pyo3::CastError<'a, 'py>;
642+
643+
fn extract(ob: Borrowed<'a, 'py, PyAny>) -> Result<Self, Self::Error> {
644+
ob.cast::<$pyreg_struct>().map(|ob| ob.borrow().0.clone())
635645
}
636646
}
637647
// The owning impl of `IntoPyObject` needs to be done manually, to better handle
@@ -761,7 +771,7 @@ macro_rules! create_bit_object {
761771
Ok(PyList::new(ob.py(), s.into_iter().map(get_inner))?.into_any())
762772
}
763773
}
764-
} else if let Ok(list) = ob.downcast::<PyList>() {
774+
} else if let Ok(list) = ob.cast::<PyList>() {
765775
let out = PyList::empty(ob.py());
766776
for item in list.iter() {
767777
out.append(get_inner(PySequenceIndex::convert_idx(

crates/circuit/src/bit_locator.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,13 @@ where
102102

103103
impl<B, R> BitLocator<B, R>
104104
where
105-
B: Debug + Clone + Hash + Eq + for<'py> IntoPyObject<'py> + for<'py> FromPyObject<'py>,
106-
R: Register + Debug + Clone + for<'py> IntoPyObject<'py> + for<'py> FromPyObject<'py>,
105+
B: Debug
106+
+ Clone
107+
+ Hash
108+
+ Eq
109+
+ for<'py> IntoPyObject<'py>
110+
+ for<'a, 'py> FromPyObject<'a, 'py, Error: Into<PyErr>>,
111+
R: Register + Debug + Clone + for<'py> IntoPyObject<'py> + for<'a, 'py> FromPyObject<'a, 'py>,
107112
{
108113
/// Get or create the cached Python dictionary that represents this.
109114
pub fn cached(&self, py: Python) -> &Py<PyDict> {
@@ -121,7 +126,10 @@ where
121126
pub fn from_py_dict(dict: &Bound<PyDict>) -> PyResult<Self> {
122127
let mut locator = Self::new();
123128
for (key, value) in dict {
124-
locator.insert(key.extract()?, value.extract()?);
129+
locator.insert(
130+
key.extract().map_err(Into::<PyErr>::into)?,
131+
value.extract()?,
132+
);
125133
}
126134
Ok(locator)
127135
}

0 commit comments

Comments
 (0)