Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/test_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9", "3.10", "3.11", "3.12", "3.13"]
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]

steps:
- uses: actions/checkout@v4
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ ndarray = { version = "0.16", optional = true }
## Use dynamic or static arrays from the [nalgebra] crate as value of a quantity.
nalgebra = { version = "0.34", optional = true }
approx = { version = "0.5", optional = true }
pyo3 = { version = "0.26", optional = true }
numpy = { version = "0.26", optional = true }
pyo3 = { version = "0.27", optional = true }
numpy = { version = "0.27", optional = true }
num-dual = { version = "0.12", optional = true }

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

2 changes: 1 addition & 1 deletion example/extend_quantity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ name = "extend_quantity"
crate-type = ["cdylib"]

[dependencies]
pyo3 = { version = "0.26", features = ["extension-module", "abi3-py39"] }
pyo3 = { version = "0.27", features = ["extension-module", "abi3-py310"] }
quantity = { version = "*", path = "../../", features = ["python_numpy"] }
ndarray = "0.16"
nalgebra = "0.34"
6 changes: 3 additions & 3 deletions si-units/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ crate-type = ["cdylib"]

[dependencies]
ndarray = "0.16"
numpy = "0.26"
pyo3 = { version = "0.26", features = ["extension-module", "abi3-py39"] }
regex = "1.11"
numpy = "0.27"
pyo3 = { version = "0.27", features = ["extension-module", "abi3-py310"] }
regex = "1.12"
thiserror = "2.0"
21 changes: 11 additions & 10 deletions si-units/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![warn(clippy::all)]
#![allow(non_snake_case)]
use ndarray::{arr1, Array1};
use ndarray::{Array1, arr1};
use numpy::IntoPyArray;
use pyo3::basic::CompareOp;
use pyo3::exceptions::PyRuntimeError;
Expand Down Expand Up @@ -150,7 +150,7 @@ impl PySIObject {
}

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

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

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

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

impl<'py> FromPyObject<'py> for SINumber {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
impl<'py> FromPyObject<'_, 'py> for SINumber {
type Error = PyErr;
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let py = ob.py();
let ob = ob.downcast::<PySIObject>()?.borrow();
let ob = ob.cast::<PySIObject>()?.borrow();
let value = ob.value.extract::<f64>(py)?;
let unit = ob.unit;
Ok(SINumber { value, unit })
Expand Down Expand Up @@ -317,7 +318,7 @@ fn array(value: Bound<'_, PyAny>) -> PyResult<Bound<'_, PySIObject>> {
let value = value.into_pyarray(py).into_any().unbind();
Bound::new(py, PySIObject::new(value, unit))
} else {
Ok(value.downcast_into::<PySIObject>()?)
Ok(value.cast_into::<PySIObject>()?)
}
}

Expand Down
26 changes: 16 additions & 10 deletions src/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ impl<'py, T: Integer, L: Integer, M: Integer, I: Integer, THETA: Integer, N: Int
}

impl<'py, T: Integer, L: Integer, M: Integer, I: Integer, THETA: Integer, N: Integer, J: Integer>
FromPyObject<'py> for Quantity<f64, SIUnit<T, L, M, I, THETA, N, J>>
FromPyObject<'_, 'py> for Quantity<f64, SIUnit<T, L, M, I, THETA, N, J>>
where
Self: PrintUnit,
{
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
type Error = PyErr;
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let Ok((value, unit_from)) = ob
.call_method0("__getnewargs__")
.and_then(|raw| raw.extract::<(f64, [i8; 7])>())
Expand Down Expand Up @@ -131,11 +132,12 @@ impl<
N: Integer,
J: Integer,
D: Dimension,
> FromPyObject<'py> for Quantity<Array<f64, D>, SIUnit<T, L, M, I, THETA, N, J>>
> FromPyObject<'_, 'py> for Quantity<Array<f64, D>, SIUnit<T, L, M, I, THETA, N, J>>
where
Self: PrintUnit,
{
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
type Error = PyErr;
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let Ok((value, unit_from)) = ob
.call_method0("__getnewargs__")
.and_then(|raw| raw.extract::<(PyReadonlyArray<f64, D>, [i8; 7])>())
Expand All @@ -162,11 +164,12 @@ where

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

#[cfg(feature = "nalgebra")]
impl<'py, T: Integer, L: Integer, M: Integer, I: Integer, THETA: Integer, N: Integer, J: Integer>
FromPyObject<'py> for Quantity<DMatrix<f64>, SIUnit<T, L, M, I, THETA, N, J>>
FromPyObject<'_, 'py> for Quantity<DMatrix<f64>, SIUnit<T, L, M, I, THETA, N, J>>
where
Self: PrintUnit,
{
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
type Error = PyErr;
fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let Ok((value, unit_from)) = ob.call_method0("__getnewargs__").and_then(|raw| {
raw.extract::<(PyReadonlyArray2<f64>, [i8; 7])>()
.map(|(m, u)| {
Expand Down Expand Up @@ -245,8 +249,10 @@ impl<'py> IntoPyObject<'py> for Angle {
}
}

impl<'py> FromPyObject<'py> for Angle {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
impl<'py> FromPyObject<'_, 'py> for Angle {
type Error = PyErr;

fn extract(ob: Borrowed<'_, 'py, PyAny>) -> PyResult<Self> {
let Ok(value) = ob
.call_method0("__getnewargs__")
.and_then(|raw| raw.extract::<f64>())
Expand Down