From 8ae1c5272c5b8eb0990f712268c621cbf71f5be5 Mon Sep 17 00:00:00 2001 From: Anurag Singh Date: Wed, 2 Dec 2026 00:17:44 +0530 Subject: [PATCH] Widen expression term fields to accept strings mypy reads the field annotation rather than the explicit constructor, so str terms were rejected unless the pydantic plugin was enabled. Normalize strings with BeforeValidator and cast for binding. --- pyiceberg/expressions/__init__.py | 16 ++++++++-------- tests/expressions/test_expressions.py | 5 +++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/pyiceberg/expressions/__init__.py b/pyiceberg/expressions/__init__.py index ad916171c5..7bc33ea118 100644 --- a/pyiceberg/expressions/__init__.py +++ b/pyiceberg/expressions/__init__.py @@ -21,10 +21,10 @@ from abc import ABC, abstractmethod from collections.abc import Callable, Iterable, Sequence from functools import cached_property -from typing import Any, TypeAlias +from typing import Annotated, Any, TypeAlias, cast from typing import Literal as TypingLiteral -from pydantic import ConfigDict, Field, SerializeAsAny, model_validator +from pydantic import BeforeValidator, ConfigDict, Field, SerializeAsAny, model_validator from pydantic_core.core_schema import ValidatorFunctionWrapHandler from pyiceberg.expressions.literals import AboveMax, BelowMin, Literal, literal @@ -509,7 +509,7 @@ def as_unbound(self) -> type[UnboundPredicate]: ... class UnboundPredicate(Unbound, BooleanExpression, ABC): model_config = ConfigDict(arbitrary_types_allowed=True) - term: UnboundTerm + term: Annotated[str | UnboundTerm, BeforeValidator(_to_unbound_term)] def __init__(self, term: str | UnboundTerm, **kwargs: Any) -> None: super().__init__(term=_to_unbound_term(term), **kwargs) @@ -541,7 +541,7 @@ def __str__(self) -> str: return f"{str(self.__class__.__name__)}(term={str(self.term)})" def bind(self, schema: Schema, case_sensitive: bool = True) -> BoundUnaryPredicate: - bound_term = self.term.bind(schema, case_sensitive) + bound_term = cast(UnboundTerm, self.term).bind(schema, case_sensitive) bound_type = self.as_bound return bound_type(bound_term) # type: ignore[misc] @@ -709,7 +709,7 @@ def __init__( super().__init__(term=_to_unbound_term(term), values=literal_set) def bind(self, schema: Schema, case_sensitive: bool = True) -> BoundSetPredicate: - bound_term = self.term.bind(schema, case_sensitive) + bound_term = cast(UnboundTerm, self.term).bind(schema, case_sensitive) literal_set = self.literals return self.as_bound(bound_term, {lit.to(bound_term.ref().field.field_type) for lit in literal_set}) # type: ignore @@ -729,7 +729,7 @@ def __eq__(self, other: Any) -> bool: """Return the equality of two instances of the SetPredicate class.""" return self.term == other.term and self.literals == other.literals if isinstance(other, self.__class__) else False - def __getnewargs__(self) -> tuple[UnboundTerm, set[Any]]: + def __getnewargs__(self) -> tuple[str | UnboundTerm, set[Any]]: """Pickle the SetPredicate class.""" return (self.term, self.literals) @@ -893,7 +893,7 @@ def as_bound(self) -> type[BoundNotIn]: # type: ignore class LiteralPredicate(UnboundPredicate, ABC): type: TypingLiteral["lt", "lt-eq", "gt", "gt-eq", "eq", "not-eq", "starts-with", "not-starts-with"] = Field(alias="type") - term: UnboundTerm + term: Annotated[str | UnboundTerm, BeforeValidator(_to_unbound_term)] value: LiteralValue = Field() model_config = ConfigDict(populate_by_name=True, frozen=True, arbitrary_types_allowed=True) @@ -908,7 +908,7 @@ def literal(self) -> LiteralValue: return self.value def bind(self, schema: Schema, case_sensitive: bool = True) -> BoundLiteralPredicate: - bound_term = self.term.bind(schema, case_sensitive) + bound_term = cast(UnboundTerm, self.term).bind(schema, case_sensitive) lit = self.literal.to(bound_term.ref().field.field_type) if isinstance(lit, AboveMax): diff --git a/tests/expressions/test_expressions.py b/tests/expressions/test_expressions.py index ff0c5e80aa..18a03056b8 100644 --- a/tests/expressions/test_expressions.py +++ b/tests/expressions/test_expressions.py @@ -1397,6 +1397,11 @@ def test_deepcopy_then_pickle() -> None: assert restored == expr +def test_unbound_predicate_validates_a_string_term() -> None: + expr = EqualTo.model_validate({"term": "x", "value": 1}) + assert expr == EqualTo("x", 1) + + # __ __ ___ # | \/ |_ _| _ \_ _ # | |\/| | || | _/ || |