From f32f8fc16347e9e109c6085a18b6ded2c037c894 Mon Sep 17 00:00:00 2001 From: JHM Darbyshire <24256554+attack68@users.noreply.github.com> Date: Sun, 26 Jan 2025 18:41:24 +0100 Subject: [PATCH] UPG: rust packages upgrades (#655) --- Cargo.toml | 10 ++++----- rust/calendars/calendar.rs | 4 ++-- rust/calendars/calendar_py.rs | 31 ++++++++++++++-------------- rust/curves/curve_py.rs | 39 ++++++++++++++++++----------------- rust/dual/dual_py.rs | 31 ++++++++++++++++------------ rust/dual/enums.rs | 4 ++-- 6 files changed, 63 insertions(+), 56 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cd1829731..0c822dd02 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,15 +29,15 @@ path = "rust/main.rs" #pyo3 = { version = "0.20.3", features = ["abi3-py39", "extension-module"] } serde = { version = "1.0", features = ["derive", "rc"] } chrono = { version = "0.4", features = ["serde"] } -indexmap = { version = "2.0", features = ["serde"] } +indexmap = { version = "2.7", features = ["serde"] } ndarray = { version = "0.16", features = ["serde"] } internment = { version = "0.8", features = ["serde"] } -pyo3 = "0.22" +pyo3 = "0.23" num-traits = "0.2" auto_ops = "0.3" -numpy = "0.22" -itertools = "0.12" -statrs = "0.16" +numpy = "0.23" +itertools = "0.14" +statrs = "0.18" bincode = "1.3" serde_json = "1.0" diff --git a/rust/calendars/calendar.rs b/rust/calendars/calendar.rs index 4e6c3e55b..0d6323ff5 100644 --- a/rust/calendars/calendar.rs +++ b/rust/calendars/calendar.rs @@ -2,7 +2,7 @@ use chrono::prelude::*; use chrono::Weekday; use indexmap::set::IndexSet; use pyo3::exceptions::PyValueError; -use pyo3::{pyclass, FromPyObject, PyErr}; +use pyo3::{pyclass, FromPyObject, PyErr, IntoPyObject}; use serde::{Deserialize, Serialize}; use std::collections::HashSet; @@ -10,7 +10,7 @@ use crate::calendars::dateroll::DateRoll; use crate::calendars::named::get_calendar_by_name; /// Container for calendar types. -#[derive(Debug, Clone, PartialEq, FromPyObject, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, FromPyObject, Serialize, Deserialize, IntoPyObject)] pub enum CalType { Cal(Cal), UnionCal(UnionCal), diff --git a/rust/calendars/calendar_py.rs b/rust/calendars/calendar_py.rs index 9a8fc3c54..ae1490632 100644 --- a/rust/calendars/calendar_py.rs +++ b/rust/calendars/calendar_py.rs @@ -12,21 +12,22 @@ use pyo3::prelude::*; use pyo3::types::PyBytes; use std::collections::HashSet; -impl IntoPy for CalType { - fn into_py(self, py: Python<'_>) -> PyObject { - macro_rules! into_py { - ($obj: ident) => { - Py::new(py, $obj).unwrap().to_object(py) - }; - } - - match self { - CalType::Cal(i) => into_py!(i), - CalType::UnionCal(i) => into_py!(i), - CalType::NamedCal(i) => into_py!(i), - } - } -} +// // removed when upgrading to py03 0.23, see https://pyo3.rs/v0.23.0/migration#intopyobject-and-intopyobjectref-derive-macros +// impl IntoPy for CalType { +// fn into_py(self, py: Python<'_>) -> PyObject { +// macro_rules! into_py { +// ($obj: ident) => { +// Py::new(py, $obj).unwrap().to_object(py) +// }; +// } +// +// match self { +// CalType::Cal(i) => into_py!(i), +// CalType::UnionCal(i) => into_py!(i), +// CalType::NamedCal(i) => into_py!(i), +// } +// } +// } #[pymethods] impl Convention { diff --git a/rust/curves/curve_py.rs b/rust/curves/curve_py.rs index 28955d4c5..92c72dcf9 100644 --- a/rust/curves/curve_py.rs +++ b/rust/curves/curve_py.rs @@ -19,7 +19,7 @@ use pyo3::types::PyBytes; use serde::{Deserialize, Serialize}; /// Interpolation -#[derive(Debug, Clone, PartialEq, FromPyObject, Deserialize, Serialize)] +#[derive(Debug, Clone, PartialEq, FromPyObject, Deserialize, Serialize, IntoPyObject)] pub(crate) enum CurveInterpolator { LogLinear(LogLinearInterpolator), Linear(LinearInterpolator), @@ -29,24 +29,25 @@ pub(crate) enum CurveInterpolator { Null(NullInterpolator), } -impl IntoPy for CurveInterpolator { - fn into_py(self, py: Python<'_>) -> PyObject { - macro_rules! into_py { - ($obj: ident) => { - Py::new(py, $obj).unwrap().to_object(py) - }; - } - - match self { - CurveInterpolator::LogLinear(i) => into_py!(i), - CurveInterpolator::Linear(i) => into_py!(i), - CurveInterpolator::LinearZeroRate(i) => into_py!(i), - CurveInterpolator::FlatForward(i) => into_py!(i), - CurveInterpolator::FlatBackward(i) => into_py!(i), - CurveInterpolator::Null(i) => into_py!(i), - } - } -} +// // removed upgrading to pyo3 0.23, see https://pyo3.rs/v0.23.0/migration#intopyobject-and-intopyobjectref-derive-macros +// impl IntoPy for CurveInterpolator { +// fn into_py(self, py: Python<'_>) -> PyObject { +// macro_rules! into_py { +// ($obj: ident) => { +// Py::new(py, $obj).unwrap().to_object(py) +// }; +// } +// +// match self { +// CurveInterpolator::LogLinear(i) => into_py!(i), +// CurveInterpolator::Linear(i) => into_py!(i), +// CurveInterpolator::LinearZeroRate(i) => into_py!(i), +// CurveInterpolator::FlatForward(i) => into_py!(i), +// CurveInterpolator::FlatBackward(i) => into_py!(i), +// CurveInterpolator::Null(i) => into_py!(i), +// } +// } +// } impl CurveInterpolation for CurveInterpolator { fn interpolated_value(&self, nodes: &NodesTimestamp, date: &NaiveDateTime) -> Number { diff --git a/rust/dual/dual_py.rs b/rust/dual/dual_py.rs index 62fd633fd..48c2ee23d 100644 --- a/rust/dual/dual_py.rs +++ b/rust/dual/dual_py.rs @@ -16,7 +16,7 @@ use numpy::{Element, PyArray1, PyArray2, PyArrayDescr, ToPyArray}; unsafe impl Element for Dual { const IS_COPY: bool = false; - fn get_dtype_bound(py: Python<'_>) -> Bound<'_, PyArrayDescr> { + fn get_dtype(py: Python<'_>) -> Bound<'_, PyArrayDescr> { PyArrayDescr::object_bound(py) } @@ -26,7 +26,7 @@ unsafe impl Element for Dual { } unsafe impl Element for Dual2 { const IS_COPY: bool = false; - fn get_dtype_bound(py: Python<'_>) -> Bound<'_, PyArrayDescr> { + fn get_dtype(py: Python<'_>) -> Bound<'_, PyArrayDescr> { PyArrayDescr::object_bound(py) } @@ -35,15 +35,16 @@ unsafe impl Element for Dual2 { } } -impl IntoPy for Number { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Number::F64(f) => PyFloat::new_bound(py, f).to_object(py), - Number::Dual(d) => Py::new(py, d).unwrap().to_object(py), - Number::Dual2(d) => Py::new(py, d).unwrap().to_object(py), - } - } -} +// This was removed when upgrading to pyO3 0.23: see https://pyo3.rs/v0.23.0/migration#intopyobject-and-intopyobjectref-derive-macros +// impl IntoPy for Number { +// fn into_py(self, py: Python<'_>) -> PyObject { +// match self { +// Number::F64(f) => PyFloat::new_bound(py, f).to_object(py), +// Number::Dual(d) => Py::new(py, d).unwrap().to_object(py), +// Number::Dual2(d) => Py::new(py, d).unwrap().to_object(py), +// } +// } +// } // https://github.com/PyO3/pyo3/discussions/3911 // #[derive(Debug, Clone, PartialEq, PartialOrd, FromPyObject)] @@ -151,7 +152,7 @@ impl Dual { #[getter] #[pyo3(name = "dual2")] - fn dual2_py<'py>(&'py self, _py: Python<'py>) -> PyResult<&'py PyArray2> { + fn dual2_py<'py>(&'py self, _py: Python<'py>) -> PyResult>> { Err(PyValueError::new_err( "`Dual` variable cannot possess `dual2` attribute.", )) @@ -167,7 +168,11 @@ impl Dual { } #[pyo3(name = "grad2")] - fn grad2<'py>(&'py self, _py: Python<'py>, _vars: Vec) -> PyResult<&'py PyArray2> { + fn grad2<'py>( + &'py self, + _py: Python<'py>, + _vars: Vec + ) -> PyResult>> { Err(PyValueError::new_err( "Cannot evaluate second order derivative on a Dual.", )) diff --git a/rust/dual/enums.rs b/rust/dual/enums.rs index 78fbdffe2..3c2c8034b 100644 --- a/rust/dual/enums.rs +++ b/rust/dual/enums.rs @@ -1,7 +1,7 @@ use crate::dual::{Dual, Dual2}; use crate::splines::{PPSplineDual, PPSplineDual2, PPSplineF64}; use ndarray::{Array1, Array2}; -use pyo3::{pyclass, FromPyObject, PyErr}; +use pyo3::{pyclass, FromPyObject, PyErr, IntoPyObject}; use serde::{Deserialize, Serialize}; /// Defines the order of gradients available in a calculation with AD. @@ -17,7 +17,7 @@ pub enum ADOrder { } /// Container for the three core numeric types; [f64], [Dual] and [Dual2]. -#[derive(Debug, Clone, FromPyObject, Serialize, Deserialize)] +#[derive(Debug, Clone, FromPyObject, Serialize, Deserialize, IntoPyObject)] pub enum Number { Dual(Dual), Dual2(Dual2),