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 4144c9f
Show file tree
Hide file tree
Showing 31 changed files with 101 additions and 87 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/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ impl PyDataFrame {
#[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 +101,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
2 changes: 1 addition & 1 deletion crates/polars-python/src/dataframe/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ 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();
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
2 changes: 1 addition & 1 deletion crates/polars-python/src/dataframe/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl PyDataFrame {
.with_compat_level(CompatLevel::newest())
.finish(&mut self.df.clone())
.expect("ipc writer");
Ok(PyBytes::new_bound(py, &buf).to_object(py))
Ok(PyBytes::new(py, &buf).to_object(py))
}

#[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
27 changes: 14 additions & 13 deletions crates/polars-python/src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use polars_error::polars_err;
use pyo3::exceptions::PyTypeError;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyString, PyStringMethods};
use pyo3::IntoPyObjectExt;

use crate::error::PyPolarsErr;
use crate::prelude::resolve_homedir;
Expand Down Expand Up @@ -51,7 +52,7 @@ impl PyFileLikeObject {
let buf = Python::with_gil(|py| {
let bytes = self
.inner
.call_method_bound(py, "read", (), None)
.call_method(py, "read", (), None)
.expect("no read method found");

if let Ok(bytes) = bytes.downcast_bound::<PyBytes>(py) {
Expand Down Expand Up @@ -106,9 +107,9 @@ impl PyFileLikeObject {
/// Extracts a string repr from, and returns an IO error to send back to rust.
fn pyerr_to_io_err(e: PyErr) -> io::Error {
Python::with_gil(|py| {
let e_as_object: PyObject = e.into_py(py);
let e_as_object: PyObject = e.into_py_any(py).unwrap();

match e_as_object.call_method_bound(py, "__str__", (), None) {
match e_as_object.call_method(py, "__str__", (), None) {
Ok(repr) => match repr.extract::<String>(py) {
Ok(s) => io::Error::new(io::ErrorKind::Other, s),
Err(_e) => io::Error::new(io::ErrorKind::Other, "An unknown error has occurred"),
Expand All @@ -123,7 +124,7 @@ impl Read for PyFileLikeObject {
Python::with_gil(|py| {
let bytes = self
.inner
.call_method_bound(py, "read", (buf.len(),), None)
.call_method(py, "read", (buf.len(),), None)
.map_err(pyerr_to_io_err)?;

let opt_bytes = bytes.downcast_bound::<PyBytes>(py);
Expand All @@ -149,11 +150,11 @@ impl Read for PyFileLikeObject {
impl Write for PyFileLikeObject {
fn write(&mut self, buf: &[u8]) -> Result<usize, io::Error> {
Python::with_gil(|py| {
let pybytes = PyBytes::new_bound(py, buf);
let pybytes = PyBytes::new(py, buf);

let number_bytes_written = self
.inner
.call_method_bound(py, "write", (pybytes,), None)
.call_method(py, "write", (pybytes,), None)
.map_err(pyerr_to_io_err)?;

number_bytes_written.extract(py).map_err(pyerr_to_io_err)
Expand All @@ -163,7 +164,7 @@ impl Write for PyFileLikeObject {
fn flush(&mut self) -> Result<(), io::Error> {
Python::with_gil(|py| {
self.inner
.call_method_bound(py, "flush", (), None)
.call_method(py, "flush", (), None)
.map_err(pyerr_to_io_err)?;

Ok(())
Expand All @@ -182,7 +183,7 @@ impl Seek for PyFileLikeObject {

let new_position = self
.inner
.call_method_bound(py, "seek", (offset, whence), None)
.call_method(py, "seek", (offset, whence), None)
.map_err(pyerr_to_io_err)?;

new_position.extract(py).map_err(pyerr_to_io_err)
Expand Down Expand Up @@ -235,7 +236,7 @@ pub fn get_python_scan_source_input(
let file_path = resolve_homedir(file_path);
Ok(PythonScanSourceInput::Path(file_path))
} else {
let io = py.import_bound("io").unwrap();
let io = py.import("io").unwrap();
let is_utf8_encoding = |py_f: &Bound<PyAny>| -> PyResult<bool> {
let encoding = py_f.getattr("encoding")?;
let encoding = encoding.extract::<Cow<str>>()?;
Expand Down Expand Up @@ -316,7 +317,7 @@ pub fn get_python_scan_source_input(
};
PyFileLikeObject::ensure_requirements(&py_f, !write, write, !write)?;
Ok(PythonScanSourceInput::Buffer(
PyFileLikeObject::new(py_f.to_object(py)).as_bytes(),
PyFileLikeObject::new(py_f.unbind()).as_bytes(),
))
}
})
Expand All @@ -338,7 +339,7 @@ fn get_either_buffer_or_path(
};
Ok((EitherRustPythonFile::Rust(f), Some(file_path)))
} else {
let io = py.import_bound("io").unwrap();
let io = py.import("io").unwrap();
let is_utf8_encoding = |py_f: &Bound<PyAny>| -> PyResult<bool> {
let encoding = py_f.getattr("encoding")?;
let encoding = encoding.extract::<Cow<str>>()?;
Expand Down Expand Up @@ -420,7 +421,7 @@ fn get_either_buffer_or_path(
py_f
};
PyFileLikeObject::ensure_requirements(&py_f, !write, write, !write)?;
let f = PyFileLikeObject::new(py_f.to_object(py));
let f = PyFileLikeObject::new(py_f.unbind());
Ok((EitherRustPythonFile::Py(f), None))
}
})
Expand Down Expand Up @@ -467,7 +468,7 @@ pub fn get_mmap_bytes_reader_and_path<'a>(
}
// string so read file
else {
match get_either_buffer_or_path(py_f.to_object(py_f.py()), false)? {
match get_either_buffer_or_path(py_f.to_owned().unbind(), false)? {
(EitherRustPythonFile::Rust(f), path) => Ok((Box::new(f), path)),
(EitherRustPythonFile::Py(f), path) => Ok((Box::new(f), path)),
}
Expand Down
4 changes: 2 additions & 2 deletions crates/polars-python/src/functions/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub fn read_ipc_schema(py: Python, py_f: PyObject) -> PyResult<PyObject> {
EitherRustPythonFile::Py(mut r) => read_file_metadata(&mut r).map_err(PyPolarsErr::from)?,
};

let dict = PyDict::new_bound(py);
let dict = PyDict::new(py);
fields_to_pydict(&metadata.schema, &dict, py)?;
Ok(dict.to_object(py))
}
Expand All @@ -42,7 +42,7 @@ pub fn read_parquet_schema(py: Python, py_f: PyObject) -> PyResult<PyObject> {
};
let arrow_schema = infer_schema(&metadata).map_err(PyPolarsErr::from)?;

let dict = PyDict::new_bound(py);
let dict = PyDict::new(py);
fields_to_pydict(&arrow_schema, &dict, py)?;
Ok(dict.to_object(py))
}
Expand Down
1 change: 1 addition & 0 deletions crates/polars-python/src/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(deprecated)]
mod aggregation;
mod business;
mod eager;
Expand Down
1 change: 1 addition & 0 deletions crates/polars-python/src/interop/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#![allow(deprecated)]
pub mod arrow;
pub mod numpy;
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 @@ -278,7 +278,7 @@ fn df_columns_to_numpy(
arr
});

let numpy = PyModule::import_bound(py, intern!(py, "numpy"))?;
let numpy = PyModule::import(py, intern!(py, "numpy"))?;
let np_array = match order {
IndexOrder::C => numpy
.getattr(intern!(py, "column_stack"))
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/lazyframe/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ impl PyLazyFrame {
.allow_threads(|| self.ldf.collect_schema())
.map_err(PyPolarsErr::from)?;

let schema_dict = PyDict::new_bound(py);
let schema_dict = PyDict::new(py);
schema.iter_fields().for_each(|fld| {
schema_dict
.set_item(fld.name().as_str(), Wrap(fld.dtype().clone()).to_object(py))
Expand Down
2 changes: 2 additions & 0 deletions crates/polars-python/src/lazyframe/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![allow(deprecated)]

mod exitable;
#[cfg(feature = "pymethods")]
mod general;
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/lazyframe/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl PyLazyFrame {
ciborium::ser::into_writer(&self.ldf.logical_plan, &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, py: Python, state: PyObject) -> PyResult<()> {
Expand Down
2 changes: 1 addition & 1 deletion crates/polars-python/src/lazygroupby.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl PyLazyGroupBy {
let function = move |df: DataFrame| {
Python::with_gil(|py| {
// get the pypolars module
let pypolars = PyModule::import_bound(py, "polars").unwrap();
let pypolars = PyModule::import(py, "polars").unwrap();

// create a PyDataFrame struct/object for Python
let pydf = PyDataFrame::new(df);
Expand Down
1 change: 0 additions & 1 deletion crates/polars-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![allow(non_local_definitions)]
#![allow(clippy::too_many_arguments)] // Python functions can have many arguments due to default arguments
#![allow(clippy::disallowed_types)]
#![allow(deprecated)]

#[cfg(feature = "csv")]
pub mod batched_csv;
Expand Down
Loading

0 comments on commit 4144c9f

Please sign in to comment.