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] 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 + + # __ __ ___ # | \/ |_ _| _ \_ _ # | |\/| | || | _/ || |