Skip to content
New issue

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

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

Already on GitHub? # to your account

feat(python): Improve error message when passing invalid input to lit #15718

Merged
merged 2 commits into from
Apr 17, 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
6 changes: 4 additions & 2 deletions py-polars/src/functions/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,10 @@ pub fn lit(value: &PyAny, allow_object: bool) -> PyResult<PyExpr> {
Ok(dsl::lit(s).into())
} else {
Err(PyTypeError::new_err(format!(
"invalid literal value: {:?}",
value.str()?
"cannot create expression literal for value of type {}: {}\
\n\nHint: Pass `allow_object=True` to accept any value and create a literal of type Object.",
value.get_type().qualname()?,
value.repr()?
)))
}
}
Expand Down
8 changes: 8 additions & 0 deletions py-polars/tests/unit/functions/test_lit.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,11 @@ def test_list_datetime_11571() -> None:
)
def test_lit_int_return_type(input: int, dtype: pl.PolarsDataType) -> None:
assert pl.select(pl.lit(input)).to_series().dtype == dtype


def test_lit_unsupported_type() -> None:
with pytest.raises(
TypeError,
match="cannot create expression literal for value of type LazyFrame: ",
):
pl.lit(pl.LazyFrame({"a": [1, 2, 3]}))
11 changes: 11 additions & 0 deletions py-polars/tests/unit/operations/test_with_columns.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

import polars as pl
from polars.testing import assert_frame_equal

Expand Down Expand Up @@ -165,3 +167,12 @@ def test_with_columns_seq() -> None:
}
)
assert_frame_equal(result, expected)


# https://github.com/pola-rs/polars/issues/15588
def test_with_columns_invalid_type() -> None:
lf = pl.LazyFrame({"a": [1, 2, 3]})
with pytest.raises(
TypeError, match="cannot create expression literal for value of type LazyFrame"
):
lf.with_columns(lf) # type: ignore[arg-type]
Loading