Skip to content

Commit

Permalink
Update PyO3 and numpy dependencies to 0.23 (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
prehner authored Dec 5, 2024
1 parent 7eda3bd commit 1560c46
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 54 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
- name: Build Wheels
uses: PyO3/maturin-action@v1
with:
Expand All @@ -48,7 +48,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
architecture: x64
- name: Build wheels - x86_64
uses: PyO3/maturin-action@v1
Expand All @@ -67,7 +67,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
architecture: arm64
- name: Build wheels - aarch64
uses: PyO3/maturin-action@v1
Expand All @@ -86,7 +86,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
architecture: x64
- name: Build wheels
uses: PyO3/maturin-action@v1
Expand All @@ -111,7 +111,7 @@ jobs:
merge-multiple: true
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
- name: Publish to PyPi
env:
TWINE_USERNAME: __token__
Expand All @@ -127,7 +127,7 @@ jobs:
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
- name: Install python dependencies
run: |
pip install sphinx sphinx-rtd-theme numpydoc
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test_documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup python
uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
- name: Install python dependencies
run: |
pip install sphinx sphinx-rtd-theme numpydoc numpy torch
Expand Down
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"]
python-version: ["3.10", "3.11", "3.12", "3.13"]

steps:
- uses: actions/checkout@v4
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/wheels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
- name: Build Wheels
uses: PyO3/maturin-action@v1
with:
Expand All @@ -24,12 +24,12 @@ jobs:
name: wheel-linux-x86_64
path: dist
macos-x86_64:
runs-on: macos-12
runs-on: macos-14
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
architecture: x64
- name: Build wheels - x86_64
uses: PyO3/maturin-action@v1
Expand All @@ -47,7 +47,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
architecture: arm64
- name: Build wheels - aarch64
uses: PyO3/maturin-action@v1
Expand All @@ -65,7 +65,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: 3.9
python-version: 3.11
architecture: x64
- name: Build wheels
uses: PyO3/maturin-action@v1
Expand Down
9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ exclude = ["/.github/*", "*.ipynb", "/docs"]
features = ["python_numpy", "num-dual", "approx"]
rustdoc-args = ["--html-in-header", "./src/docs-header.html"]

[workspace]
members = ["si-units", "example/extend_quantity"]

[dependencies]
typenum = "1.17"
document-features = "0.2"
## Use N-dimensional arrays from the [ndarray] crate as value of a quantity.
ndarray = { version = "0.16", optional = true}
ndarray = { version = "0.16", optional = true }
approx = { version = "0.5", optional = true }
pyo3 = { version = "0.22", optional = true }
numpy = { version = "0.22", optional = true }
pyo3 = { version = "0.23", optional = true }
numpy = { version = "0.23", optional = true }
## Use generalized (hyper-)dual numbers from the [num-dual] crate as value of a quantity.
num-dual = { version = "0.10", optional = true }

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

[dependencies]
pyo3 = { version = "0.22.0", features = ["extension-module", "abi3-py39"] }
pyo3 = { version = "0.23.0", features = ["extension-module", "abi3-py39"] }
quantity = { version = "*", path = "../../", features = ["python_numpy"] }
ndarray = "0.16"
8 changes: 4 additions & 4 deletions si-units/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ crate-type = ["cdylib"]

[dependencies]
ndarray = "0.16"
numpy = "0.22"
thiserror = "1.0"
numpy = "0.23"
thiserror = "2.0"
regex = "1.10"

[dependencies.pyo3]
version = "0.22"
features = ["extension-module", "abi3", "abi3-py37"]
version = "0.23"
features = ["extension-module", "abi3", "abi3-py37"]
14 changes: 7 additions & 7 deletions si-units/src/extra_units.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ pub struct Celsius;
#[pymethods]
impl Celsius {
fn __rmul__(&self, lhs: &Bound<'_, PyAny>) -> PyResult<PySIObject> {
let delta: Py<PyAny> = 273.15.into_py(lhs.py());
let delta = delta.bind(lhs.py());
let mut value = lhs.call_method1("__add__", (delta,))?;
if PyNotImplemented::is_exact_type_of_bound(&value) {
let delta = PyFloat::new(lhs.py(), 273.15);
let mut value = lhs.call_method1("__add__", (&delta,))?;
if PyNotImplemented::is_exact_type_of(&value) {
value = delta.call_method1("__add__", (lhs,))?;
}
Ok(PySIObject::new(value.unbind(), _KELVIN))
Expand Down Expand Up @@ -48,9 +47,10 @@ impl Debye {
if n % 2 == 1 {
Err(QuantityError::DebyePower)?
} else {
let value = (self.0.powi(2) * 1e-19 * 1e-30).powi(n / 2);
let value = PyFloat::new(py, (self.0.powi(2) * 1e-19 * 1e-30).powi(n / 2));
let value = value.into_any().unbind();
let unit = (_JOULE * _METER.powi(3)).powi(n / 2);
Ok(PySIObject::new(value.into_py(py), unit))
Ok(PySIObject::new(value, unit))
}
}
}
Expand Down Expand Up @@ -98,7 +98,7 @@ impl Angle {
if let Ok(r) = rhs.extract::<f64>() {
Ok(Bound::new(rhs.py(), Self(self.0 / r))?.into_any())
} else if let Ok(r) = rhs.extract::<Self>() {
Ok(PyFloat::new_bound(rhs.py(), self.0 / r.0).into_any())
Ok(PyFloat::new(rhs.py(), self.0 / r.0).into_any())
} else {
Err(PyErr::new::<PyTypeError, _>(format!(
"Can't divide angle by {}",
Expand Down
2 changes: 1 addition & 1 deletion si-units/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ fn unit_to_latex_product(vec: Vec<(&str, i8)>) -> Option<String> {
}
}

impl<'a, D: Dimension> fmt::Display for SIArray<'a, D> {
impl<D: Dimension> fmt::Display for SIArray<'_, D> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match DERIVED_UNITS.get(&self.unit) {
Some((unit, symbol, _, _, _)) => {
Expand Down
25 changes: 13 additions & 12 deletions si-units/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl PySIObject {
/// 1 m
pub fn sqrt(&self, py: Python) -> PyResult<Self> {
let value = if let Ok(v) = self.value.extract::<f64>(py) {
v.sqrt().into_py(py)
PyFloat::new(py, v.sqrt()).into_any().unbind()
} else {
self.value.call_method0(py, "sqrt")?
};
Expand All @@ -141,7 +141,7 @@ impl PySIObject {
/// 1 m
pub fn cbrt(&self, py: Python) -> PyResult<Self> {
let value = if let Ok(v) = self.value.extract::<f64>(py) {
v.cbrt().into_py(py)
PyFloat::new(py, v.cbrt()).into_any().unbind()
} else {
self.value.call_method0(py, "cbrt")?
};
Expand Down Expand Up @@ -195,7 +195,7 @@ impl PySIObject {
.value
.bind(rhs.py())
.call_method1("__mul__", (&rhs_value,))?;
if PyNotImplemented::is_exact_type_of_bound(&value) {
if PyNotImplemented::is_exact_type_of(&value) {
value = rhs_value.call_method1("__rmul__", (&self.value,))?;
}
Self::new_unit_checked(value, unit)
Expand All @@ -211,7 +211,7 @@ impl PySIObject {
.value
.bind(lhs.py())
.call_method1("__rmul__", (&lhs_value,))?;
if PyNotImplemented::is_exact_type_of_bound(&value) {
if PyNotImplemented::is_exact_type_of(&value) {
value = lhs_value.call_method1("__mul__", (&self.value,))?;
}
Self::new_unit_checked(value, unit)
Expand All @@ -222,7 +222,7 @@ impl PySIObject {
(r.value.bind(rhs.py()).clone(), self.unit / r.unit)
} else if rhs.downcast::<Celsius>().is_ok() {
return if self.unit == _KELVIN {
let delta = PyFloat::new_bound(rhs.py(), 273.15);
let delta = PyFloat::new(rhs.py(), 273.15);
self.value.bind(rhs.py()).call_method1("__sub__", (&delta,))
} else {
Err(QuantityError::InconsistentUnits {
Expand All @@ -237,7 +237,7 @@ impl PySIObject {
.value
.bind(rhs.py())
.call_method1("__truediv__", (&rhs_value,))?;
if PyNotImplemented::is_exact_type_of_bound(&value) {
if PyNotImplemented::is_exact_type_of(&value) {
value = rhs_value.call_method1("__rtruediv__", (&self.value,))?;
}
Self::new_unit_checked(value, unit)
Expand All @@ -253,7 +253,7 @@ impl PySIObject {
.value
.bind(lhs.py())
.call_method1("__rtruediv__", (&lhs_value,))?;
if PyNotImplemented::is_exact_type_of_bound(&value) {
if PyNotImplemented::is_exact_type_of(&value) {
value = lhs_value.call_method1("__truediv__", (&self.value,))?;
}
Self::new_unit_checked(value, unit)
Expand Down Expand Up @@ -339,7 +339,7 @@ impl SIArray1 {
let py = value.py();
if let Ok(v) = value.extract::<SINumber>() {
let value = arr1(&[1.0]) * v.value;
let value = value.into_pyarray_bound(py).into_any().unbind();
let value = value.into_pyarray(py).into_any().unbind();
Bound::new(py, PySIObject::new(value, v.unit))
} else if let Ok(v) = value.extract::<Vec<SINumber>>() {
let mut unit = SIUnit::DIMENSIONLESS;
Expand All @@ -355,7 +355,7 @@ impl SIArray1 {
}
}
let value: Array1<_> = Array1::from_vec(value);
let value = value.into_pyarray_bound(py).into_any().unbind();
let value = value.into_pyarray(py).into_any().unbind();
Bound::new(py, PySIObject::new(value, unit))
} else {
Ok(value.downcast_into::<PySIObject>()?)
Expand Down Expand Up @@ -386,7 +386,7 @@ impl SIArray1 {
) -> Result<PySIObject, QuantityError> {
if start.unit == end.unit {
let value = Array1::linspace(start.value, end.value, n);
let value = value.into_pyarray_bound(py).into_any().unbind();
let value = value.into_pyarray(py).into_any().unbind();
Ok(PySIObject::new(value, start.unit))
} else {
Err(QuantityError::InconsistentUnits {
Expand Down Expand Up @@ -420,7 +420,7 @@ impl SIArray1 {
) -> Result<PySIObject, QuantityError> {
if start.unit == end.unit {
let value = Array1::logspace(10.0, start.value.log10(), end.value.log10(), n);
let value = value.into_pyarray_bound(py).into_any().unbind();
let value = value.into_pyarray(py).into_any().unbind();
Ok(PySIObject::new(value, start.unit))
} else {
Err(QuantityError::InconsistentUnits {
Expand Down Expand Up @@ -594,5 +594,6 @@ pub fn si_units(m: &Bound<'_, PyModule>) -> PyResult<()> {
}

fn add_constant(m: &Bound<'_, PyModule>, name: &str, value: f64, unit: SIUnit) -> PyResult<()> {
m.add(name, PySIObject::new(value.into_py(m.py()), unit))
let value = PyFloat::new(m.py(), value).into_any().unbind();
m.add(name, PySIObject::new(value, unit))
}
Loading

0 comments on commit 1560c46

Please sign in to comment.