From 1356f62ebef099f22a8ef512c2738fc2613a953b Mon Sep 17 00:00:00 2001 From: Yshuro <83331159+Yshuro@users.noreply.github.com> Date: Thu, 19 Nov 2026 01:38:25 +0100 Subject: [PATCH 1/3] Add explicit truthiness semantics for constant BooleanExpressions (#3543) --- pyiceberg/expressions/__init__.py | 15 ++++++++++ tests/expressions/test_expressions.py | 41 +++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/pyiceberg/expressions/__init__.py b/pyiceberg/expressions/__init__.py index ad916171c5..5e317ff49d 100644 --- a/pyiceberg/expressions/__init__.py +++ b/pyiceberg/expressions/__init__.py @@ -52,6 +52,13 @@ def _to_literal(value: L | Literal[L]) -> Literal[L]: class BooleanExpression(IcebergBaseModel, ABC): """An expression that evaluates to a boolean.""" + def __bool__(self) -> bool: + """Raise an error to prevent truthiness evaluation of non-constant expressions.""" + raise TypeError( + f"Cannot evaluate truthiness of non-constant BooleanExpression: {self}. " + "Use bitwise operators (~, &, |) for expressions or comparison with AlwaysTrue()/AlwaysFalse()." + ) + @abstractmethod def __invert__(self) -> BooleanExpression: """Transform the Expression into its negated version.""" @@ -452,6 +459,10 @@ class AlwaysTrue(BooleanExpression, Singleton, IcebergRootModel[bool]): root: bool = True + def __bool__(self) -> bool: + """Return True for AlwaysTrue.""" + return True + def __invert__(self) -> AlwaysFalse: """Transform the Expression into its negated version.""" return AlwaysFalse() @@ -470,6 +481,10 @@ class AlwaysFalse(BooleanExpression, Singleton, IcebergRootModel[bool]): root: bool = False + def __bool__(self) -> bool: + """Return False for AlwaysFalse.""" + return False + def __invert__(self) -> AlwaysTrue: """Transform the Expression into its negated version.""" return AlwaysTrue() diff --git a/tests/expressions/test_expressions.py b/tests/expressions/test_expressions.py index ff0c5e80aa..ac6742bed9 100644 --- a/tests/expressions/test_expressions.py +++ b/tests/expressions/test_expressions.py @@ -1397,6 +1397,47 @@ def test_deepcopy_then_pickle() -> None: assert restored == expr +def test_always_true_truthiness() -> None: + expr = AlwaysTrue() + assert bool(expr) is True + assert (not expr) is False + executed = False + if expr: + executed = True + assert executed is True + + +def test_always_false_truthiness() -> None: + expr = AlwaysFalse() + assert bool(expr) is False + assert (not expr) is True + executed = False + if not expr: + executed = True + assert executed is True + + +@pytest.mark.parametrize( + "expr", + [ + EqualTo("a", 1), + NotEqualTo("a", 1), + And(AlwaysTrue(), EqualTo("a", 1)), + Or(AlwaysFalse(), EqualTo("a", 1)), + Not(EqualTo("a", 1)), + In("a", (1, 2)), + NotIn("a", (1, 2)), + ], +) +def test_non_constant_boolean_expression_truthiness_raises(expr: BooleanExpression) -> None: + with pytest.raises(TypeError, match="Cannot evaluate truthiness of non-constant BooleanExpression"): + bool(expr) + + with pytest.raises(TypeError, match="Cannot evaluate truthiness of non-constant BooleanExpression"): + if expr: + pass + + # __ __ ___ # | \/ |_ _| _ \_ _ # | |\/| | || | _/ || | From afa1cd37b711380bddf17bea59fce0fbfcb45c40 Mon Sep 17 00:00:00 2001 From: Sami Chergui <83331159+Yshuro@users.noreply.github.com> Date: Thu, 19 Nov 2026 08:37:26 +0100 Subject: [PATCH 2/3] fix(catalog): avoid truthiness check on request filter in rest scan planning Signed-off-by: Sami Chergui <83331159+Yshuro@users.noreply.github.com> --- pyiceberg/catalog/rest/__init__.py | 2 +- tests/io/test_pyarrow.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyiceberg/catalog/rest/__init__.py b/pyiceberg/catalog/rest/__init__.py index 2ad3684c9a..94bf984a3d 100644 --- a/pyiceberg/catalog/rest/__init__.py +++ b/pyiceberg/catalog/rest/__init__.py @@ -904,7 +904,7 @@ def _plan_scan_result(self, identifier: str | Identifier, request: PlanTableScan if not isinstance(response, PlanCompleted): raise RuntimeError(f"Invalid planStatus for response: {type(response).__name__}") - tasks = self._expand_plan_tasks(identifier, response, request.filter or AlwaysTrue()) + tasks = self._expand_plan_tasks(identifier, response, request.filter if request.filter is not None else AlwaysTrue()) return PlannedScanResult( tasks=tasks, storage_credentials=list(response.storage_credentials or []), diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index ca21fd05f0..653c48b9f4 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -1259,7 +1259,7 @@ def _set_spec_id(datafile: DataFile) -> DataFile: ), io=PyArrowFileIO(), projected_schema=schema, - row_filter=expr or AlwaysTrue(), + row_filter=expr if expr is not None else AlwaysTrue(), case_sensitive=True, ).to_table( tasks=[ From ac8f86a6555c0c0cde13d9087fb867c6fbce24ca Mon Sep 17 00:00:00 2001 From: Yshuro Date: Mon, 23 Nov 2026 14:25:42 +0100 Subject: [PATCH 3/3] Improve truthiness error message and add clarification comments Signed-off-by: Yshuro --- pyiceberg/catalog/rest/__init__.py | 1 + pyiceberg/expressions/__init__.py | 2 +- tests/expressions/test_expressions.py | 8 ++++++-- tests/io/test_pyarrow.py | 1 + 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pyiceberg/catalog/rest/__init__.py b/pyiceberg/catalog/rest/__init__.py index 94bf984a3d..88981ff85b 100644 --- a/pyiceberg/catalog/rest/__init__.py +++ b/pyiceberg/catalog/rest/__init__.py @@ -904,6 +904,7 @@ def _plan_scan_result(self, identifier: str | Identifier, request: PlanTableScan if not isinstance(response, PlanCompleted): raise RuntimeError(f"Invalid planStatus for response: {type(response).__name__}") + # BooleanExpression disallows truthiness checks; avoid `request.filter or AlwaysTrue()` tasks = self._expand_plan_tasks(identifier, response, request.filter if request.filter is not None else AlwaysTrue()) return PlannedScanResult( tasks=tasks, diff --git a/pyiceberg/expressions/__init__.py b/pyiceberg/expressions/__init__.py index 5e317ff49d..c6bdac17e0 100644 --- a/pyiceberg/expressions/__init__.py +++ b/pyiceberg/expressions/__init__.py @@ -56,7 +56,7 @@ def __bool__(self) -> bool: """Raise an error to prevent truthiness evaluation of non-constant expressions.""" raise TypeError( f"Cannot evaluate truthiness of non-constant BooleanExpression: {self}. " - "Use bitwise operators (~, &, |) for expressions or comparison with AlwaysTrue()/AlwaysFalse()." + "Use bitwise operators (~, &, |) instead of `and`, `or`, `not` for combining expressions." ) @abstractmethod diff --git a/tests/expressions/test_expressions.py b/tests/expressions/test_expressions.py index ac6742bed9..1832483be5 100644 --- a/tests/expressions/test_expressions.py +++ b/tests/expressions/test_expressions.py @@ -1430,10 +1430,14 @@ def test_always_false_truthiness() -> None: ], ) def test_non_constant_boolean_expression_truthiness_raises(expr: BooleanExpression) -> None: - with pytest.raises(TypeError, match="Cannot evaluate truthiness of non-constant BooleanExpression"): + expected_pattern = ( + r"Cannot evaluate truthiness of non-constant BooleanExpression: .* " + r"Use bitwise operators \(~, &, \|\) instead of `and`, `or`, `not` for combining expressions\." + ) + with pytest.raises(TypeError, match=expected_pattern): bool(expr) - with pytest.raises(TypeError, match="Cannot evaluate truthiness of non-constant BooleanExpression"): + with pytest.raises(TypeError, match=expected_pattern): if expr: pass diff --git a/tests/io/test_pyarrow.py b/tests/io/test_pyarrow.py index 653c48b9f4..3a33fb2cde 100644 --- a/tests/io/test_pyarrow.py +++ b/tests/io/test_pyarrow.py @@ -1259,6 +1259,7 @@ def _set_spec_id(datafile: DataFile) -> DataFile: ), io=PyArrowFileIO(), projected_schema=schema, + # BooleanExpression disallows truthiness checks; avoid `expr or AlwaysTrue()` row_filter=expr if expr is not None else AlwaysTrue(), case_sensitive=True, ).to_table(