Skip to content

Commit

Permalink
Bump to pyo3 0.22 (#226)
Browse files Browse the repository at this point in the history
* Bump to pyo3 0.22

* smaller diff

* fix compile
  • Loading branch information
kylebarron authored Oct 11, 2024
1 parent 2d9861f commit 93de506
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 301 deletions.
274 changes: 18 additions & 256 deletions Cargo.lock

Large diffs are not rendered by default.

11 changes: 3 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,10 @@ arrow-schema = "53"
arrow-select = "53"
half = "2"
indexmap = "2"
# numpy = "0.21"
# TODO: Pin to released version once NumPy 2.0 support is merged
# https://github.com/PyO3/rust-numpy/issues/409
# This is the fork used by polars
# https://github.com/pola-rs/polars/blob/fac700d9670feb57f1df32beaeee38377725fccf/py-polars/Cargo.toml#L33-L35
numpy = { git = "https://github.com/stinodego/rust-numpy.git", rev = "9ba9962ae57ba26e35babdce6f179edf5fe5b9c8", default-features = false }
numpy = "0.22"
parquet = "53"
pyo3 = { version = "0.21", features = ["macros", "indexmap"] }
pyo3-file = "0.8.1"
pyo3 = { version = "0.22", features = ["macros", "indexmap"] }
pyo3-file = "0.9"
thiserror = "1"

[profile.release]
Expand Down
6 changes: 4 additions & 2 deletions arro3-io/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ impl FileReader {

impl<'py> FromPyObject<'py> for FileReader {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let py = ob.py();
if let Ok(path) = ob.extract::<PathBuf>() {
Ok(Self::File(File::open(path)?))
} else if let Ok(path) = ob.extract::<String>() {
Ok(Self::File(File::open(path)?))
} else {
Ok(Self::FileLike(PyFileLikeObject::with_requirements(
ob.as_gil_ref().into(),
ob.into_py(py),
true,
false,
true,
Expand Down Expand Up @@ -114,13 +115,14 @@ pub enum FileWriter {

impl<'py> FromPyObject<'py> for FileWriter {
fn extract_bound(ob: &Bound<'py, PyAny>) -> PyResult<Self> {
let py = ob.py();
if let Ok(path) = ob.extract::<PathBuf>() {
Ok(Self::File(File::create(path)?))
} else if let Ok(path) = ob.extract::<String>() {
Ok(Self::File(File::create(path)?))
} else {
Ok(Self::FileLike(PyFileLikeObject::with_requirements(
ob.as_gil_ref().into(),
ob.into_py(py),
false,
true,
true,
Expand Down
5 changes: 3 additions & 2 deletions pyo3-arrow/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ impl PyArray {
}

#[allow(unused_variables)]
#[pyo3(signature = (requested_schema=None))]
fn __arrow_c_array__<'py>(
&'py self,
py: Python<'py>,
Expand Down Expand Up @@ -339,8 +340,8 @@ impl PyArray {
return buf.try_into();
}

let numpy_array: &PyUntypedArray = FromPyObject::extract_bound(&numpy_array)?;
let arrow_array = from_numpy(py, numpy_array)?;
let numpy_array: Bound<PyUntypedArray> = FromPyObject::extract_bound(&numpy_array)?;
let arrow_array = from_numpy(py, &numpy_array)?;
Ok(Self::from_array_ref(arrow_array))
}

Expand Down
1 change: 1 addition & 0 deletions pyo3-arrow/src/array_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl PyArrayReader {
}

#[allow(unused_variables)]
#[pyo3(signature = (requested_schema=None))]
fn __arrow_c_stream__<'py>(
&'py mut self,
py: Python<'py>,
Expand Down
2 changes: 2 additions & 0 deletions pyo3-arrow/src/chunked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ impl Display for PyChunkedArray {
#[pymethods]
impl PyChunkedArray {
#[new]
#[pyo3(signature = (arrays, r#type=None))]
fn init(arrays: &Bound<PyAny>, r#type: Option<PyField>) -> PyArrowResult<Self> {
if let Ok(data) = arrays.extract::<AnyArray>() {
Ok(data.into_chunked_array()?)
Expand Down Expand Up @@ -295,6 +296,7 @@ impl PyChunkedArray {
}

#[allow(unused_variables)]
#[pyo3(signature = (requested_schema=None))]
fn __arrow_c_stream__<'py>(
&'py self,
py: Python<'py>,
Expand Down
20 changes: 4 additions & 16 deletions pyo3-arrow/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Contains the [`PyArrowError`], the Error returned by most fallible functions in this crate.
use pyo3::exceptions::{PyException, PyTypeError, PyValueError};
use pyo3::exceptions::{PyException, PyValueError};
use pyo3::prelude::*;
use pyo3::PyDowncastError;
use pyo3::DowncastError;
use thiserror::Error;

/// The Error variants returned by this crate.
Expand All @@ -27,26 +27,14 @@ impl From<PyArrowError> for PyErr {
}
}

impl From<PyTypeError> for PyArrowError {
fn from(other: PyTypeError) -> Self {
Self::PyErr((&other).into())
}
}

impl<'a> From<PyDowncastError<'a>> for PyArrowError {
fn from(other: PyDowncastError<'a>) -> Self {
impl<'a, 'py> From<DowncastError<'a, 'py>> for PyArrowError {
fn from(other: DowncastError<'a, 'py>) -> Self {
Self::PyErr(PyValueError::new_err(format!(
"Could not downcast: {}",
other
)))
}
}

impl From<PyValueError> for PyArrowError {
fn from(other: PyValueError) -> Self {
Self::PyErr((&other).into())
}
}

/// A type wrapper around `Result<T, PyArrowError>`.
pub type PyArrowResult<T> = Result<T, PyArrowError>;
37 changes: 20 additions & 17 deletions pyo3-arrow/src/interop/numpy/from_numpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use arrow::datatypes::{
UInt32Type, UInt64Type, UInt8Type,
};
use arrow_array::{ArrayRef, BooleanArray, PrimitiveArray};
use numpy::{dtype_bound, PyArray1, PyArrayDescr, PyUntypedArray};
use numpy::{
dtype_bound, PyArray1, PyArrayDescr, PyArrayDescrMethods, PyArrayMethods, PyUntypedArray,
PyUntypedArrayMethods,
};
use pyo3::exceptions::PyValueError;
use pyo3::Python;
use pyo3::prelude::*;

use crate::error::PyArrowResult;

pub fn from_numpy(py: Python, array: &PyUntypedArray) -> PyArrowResult<ArrayRef> {
pub fn from_numpy(py: Python, array: &Bound<PyUntypedArray>) -> PyArrowResult<ArrayRef> {
macro_rules! numpy_to_arrow {
($rust_type:ty, $arrow_type:ty) => {{
let arr = array.downcast::<PyArray1<$rust_type>>()?;
Expand All @@ -21,36 +24,36 @@ pub fn from_numpy(py: Python, array: &PyUntypedArray) -> PyArrowResult<ArrayRef>
}};
}
let dtype = array.dtype();
if is_type::<half::f16>(py, dtype) {
if is_type::<half::f16>(py, &dtype) {
numpy_to_arrow!(half::f16, Float16Type)
} else if is_type::<f32>(py, dtype) {
} else if is_type::<f32>(py, &dtype) {
numpy_to_arrow!(f32, Float32Type)
} else if is_type::<f64>(py, dtype) {
} else if is_type::<f64>(py, &dtype) {
numpy_to_arrow!(f64, Float64Type)
} else if is_type::<u8>(py, dtype) {
} else if is_type::<u8>(py, &dtype) {
numpy_to_arrow!(u8, UInt8Type)
} else if is_type::<u16>(py, dtype) {
} else if is_type::<u16>(py, &dtype) {
numpy_to_arrow!(u16, UInt16Type)
} else if is_type::<u32>(py, dtype) {
} else if is_type::<u32>(py, &dtype) {
numpy_to_arrow!(u32, UInt32Type)
} else if is_type::<u64>(py, dtype) {
} else if is_type::<u64>(py, &dtype) {
numpy_to_arrow!(u64, UInt64Type)
} else if is_type::<i8>(py, dtype) {
} else if is_type::<i8>(py, &dtype) {
numpy_to_arrow!(i8, Int8Type)
} else if is_type::<i16>(py, dtype) {
} else if is_type::<i16>(py, &dtype) {
numpy_to_arrow!(i16, Int16Type)
} else if is_type::<i32>(py, dtype) {
} else if is_type::<i32>(py, &dtype) {
numpy_to_arrow!(i32, Int32Type)
} else if is_type::<i64>(py, dtype) {
} else if is_type::<i64>(py, &dtype) {
numpy_to_arrow!(i64, Int64Type)
} else if is_type::<bool>(py, dtype) {
} else if is_type::<bool>(py, &dtype) {
let arr = array.downcast::<PyArray1<bool>>()?;
Ok(Arc::new(BooleanArray::from(arr.to_owned_array().to_vec())))
} else {
Err(PyValueError::new_err(format!("Unsupported data type {}", dtype)).into())
}
}

fn is_type<T: numpy::Element>(py: Python, dtype: &PyArrayDescr) -> bool {
dtype.is_equiv_to(dtype_bound::<T>(py).as_gil_ref())
fn is_type<T: numpy::Element>(py: Python, dtype: &Bound<PyArrayDescr>) -> bool {
dtype.is_equiv_to(&dtype_bound::<T>(py))
}
1 change: 1 addition & 0 deletions pyo3-arrow/src/record_batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ impl PyRecordBatch {
}

#[allow(unused_variables)]
#[pyo3(signature = (requested_schema=None))]
fn __arrow_c_array__<'py>(
&'py self,
py: Python<'py>,
Expand Down
1 change: 1 addition & 0 deletions pyo3-arrow/src/record_batch_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ impl PyRecordBatchReader {
}

#[allow(unused_variables)]
#[pyo3(signature = (requested_schema=None))]
fn __arrow_c_stream__<'py>(
&'py mut self,
py: Python<'py>,
Expand Down
1 change: 1 addition & 0 deletions pyo3-arrow/src/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl PyScalar {
}

#[allow(unused_variables)]
#[pyo3(signature = (requested_schema=None))]
fn __arrow_c_array__<'py>(
&'py self,
py: Python<'py>,
Expand Down
1 change: 1 addition & 0 deletions pyo3-arrow/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ impl PyTable {
}

#[allow(unused_variables)]
#[pyo3(signature = (requested_schema=None))]
fn __arrow_c_stream__<'py>(
&'py self,
py: Python<'py>,
Expand Down

0 comments on commit 93de506

Please sign in to comment.