Skip to content

Commit

Permalink
Fix bunch of deprecation warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers committed Dec 2, 2024
1 parent 7843535 commit c4b62c3
Show file tree
Hide file tree
Showing 32 changed files with 150 additions and 143 deletions.
12 changes: 6 additions & 6 deletions crates/polars-python/src/cloud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use crate::lazyframe::visit::NodeTraverser;
use crate::{PyDataFrame, PyLazyFrame};

#[pyfunction]
pub fn prepare_cloud_plan(lf: PyLazyFrame, py: Python) -> PyResult<PyObject> {
pub fn prepare_cloud_plan(lf: PyLazyFrame, py: Python<'_>) -> PyResult<Bound<'_, PyBytes>> {
let plan = lf.ldf.logical_plan;
let bytes = polars::prelude::prepare_cloud_plan(plan).map_err(PyPolarsErr::from)?;

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

/// Take a serialized `IRPlan` and execute it on the GPU engine.
Expand Down Expand Up @@ -62,13 +62,13 @@ fn gpu_post_opt(
expr_arena: &mut Arena<AExpr>,
) -> PolarsResult<()> {
// Get cuDF Python function.
let cudf = PyModule::import_bound(py, intern!(py, "cudf_polars")).unwrap();
let cudf = PyModule::import(py, intern!(py, "cudf_polars")).unwrap();
let lambda = cudf.getattr(intern!(py, "execute_with_cudf")).unwrap();

// Define cuDF config.
let polars = PyModule::import_bound(py, intern!(py, "polars")).unwrap();
let polars = PyModule::import(py, intern!(py, "polars")).unwrap();
let engine = polars.getattr(intern!(py, "GPUEngine")).unwrap();
let kwargs = [("raise_on_fail", true)].into_py_dict_bound(py);
let kwargs = [("raise_on_fail", true)].into_py_dict(py).unwrap();
let engine = engine.call((), Some(&kwargs)).unwrap();

// Define node traverser.
Expand All @@ -79,7 +79,7 @@ fn gpu_post_opt(

// Pass the node visitor which allows the Python callback to replace parts of the query plan.
// Remove "cuda" or specify better once we have multiple post-opt callbacks.
let kwargs = [("config", engine)].into_py_dict_bound(py);
let kwargs = [("config", engine)].into_py_dict(py).unwrap();
lambda
.call((nt,), Some(&kwargs))
.map_err(|e| polars_err!(ComputeError: "'cuda' conversion failed: {}", e))?;
Expand Down
6 changes: 3 additions & 3 deletions crates/polars-python/src/conversion/any_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ pub(crate) fn any_value_into_py_object(av: AnyValue, py: Python) -> PyObject {
let object = v.0.as_any().downcast_ref::<ObjectValue>().unwrap();
object.inner.clone_ref(py)
},
AnyValue::Binary(v) => PyBytes::new_bound(py, v).into_py(py),
AnyValue::BinaryOwned(v) => PyBytes::new_bound(py, &v).into_py(py),
AnyValue::Binary(v) => PyBytes::new(py, v).into_py(py),
AnyValue::BinaryOwned(v) => PyBytes::new(py, &v).into_py(py),
AnyValue::Decimal(v, scale) => {
let convert = utils.getattr(intern!(py, "to_py_decimal")).unwrap();
const N: usize = 3;
Expand Down Expand Up @@ -351,7 +351,7 @@ pub(crate) fn py_object_to_any_value<'py>(
// This constructor is able to go via dedicated type constructors
// so it can be much faster.
let py = ob.py();
let kwargs = PyDict::new_bound(py);
let kwargs = PyDict::new(py);
kwargs.set_item("strict", strict)?;
let s = SERIES.call_bound(py, (ob,), Some(&kwargs))?;
get_list_from_series(s.bind(py), strict)
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/conversion/chunked_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl ToPyObject for Wrap<&BinaryChunked> {
let iter = self
.0
.iter()
.map(|opt_bytes| opt_bytes.map(|bytes| PyBytes::new_bound(py, bytes)));
.map(|opt_bytes| opt_bytes.map(|bytes| PyBytes::new(py, bytes)));
PyList::new_bound(py, iter).into_py(py)
}
}
Expand Down
6 changes: 4 additions & 2 deletions crates/polars-python/src/conversion/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

pub(crate) mod any_value;
pub(crate) mod chunked_array;
mod datetime;
Expand Down Expand Up @@ -149,7 +151,7 @@ fn struct_dict<'a>(
vals: impl Iterator<Item = AnyValue<'a>>,
flds: &[Field],
) -> PyObject {
let dict = PyDict::new_bound(py);
let dict = PyDict::new(py);
for (fld, val) in flds.iter().zip(vals) {
dict.set_item(fld.name().as_str(), Wrap(val).into_py(py))
.unwrap()
Expand Down Expand Up @@ -597,7 +599,7 @@ impl<'py> FromPyObject<'py> for Wrap<ScanSources> {

impl IntoPy<PyObject> for Wrap<&Schema> {
fn into_py(self, py: Python<'_>) -> PyObject {
let dict = PyDict::new_bound(py);
let dict = PyDict::new(py);
for (k, v) in self.0.iter() {
dict.set_item(k.as_str(), Wrap(v.clone()).to_object(py))
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-python/src/dataframe/construction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn dicts_to_rows<'a>(
) -> PyResult<Vec<Row<'a>>> {
let len = data.len()?;
let mut rows = Vec::with_capacity(len);
for d in data.iter()? {
for d in data.try_iter()? {
let d = d?;
let d = d.downcast::<PyDict>()?;

Expand Down Expand Up @@ -189,7 +189,7 @@ fn infer_schema_names_from_data(
.unwrap_or(data_len);

let mut names = PlIndexSet::new();
for d in data.iter()?.take(infer_schema_length) {
for d in data.try_iter()?.take(infer_schema_length) {
let d = d?;
let d = d.downcast::<PyDict>()?;
let keys = d.keys();
Expand Down
97 changes: 45 additions & 52 deletions crates/polars-python/src/dataframe/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::prelude::PyCompatLevel;
#[pymethods]
impl PyDataFrame {
#[cfg(feature = "object")]
pub fn row_tuple(&self, idx: i64) -> PyResult<PyObject> {
pub fn row_tuple<'py>(&self, idx: i64, py: Python<'py>) -> PyResult<Bound<'py, PyTuple>> {
let idx = if idx < 0 {
(self.df.height() as i64 + idx) as usize
} else {
Expand All @@ -24,64 +24,57 @@ impl PyDataFrame {
if idx >= self.df.height() {
return Err(PyPolarsErr::from(polars_err!(oob = idx, self.df.height())).into());
}
let out = Python::with_gil(|py| {
PyTuple::new_bound(
py,
self.df.get_columns().iter().map(|s| match s.dtype() {
DataType::Object(_, _) => {
let obj: Option<&ObjectValue> = s.get_object(idx).map(|any| any.into());
obj.to_object(py)
},
_ => Wrap(s.get(idx).unwrap()).into_py(py),
}),
)
.into_py(py)
});
Ok(out)
PyTuple::new(
py,
self.df.get_columns().iter().map(|s| match s.dtype() {
DataType::Object(_, _) => {
let obj: Option<&ObjectValue> = s.get_object(idx).map(|any| any.into());
obj.to_object(py)
},
_ => Wrap(s.get(idx).unwrap()).into_py(py),
}),
)
}

#[cfg(feature = "object")]
pub fn row_tuples(&self) -> PyObject {
Python::with_gil(|py| {
let mut rechunked;
// Rechunk if random access would become rather expensive.
// TODO: iterate over the chunks directly instead of using random access.
let df = if self.df.max_n_chunks() > 16 {
rechunked = self.df.clone();
rechunked.as_single_chunk_par();
&rechunked
} else {
&self.df
};
PyList::new_bound(
py,
(0..df.height()).map(|idx| {
PyTuple::new_bound(
py,
df.get_columns().iter().map(|c| match c.dtype() {
DataType::Null => py.None(),
DataType::Object(_, _) => {
let obj: Option<&ObjectValue> =
c.get_object(idx).map(|any| any.into());
obj.to_object(py)
},
_ => {
// SAFETY: we are in bounds.
let av = unsafe { c.get_unchecked(idx) };
Wrap(av).into_py(py)
},
}),
)
}),
)
.into_py(py)
})
pub fn row_tuples<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyList>> {
let mut rechunked;
// Rechunk if random access would become rather expensive.
// TODO: iterate over the chunks directly instead of using random access.
let df = if self.df.max_n_chunks() > 16 {
rechunked = self.df.clone();
rechunked.as_single_chunk_par();
&rechunked
} else {
&self.df
};
PyList::new(
py,
(0..df.height()).map(|idx| {
PyTuple::new(
py,
df.get_columns().iter().map(|c| match c.dtype() {
DataType::Null => py.None(),
DataType::Object(_, _) => {
let obj: Option<&ObjectValue> = c.get_object(idx).map(|any| any.into());
obj.to_object(py)
},
_ => {
// SAFETY: we are in bounds.
let av = unsafe { c.get_unchecked(idx) };
Wrap(av).into_py(py)
},
}),
)
.unwrap()
}),
)
}

#[allow(clippy::wrong_self_convention)]
pub fn to_arrow(&mut self, py: Python, compat_level: PyCompatLevel) -> PyResult<Vec<PyObject>> {
py.allow_threads(|| self.df.align_chunks_par());
let pyarrow = py.import_bound("pyarrow")?;
let pyarrow = py.import("pyarrow")?;
let names = self.df.get_column_names_str();

let rbs = self
Expand All @@ -101,7 +94,7 @@ impl PyDataFrame {
pub fn to_pandas(&mut self, py: Python) -> PyResult<Vec<PyObject>> {
py.allow_threads(|| self.df.as_single_chunk_par());
Python::with_gil(|py| {
let pyarrow = py.import_bound("pyarrow")?;
let pyarrow = py.import("pyarrow")?;
let names = self.df.get_column_names_str();
let cat_columns = self
.df
Expand Down
8 changes: 4 additions & 4 deletions crates/polars-python/src/dataframe/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ impl PyDataFrame {
}

/// Get datatypes
pub fn dtypes(&self, py: Python) -> PyObject {
pub fn dtypes<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyList>> {
let iter = self
.df
.iter()
.map(|s| Wrap(s.dtype().clone()).to_object(py));
PyList::new_bound(py, iter).to_object(py)
PyList::new(py, iter)
}

pub fn n_chunks(&self) -> usize {
Expand Down Expand Up @@ -402,15 +402,15 @@ impl PyDataFrame {

let function = move |df: DataFrame| {
Python::with_gil(|py| {
let pypolars = PyModule::import_bound(py, "polars").unwrap();
let pypolars = PyModule::import(py, "polars").unwrap();
let pydf = PyDataFrame::new(df);
let python_df_wrapper =
pypolars.getattr("wrap_df").unwrap().call1((pydf,)).unwrap();

// Call the lambda and get a python-side DataFrame wrapper.
let result_df_wrapper = match lambda.call1(py, (python_df_wrapper,)) {
Ok(pyobj) => pyobj,
Err(e) => panic!("UDF failed: {}", e.value_bound(py)),
Err(e) => panic!("UDF failed: {}", e.value(py)),
};
let py_pydf = result_df_wrapper.getattr(py, "_df").expect(
"Could not get DataFrame attribute '_df'. Make sure that you return a DataFrame object.",
Expand Down
2 changes: 2 additions & 0 deletions crates/polars-python/src/dataframe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

#[cfg(feature = "pymethods")]
mod construction;
#[cfg(feature = "pymethods")]
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-python/src/dataframe/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use crate::file::{get_file_like, get_mmap_bytes_reader};
#[pymethods]
impl PyDataFrame {
#[cfg(feature = "ipc_streaming")]
fn __getstate__(&self, py: Python) -> PyResult<PyObject> {
fn __getstate__<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
// Used in pickle/pickling
let mut buf: Vec<u8> = vec![];
IpcStreamWriter::new(&mut buf)
.with_compat_level(CompatLevel::newest())
.finish(&mut self.df.clone())
.expect("ipc writer");
Ok(PyBytes::new_bound(py, &buf).to_object(py))
PyBytes::new(py, &buf)
}

#[cfg(feature = "ipc_streaming")]
Expand Down
14 changes: 9 additions & 5 deletions crates/polars-python/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,20 @@ macro_rules! raise_err(
}}
);

impl IntoPy<PyObject> for Wrap<PolarsWarning> {
fn into_py(self, py: Python<'_>) -> PyObject {
impl<'py> IntoPyObject<'py> for Wrap<PolarsWarning> {
type Target = PyAny;
type Output = Bound<'py, Self::Target>;
type Error = std::convert::Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match self.0 {
PolarsWarning::CategoricalRemappingWarning => {
CategoricalRemappingWarning::type_object_bound(py).to_object(py)
Ok(CategoricalRemappingWarning::type_object(py).into_any())
},
PolarsWarning::MapWithoutReturnDtypeWarning => {
MapWithoutReturnDtypeWarning::type_object_bound(py).to_object(py)
Ok(MapWithoutReturnDtypeWarning::type_object(py).into_any())
},
PolarsWarning::UserWarning => PyUserWarning::type_object_bound(py).to_object(py),
PolarsWarning::UserWarning => Ok(PyUserWarning::type_object(py).into_any()),
}
}
}
1 change: 1 addition & 0 deletions crates/polars-python/src/expr/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(deprecated)]
#[cfg(feature = "pymethods")]
mod array;
#[cfg(feature = "pymethods")]
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/expr/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl PyExpr {
ciborium::ser::into_writer(&self.inner, &mut writer)
.map_err(|e| PyPolarsErr::Other(format!("{}", e)))?;

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

fn __setstate__(&mut self, state: &Bound<PyAny>) -> PyResult<()> {
Expand Down
Loading

0 comments on commit c4b62c3

Please sign in to comment.