Skip to content

Commit d3a0c46

Browse files
authored
Update to PyO3 0.27 (#93)
1 parent 99734c8 commit d3a0c46

6 files changed

Lines changed: 35 additions & 27 deletions

File tree

.github/workflows/test_python.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
17+
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
1818

1919
steps:
2020
- uses: actions/checkout@v4

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ ndarray = { version = "0.16", optional = true }
3232
## Use dynamic or static arrays from the [nalgebra] crate as value of a quantity.
3333
nalgebra = { version = "0.34", optional = true }
3434
approx = { version = "0.5", optional = true }
35-
pyo3 = { version = "0.26", optional = true }
36-
numpy = { version = "0.26", optional = true }
35+
pyo3 = { version = "0.27", optional = true }
36+
numpy = { version = "0.27", optional = true }
3737
num-dual = { version = "0.12", optional = true }
3838

3939
[features]
@@ -46,3 +46,4 @@ python = ["pyo3"]
4646
python_numpy = ["python", "numpy/nalgebra", "ndarray", "nalgebra"]
4747
## Enable approximate comparisons through the [approx] crate.
4848
approx = ["dep:approx", "ndarray?/approx"]
49+

example/extend_quantity/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ name = "extend_quantity"
88
crate-type = ["cdylib"]
99

1010
[dependencies]
11-
pyo3 = { version = "0.26", features = ["extension-module", "abi3-py39"] }
11+
pyo3 = { version = "0.27", features = ["extension-module", "abi3-py310"] }
1212
quantity = { version = "*", path = "../../", features = ["python_numpy"] }
1313
ndarray = "0.16"
1414
nalgebra = "0.34"

si-units/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ crate-type = ["cdylib"]
2222

2323
[dependencies]
2424
ndarray = "0.16"
25-
numpy = "0.26"
26-
pyo3 = { version = "0.26", features = ["extension-module", "abi3-py39"] }
27-
regex = "1.11"
25+
numpy = "0.27"
26+
pyo3 = { version = "0.27", features = ["extension-module", "abi3-py310"] }
27+
regex = "1.12"
2828
thiserror = "2.0"

si-units/src/lib.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![warn(clippy::all)]
22
#![allow(non_snake_case)]
3-
use ndarray::{arr1, Array1};
3+
use ndarray::{Array1, arr1};
44
use numpy::IntoPyArray;
55
use pyo3::basic::CompareOp;
66
use pyo3::exceptions::PyRuntimeError;
@@ -150,7 +150,7 @@ impl PySIObject {
150150
}
151151

152152
fn __mul__<'py>(&self, rhs: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
153-
let (rhs_value, unit) = if let Ok(r) = rhs.downcast::<Self>().map(Bound::get) {
153+
let (rhs_value, unit) = if let Ok(r) = rhs.cast::<Self>().map(Bound::get) {
154154
(r.value.bind(rhs.py()).clone(), self.unit * r.unit)
155155
} else {
156156
(rhs.clone(), self.unit)
@@ -166,7 +166,7 @@ impl PySIObject {
166166
}
167167

168168
fn __rmul__<'py>(&self, lhs: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
169-
let (lhs_value, unit) = if let Ok(l) = lhs.downcast::<Self>().map(Bound::get) {
169+
let (lhs_value, unit) = if let Ok(l) = lhs.cast::<Self>().map(Bound::get) {
170170
(l.value.bind(lhs.py()).clone(), l.unit * self.unit)
171171
} else {
172172
(lhs.clone(), self.unit)
@@ -182,9 +182,9 @@ impl PySIObject {
182182
}
183183

184184
fn __truediv__<'py>(&self, rhs: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
185-
let (rhs_value, unit) = if let Ok(r) = rhs.downcast::<Self>().map(Bound::get) {
185+
let (rhs_value, unit) = if let Ok(r) = rhs.cast::<Self>().map(Bound::get) {
186186
(r.value.bind(rhs.py()).clone(), self.unit / r.unit)
187-
} else if rhs.downcast::<Celsius>().is_ok() {
187+
} else if rhs.cast::<Celsius>().is_ok() {
188188
return if self.unit == _KELVIN {
189189
let delta = PyFloat::new(rhs.py(), 273.15);
190190
self.value.bind(rhs.py()).call_method1("__sub__", (&delta,))
@@ -208,7 +208,7 @@ impl PySIObject {
208208
}
209209

210210
fn __rtruediv__<'py>(&self, lhs: &Bound<'py, PyAny>) -> PyResult<Bound<'py, PyAny>> {
211-
let (lhs_value, unit) = if let Ok(l) = lhs.downcast::<Self>().map(Bound::get) {
211+
let (lhs_value, unit) = if let Ok(l) = lhs.cast::<Self>().map(Bound::get) {
212212
(l.value.bind(lhs.py()).clone(), l.unit / self.unit)
213213
} else {
214214
(lhs.clone(), self.unit.recip())
@@ -283,10 +283,11 @@ impl<T> SIObject<T> {
283283
}
284284
}
285285

286-
impl<'py> FromPyObject<'py> for SINumber {
287-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
286+
impl<'py> FromPyObject<'_, 'py> for SINumber {
287+
type Error = PyErr;
288+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
288289
let py = ob.py();
289-
let ob = ob.downcast::<PySIObject>()?.borrow();
290+
let ob = ob.cast::<PySIObject>()?.borrow();
290291
let value = ob.value.extract::<f64>(py)?;
291292
let unit = ob.unit;
292293
Ok(SINumber { value, unit })
@@ -317,7 +318,7 @@ fn array(value: Bound<'_, PyAny>) -> PyResult<Bound<'_, PySIObject>> {
317318
let value = value.into_pyarray(py).into_any().unbind();
318319
Bound::new(py, PySIObject::new(value, unit))
319320
} else {
320-
Ok(value.downcast_into::<PySIObject>()?)
321+
Ok(value.cast_into::<PySIObject>()?)
321322
}
322323
}
323324

src/python.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@ impl<'py, T: Integer, L: Integer, M: Integer, I: Integer, THETA: Integer, N: Int
9292
}
9393

9494
impl<'py, T: Integer, L: Integer, M: Integer, I: Integer, THETA: Integer, N: Integer, J: Integer>
95-
FromPyObject<'py> for Quantity<f64, SIUnit<T, L, M, I, THETA, N, J>>
95+
FromPyObject<'_, 'py> for Quantity<f64, SIUnit<T, L, M, I, THETA, N, J>>
9696
where
9797
Self: PrintUnit,
9898
{
99-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
99+
type Error = PyErr;
100+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
100101
let Ok((value, unit_from)) = ob
101102
.call_method0("__getnewargs__")
102103
.and_then(|raw| raw.extract::<(f64, [i8; 7])>())
@@ -131,11 +132,12 @@ impl<
131132
N: Integer,
132133
J: Integer,
133134
D: Dimension,
134-
> FromPyObject<'py> for Quantity<Array<f64, D>, SIUnit<T, L, M, I, THETA, N, J>>
135+
> FromPyObject<'_, 'py> for Quantity<Array<f64, D>, SIUnit<T, L, M, I, THETA, N, J>>
135136
where
136137
Self: PrintUnit,
137138
{
138-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
139+
type Error = PyErr;
140+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
139141
let Ok((value, unit_from)) = ob
140142
.call_method0("__getnewargs__")
141143
.and_then(|raw| raw.extract::<(PyReadonlyArray<f64, D>, [i8; 7])>())
@@ -162,11 +164,12 @@ where
162164

163165
#[cfg(feature = "nalgebra")]
164166
impl<'py, T: Integer, L: Integer, M: Integer, I: Integer, THETA: Integer, N: Integer, J: Integer>
165-
FromPyObject<'py> for Quantity<DVector<f64>, SIUnit<T, L, M, I, THETA, N, J>>
167+
FromPyObject<'_, 'py> for Quantity<DVector<f64>, SIUnit<T, L, M, I, THETA, N, J>>
166168
where
167169
Self: PrintUnit,
168170
{
169-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
171+
type Error = PyErr;
172+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
170173
let Ok((value, unit_from)) = ob.call_method0("__getnewargs__").and_then(|raw| {
171174
raw.extract::<(PyReadonlyArray1<f64>, [i8; 7])>()
172175
.map(|(m, u)| {
@@ -195,11 +198,12 @@ where
195198

196199
#[cfg(feature = "nalgebra")]
197200
impl<'py, T: Integer, L: Integer, M: Integer, I: Integer, THETA: Integer, N: Integer, J: Integer>
198-
FromPyObject<'py> for Quantity<DMatrix<f64>, SIUnit<T, L, M, I, THETA, N, J>>
201+
FromPyObject<'_, 'py> for Quantity<DMatrix<f64>, SIUnit<T, L, M, I, THETA, N, J>>
199202
where
200203
Self: PrintUnit,
201204
{
202-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
205+
type Error = PyErr;
206+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
203207
let Ok((value, unit_from)) = ob.call_method0("__getnewargs__").and_then(|raw| {
204208
raw.extract::<(PyReadonlyArray2<f64>, [i8; 7])>()
205209
.map(|(m, u)| {
@@ -245,8 +249,10 @@ impl<'py> IntoPyObject<'py> for Angle {
245249
}
246250
}
247251

248-
impl<'py> FromPyObject<'py> for Angle {
249-
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
252+
impl<'py> FromPyObject<'_, 'py> for Angle {
253+
type Error = PyErr;
254+
255+
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
250256
let Ok(value) = ob
251257
.call_method0("__getnewargs__")
252258
.and_then(|raw| raw.extract::<f64>())

0 commit comments

Comments
 (0)