Skip to content

Commit

Permalink
feat(deps): upgrade pyo3 (#325)
Browse files Browse the repository at this point in the history
* chore(deps): upgrade pyo3 and arrow

Signed-off-by: Luka Peschke <[email protected]>

* chore: fix all pyo3 deprecation warnings

Signed-off-by: Luka Peschke <[email protected]>

---------

Signed-off-by: Luka Peschke <[email protected]>
  • Loading branch information
lukapeschke authored Jan 22, 2025
1 parent c8fea77 commit 0d4f087
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 210 deletions.
250 changes: 125 additions & 125 deletions Cargo.lock

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ crate-type = ["cdylib"]

[dependencies]
# There's a lot of stuff we don't want here, such as serde support
arrow = { version = "53.2.0", default-features = false, features = ["pyarrow"] }
calamine = { version = "0.26.1", features = ["dates"] }
chrono = { version = "0.4.39", default-features = false }
arrow = { version = "^54.0.0", default-features = false, features = [
"pyarrow",
] }
calamine = { version = "^0.26.1", features = ["dates"] }
chrono = { version = "^0.4.39", default-features = false }
log = "0.4.25"
pyo3 = { version = "0.22.6", features = ["abi3-py39"] }
pyo3-log = "0.11.0"
pyo3 = { version = "^0.23.4", features = ["abi3-py39"] }
pyo3-log = "^0.12.1"

[dev-dependencies]
pretty_assertions = "1.4.1"
rstest = { version = "0.24.0", default-features = false }
pretty_assertions = "^1.4.1"
rstest = { version = "^0.24.0", default-features = false }

# NOTE: This is a hack to bypass pyo3 limitations when testing:
# https://pyo3.rs/v0.22.3/faq.html#i-cant-run-cargo-test-or-i-cant-build-in-a-cargo-workspace-im-having-linker-issues-like-symbol-not-found-or-undefined-reference-to-_pyexc_systemerror
Expand Down
24 changes: 9 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,38 +55,32 @@ fn _fastexcel(m: &Bound<'_, PyModule>) -> PyResult<()> {

// errors
[
(
"FastExcelError",
py.get_type_bound::<py_errors::FastExcelError>(),
),
("FastExcelError", py.get_type::<py_errors::FastExcelError>()),
(
"UnsupportedColumnTypeCombinationError",
py.get_type_bound::<py_errors::UnsupportedColumnTypeCombinationError>(),
py.get_type::<py_errors::UnsupportedColumnTypeCombinationError>(),
),
(
"CannotRetrieveCellDataError",
py.get_type_bound::<py_errors::CannotRetrieveCellDataError>(),
py.get_type::<py_errors::CannotRetrieveCellDataError>(),
),
(
"CalamineCellError",
py.get_type_bound::<py_errors::CalamineCellError>(),
),
(
"CalamineError",
py.get_type_bound::<py_errors::CalamineError>(),
py.get_type::<py_errors::CalamineCellError>(),
),
("CalamineError", py.get_type::<py_errors::CalamineError>()),
(
"SheetNotFoundError",
py.get_type_bound::<py_errors::SheetNotFoundError>(),
py.get_type::<py_errors::SheetNotFoundError>(),
),
(
"ColumnNotFoundError",
py.get_type_bound::<py_errors::ColumnNotFoundError>(),
py.get_type::<py_errors::ColumnNotFoundError>(),
),
("ArrowError", py.get_type_bound::<py_errors::ArrowError>()),
("ArrowError", py.get_type::<py_errors::ArrowError>()),
(
"InvalidParametersError",
py.get_type_bound::<py_errors::InvalidParametersError>(),
py.get_type::<py_errors::InvalidParametersError>(),
),
]
.into_iter()
Expand Down
42 changes: 28 additions & 14 deletions src/types/dtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use arrow::datatypes::{DataType as ArrowDataType, TimeUnit};
use calamine::{CellErrorType, CellType, DataType, Range};
use log::warn;
use pyo3::{
prelude::PyAnyMethods, Bound, FromPyObject, PyAny, PyObject, PyResult, Python, ToPyObject,
prelude::PyAnyMethods, types::PyString, Bound, FromPyObject, IntoPyObject, IntoPyObjectRef,
PyAny, PyResult, Python,
};

use crate::error::{py_errors::IntoPyResult, FastExcelError, FastExcelErrorKind, FastExcelResult};
Expand Down Expand Up @@ -64,9 +65,26 @@ impl Display for DType {
}
}

impl ToPyObject for DType {
fn to_object(&self, py: Python<'_>) -> PyObject {
self.to_string().to_object(py)
impl<'py> IntoPyObject<'py> for DType {
type Target = PyString;

type Output = Bound<'py, Self::Target>;

type Error = std::convert::Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
self.to_string().into_pyobject(py)
}
}
impl<'py> IntoPyObject<'py> for &DType {
type Target = PyString;

type Output = Bound<'py, Self::Target>;

type Error = std::convert::Infallible;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
self.to_string().into_pyobject(py)
}
}

Expand All @@ -86,6 +104,7 @@ impl FromPyObject<'_> for DType {

pub(crate) type DTypeMap = HashMap<IdxOrName, DType>;

#[derive(IntoPyObject, IntoPyObjectRef)]
pub(crate) enum DTypes {
All(DType),
Map(DTypeMap),
Expand All @@ -110,15 +129,6 @@ impl FromPyObject<'_> for DTypes {
}
}

impl ToPyObject for DTypes {
fn to_object(&self, py: Python<'_>) -> PyObject {
match self {
DTypes::All(dtype) => dtype.to_object(py),
DTypes::Map(dtype_map) => dtype_map.to_object(py),
}
}
}

impl From<&DType> for ArrowDataType {
fn from(dtype: &DType) -> Self {
match dtype {
Expand Down Expand Up @@ -229,7 +239,11 @@ fn get_cell_dtype<DT: CellType + Debug + DataType>(
match cell.get_error() {
// considering cells with #N/A! or #REF! as null
Some(
CellErrorType::NA | CellErrorType::Value | CellErrorType::Null | CellErrorType::Ref | CellErrorType::Num,
CellErrorType::NA
| CellErrorType::Value
| CellErrorType::Null
| CellErrorType::Ref
| CellErrorType::Num,
) => Ok(DType::Null),
Some(err) => Err(FastExcelErrorKind::CalamineCellError(err.to_owned()).into()),
None => Err(FastExcelErrorKind::Internal(format!(
Expand Down
32 changes: 27 additions & 5 deletions src/types/idx_or_name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use pyo3::{
prelude::PyAnyMethods, Bound, FromPyObject, PyAny, PyObject, PyResult, Python, ToPyObject,
prelude::PyAnyMethods, Bound, FromPyObject, IntoPyObject, IntoPyObjectExt, PyAny, PyResult,
Python,
};

use crate::error::{py_errors::IntoPyResult, FastExcelError, FastExcelErrorKind, FastExcelResult};
Expand Down Expand Up @@ -42,11 +43,32 @@ impl FromPyObject<'_> for IdxOrName {
}
}

impl ToPyObject for IdxOrName {
fn to_object(&self, py: Python<'_>) -> PyObject {
impl<'py> IntoPyObject<'py> for IdxOrName {
type Target = PyAny;

type Output = Bound<'py, Self::Target>;

type Error = pyo3::PyErr;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match self {
IdxOrName::Idx(idx) => idx.into_bound_py_any(py),
IdxOrName::Name(name) => name.into_bound_py_any(py),
}
}
}

impl<'py> IntoPyObject<'py> for &IdxOrName {
type Target = PyAny;

type Output = Bound<'py, Self::Target>;

type Error = pyo3::PyErr;

fn into_pyobject(self, py: Python<'py>) -> Result<Self::Output, Self::Error> {
match self {
IdxOrName::Idx(idx) => idx.to_object(py),
IdxOrName::Name(name) => name.to_object(py),
IdxOrName::Idx(idx) => idx.into_bound_py_any(py),
IdxOrName::Name(name) => name.into_bound_py_any(py),
}
}
}
Expand Down
43 changes: 22 additions & 21 deletions src/types/python/excelreader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use arrow::{pyarrow::ToPyArrow, record_batch::RecordBatch};
use pyo3::{prelude::PyObject, pyclass, pymethods, Bound, IntoPy, PyAny, PyResult, Python};
use pyo3::{pyclass, pymethods, Bound, IntoPyObjectExt, PyAny, PyResult, Python};

use calamine::{
open_workbook_auto, open_workbook_auto_from_rs, Data, DataRef, HeaderRow, Range, Reader,
Expand Down Expand Up @@ -172,7 +172,7 @@ impl ExcelReader {
}

#[allow(clippy::too_many_arguments)]
fn build_sheet(
fn build_sheet<'py>(
&mut self,
sheet_meta: CalamineSheet,
header_row: Option<usize>,
Expand All @@ -181,11 +181,11 @@ impl ExcelReader {
n_rows: Option<usize>,
schema_sample_rows: Option<usize>,
dtype_coercion: DTypeCoercion,
use_columns: Option<&Bound<'_, PyAny>>,
use_columns: Option<&Bound<'py, PyAny>>,
dtypes: Option<DTypes>,
eager: bool,
py: Python<'_>,
) -> PyResult<PyObject> {
py: Python<'py>,
) -> PyResult<Bound<'py, PyAny>> {
// calamine `header_row` is the first row of the range to be read.
// For us `header_row` can be `None` (meaning there is no header and we should start reading
// the data at the beginning)
Expand Down Expand Up @@ -219,6 +219,7 @@ impl ExcelReader {
)
.into_pyresult()
.and_then(|rb| rb.to_pyarrow(py))
.map(|py_object| py_object.into_bound(py))
} else {
let range = self
.sheets
Expand All @@ -242,13 +243,13 @@ impl ExcelReader {
if eager {
sheet.to_arrow(py)
} else {
Ok(sheet.into_py(py))
sheet.into_bound_py_any(py)
}
}
}

#[allow(clippy::too_many_arguments)]
fn build_table(
fn build_table<'py>(
&mut self,
name: String,
header_row: Option<usize>,
Expand All @@ -260,8 +261,8 @@ impl ExcelReader {
use_columns: Option<&Bound<'_, PyAny>>,
dtypes: Option<DTypes>,
eager: bool,
py: Python<'_>,
) -> PyResult<PyObject> {
py: Python<'py>,
) -> PyResult<Bound<'py, PyAny>> {
let selected_columns = Self::build_selected_columns(use_columns).into_pyresult()?;

let table = self.sheets.get_table(&name).into_pyresult()?;
Expand All @@ -287,9 +288,9 @@ impl ExcelReader {
.into_pyresult()?;

if eager {
excel_table.to_arrow(py)
excel_table.to_arrow(py).map(|py_obj| py_obj.into_bound(py))
} else {
Ok(excel_table.into_py(py))
excel_table.into_bound_py_any(py)
}
}
}
Expand Down Expand Up @@ -336,20 +337,20 @@ impl ExcelReader {
eager = false,
))]
#[allow(clippy::too_many_arguments)]
pub fn load_sheet(
pub fn load_sheet<'py>(
&mut self,
idx_or_name: &Bound<'_, PyAny>,
idx_or_name: &Bound<'py, PyAny>,
header_row: Option<usize>,
column_names: Option<Vec<String>>,
skip_rows: Option<usize>,
n_rows: Option<usize>,
schema_sample_rows: Option<usize>,
dtype_coercion: DTypeCoercion,
use_columns: Option<&Bound<'_, PyAny>>,
use_columns: Option<&Bound<'py, PyAny>>,
dtypes: Option<DTypes>,
eager: bool,
py: Python<'_>,
) -> PyResult<PyObject> {
py: Python<'py>,
) -> PyResult<Bound<'py, PyAny>> {
// Cannot use NonZeroUsize in the parameters, as it is not supported by pyo3
if let Some(0) = schema_sample_rows {
return Err(FastExcelErrorKind::InvalidParameters(
Expand Down Expand Up @@ -414,20 +415,20 @@ impl ExcelReader {
eager = false,
))]
#[allow(clippy::too_many_arguments)]
pub fn load_table(
pub fn load_table<'py>(
&mut self,
name: &Bound<'_, PyString>,
name: &Bound<'py, PyString>,
header_row: Option<usize>,
column_names: Option<Vec<String>>,
skip_rows: usize,
n_rows: Option<usize>,
schema_sample_rows: Option<usize>,
dtype_coercion: DTypeCoercion,
use_columns: Option<&Bound<'_, PyAny>>,
use_columns: Option<&Bound<'py, PyAny>>,
dtypes: Option<DTypes>,
eager: bool,
py: Python<'_>,
) -> PyResult<PyObject> {
py: Python<'py>,
) -> PyResult<Bound<'py, PyAny>> {
// Cannot use NonZeroUsize in the parameters, as it is not supported by pyo3
if let Some(0) = schema_sample_rows {
return Err(FastExcelErrorKind::InvalidParameters(
Expand Down
Loading

0 comments on commit 0d4f087

Please sign in to comment.