Skip to content

Commit

Permalink
Fix more deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers committed Dec 6, 2024
1 parent 95eb735 commit 3dcf243
Show file tree
Hide file tree
Showing 30 changed files with 111 additions and 151 deletions.
13 changes: 6 additions & 7 deletions crates/polars-python/src/conversion/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ pub(crate) fn py_object_to_any_value<'py>(
let py = ob.py();
let kwargs = PyDict::new(py);
kwargs.set_item("strict", strict)?;
let s = pl_series(py).call_bound(py, (ob,), Some(&kwargs))?;
let s = pl_series(py).call(py, (ob,), Some(&kwargs))?;
get_list_from_series(s.bind(py), strict)
}

Expand All @@ -374,10 +374,10 @@ pub(crate) fn py_object_to_any_value<'py>(
} else if ob.is_instance_of::<PyList>() | ob.is_instance_of::<PyTuple>() {
const INFER_SCHEMA_LENGTH: usize = 25;

let list = ob.downcast::<PySequence>().unwrap();
let list = ob.downcast::<PySequence>()?;

let mut avs = Vec::with_capacity(INFER_SCHEMA_LENGTH);
let mut iter = list.iter()?;
let mut iter = list.try_iter()?;
let mut items = Vec::with_capacity(INFER_SCHEMA_LENGTH);
for item in (&mut iter).take(INFER_SCHEMA_LENGTH) {
items.push(item?);
Expand Down Expand Up @@ -477,7 +477,7 @@ pub(crate) fn py_object_to_any_value<'py>(
Ok(get_struct)
} else {
let ob_type = ob.get_type();
let type_name = ob_type.qualname().unwrap().to_string();
let type_name = ob_type.qualname()?.to_string();
match type_name.as_str() {
// Can't use pyo3::types::PyDateTime with abi3-py37 feature,
// so need this workaround instead of `isinstance(ob, datetime)`.
Expand All @@ -496,10 +496,9 @@ pub(crate) fn py_object_to_any_value<'py>(
}

// Support custom subclasses of datetime/date.
let ancestors = ob_type.getattr(intern!(py, "__mro__")).unwrap();
let ancestors = ob_type.getattr(intern!(py, "__mro__"))?;
let ancestors_str_iter = ancestors
.iter()
.unwrap()
.try_iter()?
.map(|b| b.unwrap().str().unwrap().to_string());
for c in ancestors_str_iter {
match &*c {
Expand Down
16 changes: 8 additions & 8 deletions crates/polars-python/src/conversion/chunked_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,21 @@ impl<'py> IntoPyObject<'py> for &Wrap<&DecimalChunked> {
type Output = Bound<'py, Self::Target>;
type Error = PyErr;
fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
let iter = decimal_to_pyobject_iter(py, self.0);
let iter = decimal_to_pyobject_iter(py, self.0)?;
PyList::new(py, iter)
}
}

pub(crate) fn decimal_to_pyobject_iter<'py, 'a>(
py: Python<'py>,
ca: &'a DecimalChunked,
) -> impl ExactSizeIterator<Item = Option<Bound<'py, PyAny>>> + use<'py, 'a> {
) -> PyResult<impl ExactSizeIterator<Item = Option<Bound<'py, PyAny>>> + use<'py, 'a>> {
let utils = pl_utils(py).bind(py);
let convert = utils.getattr(intern!(py, "to_py_decimal")).unwrap();
let py_scale = (-(ca.scale() as i32)).to_object(py);
let convert = utils.getattr(intern!(py, "to_py_decimal"))?;
let py_scale = (-(ca.scale() as i32)).into_pyobject(py)?;
// if we don't know precision, the only safe bet is to set it to 39
let py_precision = ca.precision().unwrap_or(39).to_object(py);
ca.iter().map(move |opt_v| {
let py_precision = ca.precision().unwrap_or(39).into_pyobject(py)?;
Ok(ca.iter().map(move |opt_v| {
opt_v.map(|v| {
// TODO! use AnyValue so that we have a single impl.
const N: usize = 3;
Expand All @@ -162,10 +162,10 @@ pub(crate) fn decimal_to_pyobject_iter<'py, 'a>(
N * size_of::<u128>(),
)
};
let digits = PyTuple::new_bound(py, buf.iter().take(n_digits));
let digits = PyTuple::new(py, buf.iter().take(n_digits)).unwrap();
convert
.call1((v.is_negative() as u8, digits, &py_precision, &py_scale))
.unwrap()
})
})
}))
}
20 changes: 7 additions & 13 deletions crates/polars-python/src/conversion/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(deprecated)]

pub(crate) mod any_value;
pub(crate) mod chunked_array;
mod datetime;
Expand Down Expand Up @@ -109,12 +107,10 @@ pub(crate) fn get_series(obj: &Bound<'_, PyAny>) -> PyResult<Series> {
Ok(s.extract::<PySeries>()?.series)
}

pub(crate) fn to_series(py: Python, s: PySeries) -> PyObject {
pub(crate) fn to_series(py: Python, s: PySeries) -> PyResult<Bound<PyAny>> {
let series = pl_series(py).bind(py);
let constructor = series
.getattr(intern!(series.py(), "_from_pyseries"))
.unwrap();
constructor.call1((s,)).unwrap().into_py(py)
let constructor = series.getattr(intern!(py, "_from_pyseries"))?;
constructor.call1((s,))
}

impl<'a> FromPyObject<'a> for Wrap<PlSmallStr> {
Expand Down Expand Up @@ -287,7 +283,7 @@ impl<'py> IntoPyObject<'py> for &Wrap<DataType> {
let s =
Series::from_arrow(PlSmallStr::from_static("category"), categories.to_boxed())
.map_err(PyPolarsErr::from)?;
let series = to_series(py, s.into());
let series = to_series(py, s.into())?;
class.call1((series,))
},
DataType::Time => pl.getattr(intern!(py, "Time")),
Expand Down Expand Up @@ -686,10 +682,8 @@ impl From<PyObject> for ObjectValue {

impl<'a> FromPyObject<'a> for ObjectValue {
fn extract_bound(ob: &Bound<'a, PyAny>) -> PyResult<Self> {
Python::with_gil(|py| {
Ok(ObjectValue {
inner: ob.to_object(py),
})
Ok(ObjectValue {
inner: ob.to_owned().unbind(),
})
}
}
Expand Down Expand Up @@ -724,7 +718,7 @@ impl<'a, T: NativeType + FromPyObject<'a>> FromPyObject<'a> for Wrap<Vec<T>> {
fn extract_bound(obj: &Bound<'a, PyAny>) -> PyResult<Self> {
let seq = obj.downcast::<PySequence>()?;
let mut v = Vec::with_capacity(seq.len().unwrap_or(0));
for item in seq.iter()? {
for item in seq.try_iter()? {
v.push(item?.extract::<T>()?);
}
Ok(Wrap(v))
Expand Down
3 changes: 2 additions & 1 deletion crates/polars-python/src/dataframe/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use pyo3::exceptions::PyIndexError;
use pyo3::prelude::*;
use pyo3::pybacked::PyBackedStr;
use pyo3::types::PyList;
use pyo3::IntoPyObjectExt;

use self::row_encode::get_row_encoding_dictionary;
use super::PyDataFrame;
Expand Down Expand Up @@ -568,7 +569,7 @@ impl PyDataFrame {
_ => return apply_lambda_unknown(df, py, lambda, inference_size),
};

Ok((PySeries::from(out).into_py(py), false))
Ok((PySeries::from(out).into_py_any(py)?, false))
})
}

Expand Down
2 changes: 0 additions & 2 deletions crates/polars-python/src/dataframe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(deprecated)]

#[cfg(feature = "pymethods")]
mod construction;
#[cfg(feature = "pymethods")]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/expr/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl PyExpr {
.list()
.to_struct(ListToStructArgs::FixedWidth(
names
.iter()?
.try_iter()?
.map(|x| Ok(x?.extract::<Wrap<PlSmallStr>>()?.0))
.collect::<PyResult<Arc<[_]>>>()?,
))
Expand Down
1 change: 0 additions & 1 deletion crates/polars-python/src/expr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(deprecated)]
#[cfg(feature = "pymethods")]
mod array;
#[cfg(feature = "pymethods")]
Expand Down
18 changes: 7 additions & 11 deletions crates/polars-python/src/expr/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,22 @@ use crate::PyExpr;

#[pymethods]
impl PyExpr {
fn __getstate__(&self, py: Python) -> PyResult<PyObject> {
fn __getstate__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyBytes>> {
// Used in pickle/pickling
let mut writer: Vec<u8> = vec![];
ciborium::ser::into_writer(&self.inner, &mut writer)
.map_err(|e| PyPolarsErr::Other(format!("{}", e)))?;

Ok(PyBytes::new(py, &writer).to_object(py))
Ok(PyBytes::new(py, &writer))
}

fn __setstate__(&mut self, state: &Bound<PyAny>) -> PyResult<()> {
// Used in pickle/pickling
match state.extract::<PyBackedBytes>() {
Ok(s) => {
let cursor = Cursor::new(&*s);
self.inner = ciborium::de::from_reader(cursor)
.map_err(|e| PyPolarsErr::Other(format!("{}", e)))?;
Ok(())
},
Err(e) => Err(e),
}
let bytes = state.extract::<PyBackedBytes>()?;
let cursor = Cursor::new(&*bytes);
self.inner =
ciborium::de::from_reader(cursor).map_err(|e| PyPolarsErr::Other(format!("{}", e)))?;
Ok(())
}

/// Serialize into binary data.
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-python/src/functions/eager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub fn concat_df(dfs: &Bound<'_, PyAny>, py: Python) -> PyResult<PyDataFrame> {
use polars_core::error::PolarsResult;
use polars_core::utils::rayon::prelude::*;

let mut iter = dfs.iter()?;
let mut iter = dfs.try_iter()?;
let first = iter.next().unwrap()?;

let first_rdf = get_df(&first)?;
Expand Down Expand Up @@ -49,7 +49,7 @@ pub fn concat_df(dfs: &Bound<'_, PyAny>, py: Python) -> PyResult<PyDataFrame> {

#[pyfunction]
pub fn concat_series(series: &Bound<'_, PyAny>) -> PyResult<PySeries> {
let mut iter = series.iter()?;
let mut iter = series.try_iter()?;
let first = iter.next().unwrap()?;

let mut s = get_series(&first)?;
Expand All @@ -64,7 +64,7 @@ pub fn concat_series(series: &Bound<'_, PyAny>) -> PyResult<PySeries> {

#[pyfunction]
pub fn concat_df_diagonal(dfs: &Bound<'_, PyAny>) -> PyResult<PyDataFrame> {
let iter = dfs.iter()?;
let iter = dfs.try_iter()?;

let dfs = iter
.map(|item| {
Expand All @@ -79,7 +79,7 @@ pub fn concat_df_diagonal(dfs: &Bound<'_, PyAny>) -> PyResult<PyDataFrame> {

#[pyfunction]
pub fn concat_df_horizontal(dfs: &Bound<'_, PyAny>) -> PyResult<PyDataFrame> {
let iter = dfs.iter()?;
let iter = dfs.try_iter()?;

let dfs = iter
.map(|item| {
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-python/src/functions/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::prelude::ArrowDataType;

#[cfg(feature = "ipc")]
#[pyfunction]
pub fn read_ipc_schema(py: Python, py_f: PyObject) -> PyResult<PyObject> {
pub fn read_ipc_schema(py: Python, py_f: PyObject) -> PyResult<Bound<PyDict>> {
use polars_core::export::arrow::io::ipc::read::read_file_metadata;
let metadata = match get_either_file(py_f, false)? {
EitherRustPythonFile::Rust(r) => {
Expand All @@ -26,12 +26,12 @@ pub fn read_ipc_schema(py: Python, py_f: PyObject) -> PyResult<PyObject> {

let dict = PyDict::new(py);
fields_to_pydict(&metadata.schema, &dict)?;
Ok(dict.to_object(py))
Ok(dict)
}

#[cfg(feature = "parquet")]
#[pyfunction]
pub fn read_parquet_schema(py: Python, py_f: PyObject) -> PyResult<PyObject> {
pub fn read_parquet_schema(py: Python, py_f: PyObject) -> PyResult<Bound<PyDict>> {
use polars_parquet::read::{infer_schema, read_metadata};

let metadata = match get_either_file(py_f, false)? {
Expand All @@ -44,7 +44,7 @@ pub fn read_parquet_schema(py: Python, py_f: PyObject) -> PyResult<PyObject> {

let dict = PyDict::new(py);
fields_to_pydict(&arrow_schema, &dict)?;
Ok(dict.to_object(py))
Ok(dict)
}

#[cfg(any(feature = "ipc", feature = "parquet"))]
Expand Down
20 changes: 9 additions & 11 deletions crates/polars-python/src/functions/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::conversion::{get_lf, Wrap};
use crate::error::PyPolarsErr;
use crate::expr::ToExprs;
use crate::map::lazy::binary_lambda;
use crate::prelude::{vec_extract_wrapped, ObjectValue};
use crate::prelude::vec_extract_wrapped;
use crate::{map, PyDataFrame, PyExpr, PyLazyFrame, PySeries};

macro_rules! set_unwrapped_or_0 {
Expand Down Expand Up @@ -151,7 +151,7 @@ pub fn collect_all_with_callback(lfs: Vec<PyLazyFrame>, lambda: PyObject) {
},
Err(err) => {
lambda
.call1(py, (PyErr::from(err).to_object(py),))
.call1(py, (PyErr::from(err),))
.map_err(|err| err.restore(py))
.ok();
},
Expand All @@ -174,7 +174,7 @@ pub fn concat_lf(
let len = seq.len()?;
let mut lfs = Vec::with_capacity(len);

for res in seq.iter()? {
for res in seq.try_iter()? {
let item = res?;
let lf = get_lf(&item)?;
lfs.push(lf);
Expand Down Expand Up @@ -302,7 +302,7 @@ pub fn concat_lf_diagonal(
parallel: bool,
to_supertypes: bool,
) -> PyResult<PyLazyFrame> {
let iter = lfs.iter()?;
let iter = lfs.try_iter()?;

let lfs = iter
.map(|item| {
Expand All @@ -326,7 +326,7 @@ pub fn concat_lf_diagonal(

#[pyfunction]
pub fn concat_lf_horizontal(lfs: &Bound<'_, PyAny>, parallel: bool) -> PyResult<PyLazyFrame> {
let iter = lfs.iter()?;
let iter = lfs.try_iter()?;

let lfs = iter
.map(|item| {
Expand Down Expand Up @@ -437,8 +437,9 @@ pub fn nth(n: i64) -> PyExpr {

#[pyfunction]
pub fn lit(value: &Bound<'_, PyAny>, allow_object: bool, is_scalar: bool) -> PyResult<PyExpr> {
let py = value.py();
if value.is_instance_of::<PyBool>() {
let val = value.extract::<bool>().unwrap();
let val = value.extract::<bool>()?;
Ok(dsl::lit(val).into())
} else if let Ok(int) = value.downcast::<PyInt>() {
let v = int
Expand All @@ -447,7 +448,7 @@ pub fn lit(value: &Bound<'_, PyAny>, allow_object: bool, is_scalar: bool) -> PyR
.map_err(PyPolarsErr::from)?;
Ok(Expr::Literal(LiteralValue::Int(v)).into())
} else if let Ok(float) = value.downcast::<PyFloat>() {
let val = float.extract::<f64>().unwrap();
let val = float.extract::<f64>()?;
Ok(Expr::Literal(LiteralValue::Float(val)).into())
} else if let Ok(pystr) = value.downcast::<PyString>() {
Ok(dsl::lit(pystr.to_string()).into())
Expand Down Expand Up @@ -479,10 +480,7 @@ pub fn lit(value: &Bound<'_, PyAny>, allow_object: bool, is_scalar: bool) -> PyR
match av {
#[cfg(feature = "object")]
AnyValue::ObjectOwned(_) => {
let s = Python::with_gil(|py| {
PySeries::new_object(py, "", vec![ObjectValue::from(value.into_py(py))], false)
.series
});
let s = PySeries::new_object(py, "", vec![value.extract()?], false).series;
Ok(dsl::lit(s).into())
},
_ => Ok(Expr::Literal(LiteralValue::from(av)).into()),
Expand Down
1 change: 0 additions & 1 deletion crates/polars-python/src/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(deprecated)]
mod aggregation;
mod business;
mod eager;
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/interop/numpy/to_numpy_df.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ where
let first_slice = ca.data_views().next().unwrap();

let start_ptr = first_slice.as_ptr();
let np_dtype = T::Native::get_dtype_bound(py);
let np_dtype = T::Native::get_dtype(py);
let dims = [first_slice.len(), df.width()].into_dimension();

unsafe {
Expand Down
6 changes: 4 additions & 2 deletions crates/polars-python/src/interop/numpy/to_numpy_series.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn series_to_numpy_view_recursive(py: Python, s: Series, writable: bool) -> PyOb
fn numeric_series_to_numpy_view(py: Python, s: Series, writable: bool) -> PyObject {
let dims = [s.len()].into_dimension();
with_match_physical_numeric_polars_type!(s.dtype(), |$T| {
let np_dtype = <$T as PolarsNumericType>::Native::get_dtype_bound(py);
let np_dtype = <$T as PolarsNumericType>::Native::get_dtype(py);
let ca: &ChunkedArray<$T> = s.unpack::<$T>().unwrap();
let flags = if writable {
flags::NPY_ARRAY_FARRAY
Expand Down Expand Up @@ -248,7 +248,9 @@ fn series_to_numpy_with_copy(py: Python, s: &Series, writable: bool) -> PyObject
},
Decimal(_, _) => {
let ca = s.decimal().unwrap();
let values = decimal_to_pyobject_iter(py, ca).map(|v| v.into_py(py));
let values = decimal_to_pyobject_iter(py, ca)
.unwrap()
.map(|v| v.into_py(py));
PyArray1::from_iter_bound(py, values).into_py(py)
},
List(_) => list_series_to_numpy(py, s, writable),
Expand Down
Loading

0 comments on commit 3dcf243

Please sign in to comment.