From a25db206ad04650e5fee53e5b282614c18052c19 Mon Sep 17 00:00:00 2001 From: fatimaLaraibBonk Date: Sat, 21 Nov 2026 23:13:30 +0500 Subject: [PATCH 1/5] fix: Apply inner rules to the elements of multi-dimensional arrays For an array with more than one dimension, `dy.Array` evaluated the inner column's rules on the sub-arrays instead of the innermost elements. Rules such as `min_length` or `min` then failed with a polars SchemaError, and `nullable=False` on the inner column was never enforced. Apply the rules one dimension further down per extra dimension, as nested `dy.Array` columns already do. --- dataframely/columns/array.py | 10 +++++- tests/column_types/test_array.py | 55 ++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/dataframely/columns/array.py b/dataframely/columns/array.py index 0d131f84..c9072889 100644 --- a/dataframely/columns/array.py +++ b/dataframely/columns/array.py @@ -87,8 +87,16 @@ def dtype(self) -> pl.DataType: return pl.Array(self.inner.dtype, self.shape) def validation_rules(self, expr: pl.Expr) -> dict[str, pl.Expr]: + def all_elements(inner_expr: pl.Expr) -> pl.Expr: + # The elements of a multi-dimensional array are arrays themselves, so the + # inner rules must be applied one dimension further down for every extra + # dimension. This matches the behavior of nested `dy.Array` columns. + for _ in range(len(self.shape) - 1): + inner_expr = pl.element().arr.eval(inner_expr).arr.all() + return expr.arr.eval(inner_expr).arr.all() + inner_rules = { - f"inner_{rule_name}": expr.arr.eval(inner_expr).arr.all() + f"inner_{rule_name}": all_elements(inner_expr) for rule_name, inner_expr in self.inner.validation_rules( pl.element() ).items() diff --git a/tests/column_types/test_array.py b/tests/column_types/test_array.py index ba3e1e81..96402c1c 100644 --- a/tests/column_types/test_array.py +++ b/tests/column_types/test_array.py @@ -145,6 +145,61 @@ def test_array_with_rules() -> None: assert failures.counts() == {"a|inner_nullability": 1, "a|inner_min_length": 1} +@pytest.mark.parametrize( + ("column", "prefix"), + [ + ( + dy.Array(dy.String(min_length=2, nullable=False), (2, 2), nullable=True), + "inner", + ), + # A multi-dimensional array must behave like the equivalent nested arrays + ( + dy.Array( + dy.Array(dy.String(min_length=2, nullable=False), 2), 2, nullable=True + ), + "inner_inner", + ), + ], +) +def test_multidimensional_array_with_rules(column: Column, prefix: str) -> None: + schema = create_schema("test", {"a": column}) + df = pl.DataFrame( + { + "a": [ + [["ab", "cd"], ["ef", "gh"]], + [["ab", "c"], ["ef", "gh"]], + [["ab", "cd"], [None, "gh"]], + None, + ] + }, + schema={"a": pl.Array(pl.String, (2, 2))}, + ) + _, failures = schema.filter(df) + assert validation_mask(df, failures).to_list() == [True, False, False, True] + assert failures.counts() == { + f"a|{prefix}_nullability": 1, + f"a|{prefix}_min_length": 1, + } + + +def test_multidimensional_array_with_numeric_rules() -> None: + schema = create_schema("test", {"a": dy.Array(dy.Int64(min=0), (2, 3))}) + df = pl.DataFrame( + {"a": [[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, -4, 5]]]}, + schema={"a": pl.Array(pl.Int64, (2, 3))}, + ) + _, failures = schema.filter(df) + assert validation_mask(df, failures).to_list() == [True, False] + assert failures.counts() == {"a|inner_min": 1} + + +def test_multidimensional_array_sample_is_valid() -> None: + schema = create_schema( + "test", {"a": dy.Array(dy.String(min_length=1, max_length=3), (2, 2))} + ) + assert schema.is_valid(schema.sample(50)) + + def test_array_with_pk() -> None: schema = create_schema( "test", From 5524f30f1a7a234025188fdc617b8f3c51bd8763 Mon Sep 17 00:00:00 2001 From: Fatima Laraib Date: Sat, 21 Nov 2026 23:21:49 +0500 Subject: [PATCH 2/5] Potential fix for pull request finding 'Add Arrange, Act, and Assert markers to the new tests' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tests/column_types/test_array.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/column_types/test_array.py b/tests/column_types/test_array.py index 96402c1c..5f884a05 100644 --- a/tests/column_types/test_array.py +++ b/tests/column_types/test_array.py @@ -162,6 +162,7 @@ def test_array_with_rules() -> None: ], ) def test_multidimensional_array_with_rules(column: Column, prefix: str) -> None: + # Arrange schema = create_schema("test", {"a": column}) df = pl.DataFrame( { @@ -174,7 +175,11 @@ def test_multidimensional_array_with_rules(column: Column, prefix: str) -> None: }, schema={"a": pl.Array(pl.String, (2, 2))}, ) + + # Act _, failures = schema.filter(df) + + # Assert assert validation_mask(df, failures).to_list() == [True, False, False, True] assert failures.counts() == { f"a|{prefix}_nullability": 1, From 626f44c9ed11f0005896aff9ab605f84fd7f7490 Mon Sep 17 00:00:00 2001 From: Fatima Laraib Date: Sat, 21 Nov 2026 23:37:03 +0500 Subject: [PATCH 3/5] Potential fix for pull request finding 'Add Arrange, Act, and Assert markers to numeric regression tests' Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tests/column_types/test_array.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/tests/column_types/test_array.py b/tests/column_types/test_array.py index 5f884a05..e2faff53 100644 --- a/tests/column_types/test_array.py +++ b/tests/column_types/test_array.py @@ -188,21 +188,32 @@ def test_multidimensional_array_with_rules(column: Column, prefix: str) -> None: def test_multidimensional_array_with_numeric_rules() -> None: + # Arrange schema = create_schema("test", {"a": dy.Array(dy.Int64(min=0), (2, 3))}) df = pl.DataFrame( {"a": [[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, -4, 5]]]}, schema={"a": pl.Array(pl.Int64, (2, 3))}, ) + + # Act _, failures = schema.filter(df) + + # Assert assert validation_mask(df, failures).to_list() == [True, False] assert failures.counts() == {"a|inner_min": 1} def test_multidimensional_array_sample_is_valid() -> None: + # Arrange schema = create_schema( "test", {"a": dy.Array(dy.String(min_length=1, max_length=3), (2, 2))} ) - assert schema.is_valid(schema.sample(50)) + + # Act + df = schema.sample(50) + + # Assert + assert schema.is_valid(df) def test_array_with_pk() -> None: From 786e9d3faa1e71dd34d036a01cd3606651235ca5 Mon Sep 17 00:00:00 2001 From: fatimaLaraibBonk Date: Mon, 23 Nov 2026 13:56:59 +0500 Subject: [PATCH 4/5] test: Cover three-dimensional arrays with inner rules Parametrize the numeric rule test with a (2, 2, 2) shape, so the inner rule is wrapped twice and an invalid innermost value is still caught. --- tests/column_types/test_array.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/tests/column_types/test_array.py b/tests/column_types/test_array.py index e2faff53..9dc2e322 100644 --- a/tests/column_types/test_array.py +++ b/tests/column_types/test_array.py @@ -1,6 +1,8 @@ # Copyright (c) QuantCo 2025-2026 # SPDX-License-Identifier: BSD-3-Clause +from typing import Any + import polars as pl import pytest @@ -187,13 +189,26 @@ def test_multidimensional_array_with_rules(column: Column, prefix: str) -> None: } -def test_multidimensional_array_with_numeric_rules() -> None: +@pytest.mark.parametrize( + ("shape", "data"), + [ + ((2, 3), [[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, -4, 5]]]), + # Three dimensions wrap the inner rule twice. + ( + (2, 2, 2), + [ + [[[0, 1], [2, 3]], [[4, 5], [6, 7]]], + [[[0, 1], [2, 3]], [[4, 5], [6, -7]]], + ], + ), + ], +) +def test_multidimensional_array_with_numeric_rules( + shape: tuple[int, ...], data: list[Any] +) -> None: # Arrange - schema = create_schema("test", {"a": dy.Array(dy.Int64(min=0), (2, 3))}) - df = pl.DataFrame( - {"a": [[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, -4, 5]]]}, - schema={"a": pl.Array(pl.Int64, (2, 3))}, - ) + schema = create_schema("test", {"a": dy.Array(dy.Int64(min=0), shape)}) + df = pl.DataFrame({"a": data}, schema={"a": pl.Array(pl.Int64, shape)}) # Act _, failures = schema.filter(df) From 1330e27e81a116fa45f9bac93605f05814ab6a3d Mon Sep 17 00:00:00 2001 From: Oliver Borchert Date: Tue, 1 Dec 2026 16:37:08 -0800 Subject: [PATCH 5/5] Slight simplification --- dataframely/columns/array.py | 2 +- tests/column_types/test_array.py | 13 ------------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/dataframely/columns/array.py b/dataframely/columns/array.py index c9072889..91e9e3ae 100644 --- a/dataframely/columns/array.py +++ b/dataframely/columns/array.py @@ -90,7 +90,7 @@ def validation_rules(self, expr: pl.Expr) -> dict[str, pl.Expr]: def all_elements(inner_expr: pl.Expr) -> pl.Expr: # The elements of a multi-dimensional array are arrays themselves, so the # inner rules must be applied one dimension further down for every extra - # dimension. This matches the behavior of nested `dy.Array` columns. + # dimension. for _ in range(len(self.shape) - 1): inner_expr = pl.element().arr.eval(inner_expr).arr.all() return expr.arr.eval(inner_expr).arr.all() diff --git a/tests/column_types/test_array.py b/tests/column_types/test_array.py index 9dc2e322..70f3b45b 100644 --- a/tests/column_types/test_array.py +++ b/tests/column_types/test_array.py @@ -218,19 +218,6 @@ def test_multidimensional_array_with_numeric_rules( assert failures.counts() == {"a|inner_min": 1} -def test_multidimensional_array_sample_is_valid() -> None: - # Arrange - schema = create_schema( - "test", {"a": dy.Array(dy.String(min_length=1, max_length=3), (2, 2))} - ) - - # Act - df = schema.sample(50) - - # Assert - assert schema.is_valid(df) - - def test_array_with_pk() -> None: schema = create_schema( "test",