Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(python): Enforce integer dtype input for int_range and int_ranges #15339

Merged
merged 3 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions py-polars/src/functions/eager.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use polars::functions;
use polars_core::prelude::*;
use polars_core::with_match_physical_integer_polars_type;
use polars_ops::series::new_int_range;
use pyo3::prelude::*;

use crate::conversion::{get_df, get_series, Wrap};
use crate::conversion::{get_df, get_series};
use crate::error::PyPolarsErr;
use crate::{PyDataFrame, PySeries};

Expand Down Expand Up @@ -93,21 +91,3 @@ pub fn concat_df_horizontal(dfs: &PyAny) -> PyResult<PyDataFrame> {
let df = functions::concat_df_horizontal(&dfs).map_err(PyPolarsErr::from)?;
Ok(df.into())
}

#[pyfunction]
pub fn eager_int_range(
lower: &PyAny,
upper: &PyAny,
step: &PyAny,
dtype: Wrap<DataType>,
) -> PyResult<PySeries> {
let ret = with_match_physical_integer_polars_type!(dtype.0, |$T| {
let start_v: <$T as PolarsNumericType>::Native = lower.extract()?;
let end_v: <$T as PolarsNumericType>::Native = upper.extract()?;
let step: i64 = step.extract()?;
new_int_range::<$T>(start_v, end_v, step, "literal")
});

let s = ret.map_err(PyPolarsErr::from)?;
Ok(s.into())
}
46 changes: 43 additions & 3 deletions py-polars/src/functions/range.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use polars::lazy::dsl;
use polars_core::with_match_physical_integer_polars_type;
use pyo3::prelude::*;

use crate::error::PyPolarsErr;
use crate::prelude::*;
use crate::PyExpr;
use crate::{PyExpr, PySeries};

#[pyfunction]
pub fn int_range(start: PyExpr, end: PyExpr, step: i64, dtype: Wrap<DataType>) -> PyExpr {
Expand All @@ -12,17 +14,55 @@ pub fn int_range(start: PyExpr, end: PyExpr, step: i64, dtype: Wrap<DataType>) -
dsl::int_range(start, end, step, dtype).into()
}

/// Eager version of `int_range` to avoid overhead from the expression engine.
#[pyfunction]
pub fn int_ranges(start: PyExpr, end: PyExpr, step: PyExpr, dtype: Wrap<DataType>) -> PyExpr {
pub fn eager_int_range(
lower: &PyAny,
upper: &PyAny,
step: &PyAny,
dtype: Wrap<DataType>,
) -> PyResult<PySeries> {
let dtype = dtype.0;
if !dtype.is_integer() {
return Err(PyPolarsErr::from(
polars_err!(ComputeError: "non-integer `dtype` passed to `int_range`: {:?}", dtype),
)
.into());
}

let ret = with_match_physical_integer_polars_type!(dtype, |$T| {
let start_v: <$T as PolarsNumericType>::Native = lower.extract()?;
let end_v: <$T as PolarsNumericType>::Native = upper.extract()?;
let step: i64 = step.extract()?;
new_int_range::<$T>(start_v, end_v, step, "literal")
});

let s = ret.map_err(PyPolarsErr::from)?;
Ok(s.into())
}

#[pyfunction]
pub fn int_ranges(
start: PyExpr,
end: PyExpr,
step: PyExpr,
dtype: Wrap<DataType>,
) -> PyResult<PyExpr> {
let dtype = dtype.0;
if !dtype.is_integer() {
return Err(PyPolarsErr::from(
polars_err!(ComputeError: "non-integer `dtype` passed to `int_ranges`: {:?}", dtype),
)
.into());
}

let mut result = dsl::int_ranges(start.inner, end.inner, step.inner);

if dtype != DataType::Int64 {
result = result.cast(DataType::List(Box::new(dtype)))
}

result.into()
Ok(result.into())
}

#[pyfunction]
Expand Down
4 changes: 2 additions & 2 deletions py-polars/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ fn polars(py: Python, m: &PyModule) -> PyResult<()> {
.unwrap();
m.add_wrapped(wrap_pyfunction!(functions::concat_df_horizontal))
.unwrap();
m.add_wrapped(wrap_pyfunction!(functions::eager_int_range))
.unwrap();

// Functions - range
m.add_wrapped(wrap_pyfunction!(functions::int_range))
.unwrap();
m.add_wrapped(wrap_pyfunction!(functions::eager_int_range))
.unwrap();
m.add_wrapped(wrap_pyfunction!(functions::int_ranges))
.unwrap();
m.add_wrapped(wrap_pyfunction!(functions::date_range))
Expand Down
16 changes: 16 additions & 0 deletions py-polars/tests/unit/functions/range/test_int_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,19 @@ def test_int_ranges_broadcasting() -> None:
}
)
assert_frame_equal(result, expected)


# https://github.com/pola-rs/polars/issues/15307
def test_int_range_non_int_dtype() -> None:
with pytest.raises(
pl.ComputeError, match="non-integer `dtype` passed to `int_range`: String"
):
pl.int_range(0, 3, dtype=pl.String, eager=True) # type: ignore[arg-type]


# https://github.com/pola-rs/polars/issues/15307
def test_int_ranges_non_int_dtype() -> None:
with pytest.raises(
pl.ComputeError, match="non-integer `dtype` passed to `int_ranges`: String"
):
pl.int_ranges(0, 3, dtype=pl.String, eager=True) # type: ignore[arg-type]
Loading