Skip to content

Commit

Permalink
chore: clippy pyo3 deprecations
Browse files Browse the repository at this point in the history
Signed-off-by: Ion Koutsouris <15728914+ion-elgreco@users.noreply.github.com>
  • Loading branch information
ion-elgreco committed Feb 19, 2025
1 parent c3b8223 commit b1776c7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 75 deletions.
18 changes: 5 additions & 13 deletions python/src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,11 @@ impl DeltaFileSystemHandler {
paths: Vec<String>,
py: Python<'py>,
) -> PyResult<Vec<Bound<'py, PyAny>>> {
let fs = PyModule::import_bound(py, "pyarrow.fs")?;
let fs = PyModule::import(py, "pyarrow.fs")?;
let file_types = fs.getattr("FileType")?;

let to_file_info = |loc: &str, type_: &Bound<'py, PyAny>, kwargs: &HashMap<&str, i64>| {
fs.call_method(
"FileInfo",
(loc, type_),
Some(&kwargs.into_py_dict_bound(py)),
)
fs.call_method("FileInfo", (loc, type_), Some(&kwargs.into_py_dict(py)?))
};

let mut infos = Vec::new();
Expand Down Expand Up @@ -199,15 +195,11 @@ impl DeltaFileSystemHandler {
recursive: bool,
py: Python<'py>,
) -> PyResult<Vec<Bound<'py, PyAny>>> {
let fs = PyModule::import_bound(py, "pyarrow.fs")?;
let fs = PyModule::import(py, "pyarrow.fs")?;
let file_types = fs.getattr("FileType")?;

let to_file_info = |loc: String, type_: &Bound<'py, PyAny>, kwargs: HashMap<&str, i64>| {
fs.call_method(
"FileInfo",
(loc, type_),
Some(&kwargs.into_py_dict_bound(py)),
)
fs.call_method("FileInfo", (loc, type_), Some(&kwargs.into_py_dict(py)?))
};

let path = Self::parse_path(&base_dir);
Expand Down Expand Up @@ -493,7 +485,7 @@ impl ObjectInputFile {
// TODO: PyBytes copies the buffer. If we move away from the limited CPython
// API (the stable C API), we could implement the buffer protocol for
// bytes::Bytes and return this zero-copy.
Ok(PyBytes::new_bound(py, data.as_ref()))
Ok(PyBytes::new(py, data.as_ref()))
}

fn fileno(&self) -> PyResult<()> {
Expand Down
76 changes: 32 additions & 44 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ use crate::schema::{schema_to_pyobject, Field};
use crate::utils::rt;
use deltalake::operations::update_field_metadata::UpdateFieldMetadataBuilder;
use pyo3::exceptions::{PyRuntimeError, PyValueError};
use pyo3::prelude::*;
use pyo3::pybacked::PyBackedStr;
use pyo3::types::{PyCapsule, PyDict, PyFrozenSet};
use pyo3::{prelude::*, IntoPyObjectExt};
use serde_json::{Map, Value};
use uuid::Uuid;

Expand Down Expand Up @@ -1210,9 +1210,9 @@ impl RawDeltaTable {

let active_partitions = active_partitions
.into_iter()
.map(|part| PyFrozenSet::new_bound(py, part.iter()))
.map(|part| PyFrozenSet::new(py, part.iter()))
.collect::<Result<Vec<Bound<'py, _>>, PyErr>>()?;
PyFrozenSet::new_bound(py, &active_partitions)
PyFrozenSet::new(py, &active_partitions)
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -1615,7 +1615,7 @@ impl RawDeltaTable {
let table = self.with_table(|t| Ok(Arc::new(t.clone())))?;
let provider = FFI_TableProvider::new(table, false, handle);

PyCapsule::new_bound(py, provider, Some(name.clone()))
PyCapsule::new(py, provider, Some(name.clone()))
}
}

Expand Down Expand Up @@ -1797,38 +1797,38 @@ fn scalar_to_py<'py>(value: &Scalar, py_date: &Bound<'py, PyAny>) -> PyResult<Bo
let py = py_date.py();
let val = match value {
Null(_) => py.None(),
Boolean(val) => val.to_object(py),
Binary(val) => val.to_object(py),
String(val) => val.to_object(py),
Byte(val) => val.to_object(py),
Short(val) => val.to_object(py),
Integer(val) => val.to_object(py),
Long(val) => val.to_object(py),
Float(val) => val.to_object(py),
Double(val) => val.to_object(py),
Boolean(val) => val.into_py_any(py)?,
Binary(val) => val.into_py_any(py)?,
String(val) => val.into_py_any(py)?,
Byte(val) => val.into_py_any(py)?,
Short(val) => val.into_py_any(py)?,
Integer(val) => val.into_py_any(py)?,
Long(val) => val.into_py_any(py)?,
Float(val) => val.into_py_any(py)?,
Double(val) => val.into_py_any(py)?,
Timestamp(_) => {
// We need to manually append 'Z' add to end so that pyarrow can cast the
// scalar value to pa.timestamp("us","UTC")
let value = value.serialize();
format!("{}Z", value).to_object(py)
format!("{}Z", value).into_py_any(py)?
}
TimestampNtz(_) => {
let value = value.serialize();
value.to_object(py)
value.into_py_any(py)?
}
// NOTE: PyArrow 13.0.0 lost the ability to cast from string to date32, so
// we have to implement that manually.
Date(_) => {
let date = py_date.call_method1("fromisoformat", (value.serialize(),))?;
date.to_object(py)
date.into_py_any(py)?
}
Decimal(_, _, _) => value.serialize().to_object(py),
Decimal(_, _, _) => value.serialize().into_py_any(py)?,
Struct(data) => {
let py_struct = PyDict::new_bound(py);
let py_struct = PyDict::new(py);
for (field, value) in data.fields().iter().zip(data.values().iter()) {
py_struct.set_item(field.name(), scalar_to_py(value, py_date)?)?;
}
py_struct.to_object(py)
py_struct.into_py_any(py)?
}
Array(_val) => todo!("how should this be converted!"),
};
Expand All @@ -1851,7 +1851,7 @@ fn scalar_to_py<'py>(value: &Scalar, py_date: &Bound<'py, PyAny>) -> PyResult<Bo
fn filestats_to_expression_next<'py>(
py: Python<'py>,
schema: &PyArrowType<ArrowSchema>,
stats_columns: &Vec<String>,
stats_columns: &[String],
file_info: LogicalFile<'_>,
) -> PyResult<Option<Bound<'py, PyAny>>> {
let ds = PyModule::import(py, "pyarrow.dataset")?;
Expand Down Expand Up @@ -2462,33 +2462,21 @@ fn _internal(m: &Bound<'_, PyModule>) -> PyResult<()> {
deltalake::unity_catalog::register_handlers(None);

let py = m.py();
m.add("DeltaError", py.get_type_bound::<DeltaError>())?;
m.add(
"CommitFailedError",
py.get_type_bound::<CommitFailedError>(),
)?;
m.add(
"DeltaProtocolError",
py.get_type_bound::<DeltaProtocolError>(),
)?;
m.add(
"TableNotFoundError",
py.get_type_bound::<TableNotFoundError>(),
)?;
m.add(
"SchemaMismatchError",
py.get_type_bound::<SchemaMismatchError>(),
)?;
m.add("DeltaError", py.get_type::<DeltaError>())?;
m.add("CommitFailedError", py.get_type::<CommitFailedError>())?;
m.add("DeltaProtocolError", py.get_type::<DeltaProtocolError>())?;
m.add("TableNotFoundError", py.get_type::<TableNotFoundError>())?;
m.add("SchemaMismatchError", py.get_type::<SchemaMismatchError>())?;

env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn")).init();
m.add("__version__", env!("CARGO_PKG_VERSION"))?;
m.add_function(pyo3::wrap_pyfunction_bound!(rust_core_version, m)?)?;
m.add_function(pyo3::wrap_pyfunction_bound!(create_deltalake, m)?)?;
m.add_function(pyo3::wrap_pyfunction_bound!(write_new_deltalake, m)?)?;
m.add_function(pyo3::wrap_pyfunction_bound!(write_to_deltalake, m)?)?;
m.add_function(pyo3::wrap_pyfunction_bound!(convert_to_deltalake, m)?)?;
m.add_function(pyo3::wrap_pyfunction_bound!(batch_distinct, m)?)?;
m.add_function(pyo3::wrap_pyfunction_bound!(
m.add_function(pyo3::wrap_pyfunction!(rust_core_version, m)?)?;
m.add_function(pyo3::wrap_pyfunction!(create_deltalake, m)?)?;
m.add_function(pyo3::wrap_pyfunction!(write_new_deltalake, m)?)?;
m.add_function(pyo3::wrap_pyfunction!(write_to_deltalake, m)?)?;
m.add_function(pyo3::wrap_pyfunction!(convert_to_deltalake, m)?)?;
m.add_function(pyo3::wrap_pyfunction!(batch_distinct, m)?)?;
m.add_function(pyo3::wrap_pyfunction!(
get_num_idx_cols_and_stats_columns,
m
)?)?;
Expand Down
30 changes: 17 additions & 13 deletions python/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use deltalake::kernel::{
PrimitiveType as DeltaPrimitve, StructField, StructType as DeltaStructType, StructTypeExt,
};
use pyo3::exceptions::{PyException, PyNotImplementedError, PyTypeError, PyValueError};
use pyo3::prelude::*;
use pyo3::{prelude::*, IntoPyObjectExt};
use std::collections::HashMap;

use crate::utils::warn;
Expand All @@ -26,19 +26,19 @@ use crate::utils::warn;
fn schema_type_to_python(schema_type: DataType, py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
match schema_type {
DataType::Primitive(data_type) => Ok((PrimitiveType::new(data_type.to_string())?)
.into_py(py)
.into_py_any(py)?
.into_bound(py)),
DataType::Array(array_type) => {
let array_type: ArrayType = (*array_type).into();
Ok(array_type.into_py(py).into_bound(py))
Ok(array_type.into_py_any(py)?.into_bound(py))
}
DataType::Map(map_type) => {
let map_type: MapType = (*map_type).into();
Ok(map_type.into_py(py).into_bound(py))
Ok(map_type.into_py_any(py)?.into_bound(py))
}
DataType::Struct(struct_type) => {
let struct_type: StructType = (*struct_type).into();
Ok(struct_type.into_py(py).into_bound(py))
Ok(struct_type.into_py_any(py)?.into_bound(py))
}
}
}
Expand Down Expand Up @@ -230,7 +230,11 @@ impl ArrayType {

#[getter]
fn contains_null<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
Ok(self.inner_type.contains_null().into_py(py).into_bound(py))
Ok(self
.inner_type
.contains_null()
.into_py_any(py)?
.into_bound(py))
}

#[pyo3(text_signature = "($self)")]
Expand Down Expand Up @@ -363,7 +367,7 @@ impl MapType {
Ok(self
.inner_type
.value_contains_null()
.into_py(py)
.into_py_any(py)?
.into_bound(py))
}

Expand Down Expand Up @@ -422,7 +426,7 @@ impl Field {

// Serialize and de-serialize JSON (it needs to be valid JSON anyways)
let metadata: HashMap<String, serde_json::Value> = if let Some(json) = metadata {
let json_dumps = PyModule::import_bound(py, "json")?.getattr("dumps")?;
let json_dumps = PyModule::import(py, "json")?.getattr("dumps")?;
let metadata_json: String = json_dumps.call1((json,))?.extract()?;
let metadata_json = Some(metadata_json)
.filter(|x| x != "null")
Expand Down Expand Up @@ -473,12 +477,12 @@ impl Field {

#[getter]
fn metadata<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> {
let json_loads = PyModule::import_bound(py, "json")?.getattr("loads")?;
let json_loads = PyModule::import(py, "json")?.getattr("loads")?;
let metadata_json: String = serde_json::to_string(self.inner.metadata())
.map_err(|err| PyValueError::new_err(err.to_string()))?;
Ok(json_loads
.call1((metadata_json,))?
.to_object(py)
.into_py_any(py)?
.bind(py)
.to_owned())
}
Expand Down Expand Up @@ -669,7 +673,7 @@ pub fn schema_to_pyobject(schema: DeltaStructType, py: Python<'_>) -> PyResult<B
inner: field.clone(),
});

let py_schema = PyModule::import_bound(py, "deltalake.schema")?.getattr("Schema")?;
let py_schema = PyModule::import(py, "deltalake.schema")?.getattr("Schema")?;

py_schema.call1((fields.collect::<Vec<_>>(),))
}
Expand Down Expand Up @@ -727,8 +731,8 @@ impl PySchema {

let super_ = self_.as_ref();
let json = super_.to_json()?;
let json_loads = PyModule::import_bound(py, "json")?.getattr("loads")?;
json_loads.call1((json.into_py(py),))
let json_loads = PyModule::import(py, "json")?.getattr("loads")?;
json_loads.call1((json.into_pyobject(py)?,))
}

#[pyo3(signature = (as_large_types = false))]
Expand Down
10 changes: 5 additions & 5 deletions python/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use deltalake::storage::{ListResult, ObjectStore, ObjectStoreError, ObjectStoreR
use futures::future::{join_all, BoxFuture, FutureExt};
use futures::StreamExt;
use pyo3::types::{IntoPyDict, PyAnyMethods, PyModule};
use pyo3::{Bound, PyAny, PyResult, Python, ToPyObject};
use pyo3::{Bound, IntoPyObjectExt, PyAny, PyResult, Python};
use tokio::runtime::Runtime;

#[inline]
Expand Down Expand Up @@ -101,13 +101,13 @@ pub fn warn<'py>(
message: &str,
stack_level: Option<u8>,
) -> PyResult<()> {
let warnings_warn = PyModule::import_bound(py, "warnings")?.getattr("warn")?;
let warning_type = PyModule::import_bound(py, "builtins")?.getattr(warning_type)?;
let warnings_warn = PyModule::import(py, "warnings")?.getattr("warn")?;
let warning_type = PyModule::import(py, "builtins")?.getattr(warning_type)?;
let stack_level = stack_level.unwrap_or(1);
let kwargs: [(&str, Bound<'py, PyAny>); 2] = [
("category", warning_type),
("stacklevel", stack_level.to_object(py).into_bound(py)),
("stacklevel", stack_level.into_py_any(py)?.into_bound(py)),
];
warnings_warn.call((message,), Some(&kwargs.into_py_dict_bound(py)))?;
warnings_warn.call((message,), Some(&kwargs.into_py_dict(py)?))?;
Ok(())
}

0 comments on commit b1776c7

Please sign in to comment.