Skip to content

Commit 4d6c363

Browse files
authored
fix: Respect strictness in list constructor (#18853)
1 parent 4dd272c commit 4d6c363

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

crates/polars-core/src/series/any_value.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,14 @@ fn any_values_to_list(
563563
Some(b.clone())
564564
} else {
565565
match b.cast(inner_type) {
566-
Ok(out) => Some(out),
566+
Ok(out) => {
567+
if out.null_count() != b.null_count() {
568+
valid = !strict;
569+
}
570+
Some(out)
571+
},
567572
Err(_) => {
573+
valid = !strict;
568574
Some(Series::full_null(b.name().clone(), b.len(), inner_type))
569575
},
570576
}

crates/polars-python/src/series/construction.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,19 @@ init_method_opt!(new_opt_i64, Int64Type, i64);
168168
init_method_opt!(new_opt_f32, Float32Type, f32);
169169
init_method_opt!(new_opt_f64, Float64Type, f64);
170170

171+
fn convert_to_avs<'a>(values: &'a Bound<'a, PyAny>, strict: bool) -> PyResult<Vec<AnyValue<'a>>> {
172+
values
173+
.iter()?
174+
.map(|v| py_object_to_any_value(&(v?).as_borrowed(), strict))
175+
.collect()
176+
}
177+
171178
#[pymethods]
172179
impl PySeries {
173180
#[staticmethod]
174181
fn new_from_any_values(name: &str, values: &Bound<PyAny>, strict: bool) -> PyResult<Self> {
175-
let any_values_result = values
176-
.iter()?
177-
.map(|v| py_object_to_any_value(&(v?).as_borrowed(), strict))
178-
.collect::<PyResult<Vec<AnyValue>>>();
179-
let result = any_values_result.and_then(|avs| {
182+
let avs = convert_to_avs(values, strict);
183+
let result = avs.and_then(|avs| {
180184
let s = Series::from_any_values(name.into(), avs.as_slice(), strict).map_err(|e| {
181185
PyTypeError::new_err(format!(
182186
"{e}\n\nHint: Try setting `strict=False` to allow passing data with mixed types."
@@ -211,17 +215,13 @@ impl PySeries {
211215
dtype: Wrap<DataType>,
212216
strict: bool,
213217
) -> PyResult<Self> {
214-
let any_values = values
215-
.iter()?
216-
.map(|v| py_object_to_any_value(&(v?).as_borrowed(), strict))
217-
.collect::<PyResult<Vec<AnyValue>>>()?;
218-
let s =
219-
Series::from_any_values_and_dtype(name.into(), any_values.as_slice(), &dtype.0, strict)
220-
.map_err(|e| {
221-
PyTypeError::new_err(format!(
218+
let avs = convert_to_avs(values, strict)?;
219+
let s = Series::from_any_values_and_dtype(name.into(), avs.as_slice(), &dtype.0, strict)
220+
.map_err(|e| {
221+
PyTypeError::new_err(format!(
222222
"{e}\n\nHint: Try setting `strict=False` to allow passing data with mixed types."
223223
))
224-
})?;
224+
})?;
225225
Ok(s.into())
226226
}
227227

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import pytest
2+
3+
import polars as pl
4+
5+
6+
def test_list_constructor_strictness() -> None:
7+
with pytest.raises(TypeError, match="setting `strict=False`"):
8+
pl.Series([[1], ["two"]], strict=True)

0 commit comments

Comments
 (0)