jaimeferj commented on code in PR #2561:
URL: https://github.com/apache/iceberg-python/pull/2561#discussion_r2440322692


##########
pyiceberg/expressions/__init__.py:
##########
@@ -743,12 +750,52 @@ def as_bound(self) -> Type[BoundNotIn[L]]:
         return BoundNotIn[L]
 
 
-class LiteralPredicate(UnboundPredicate[L], ABC):
-    literal: Literal[L]
+class LiteralPredicate(IcebergBaseModel, UnboundPredicate[L], ABC):
+    type: TypingLiteral["lt", "lt-eq", "gt", "gt-eq", "eq", "not-eq", 
"starts-with", "not-starts-with"] = Field(alias="type")
+    term: UnboundTerm[Any]
+    value: Literal[L] = Field(alias="literal", serialization_alias="value")
+
+    model_config = ConfigDict(populate_by_name=True, frozen=True, 
arbitrary_types_allowed=True)
+
+    def __init__(
+        self,
+        term: Union[str, UnboundTerm[Any], BoundReference[Any]],
+        literal: Union[L, Literal[L], None] = None,
+        **data: Any,
+    ) -> None:  # pylint: disable=W0621
+        extra = dict(data)
+
+        literal_candidates = []
+        if literal is not None:
+            literal_candidates.append(literal)
+        if "literal" in extra:
+            literal_candidates.append(extra.pop("literal"))
+        if "value" in extra:
+            literal_candidates.append(extra.pop("value"))
+
+        literal_candidates = [candidate for candidate in literal_candidates if 
candidate is not None]
+
+        if not literal_candidates:
+            raise TypeError("LiteralPredicate requires a literal or value 
argument")
+        if len(literal_candidates) > 1:
+            raise TypeError("literal/value provided multiple times")
+
+        init = cast("Callable[..., None]", IcebergBaseModel.__init__)
+        init(self, term=_to_unbound_term(term), 
literal=_to_literal(literal_candidates[0]), **extra)
+
+    @field_validator("term", mode="before")
+    @classmethod
+    def _convert_term(cls, value: Any) -> UnboundTerm[Any]:
+        return _to_unbound_term(value)
+
+    @field_validator("value", mode="before")
+    @classmethod
+    def _convert_value(cls, value: Any) -> Literal[Any]:
+        return _to_literal(value)

Review Comment:
   This error im not sure how to tackle it: 
   ```
     tests/expressions/test_expressions.py:433: error: Argument "term" to 
"NotEqualTo" has incompatible type "BoundReference[str]"; expected "str | 
UnboundTerm[Any]"  [arg-type]```
   
   The test errors can be easily fixed by:
   
   ```suggestion
       value: Literal[L] = Field()
       model_config = ConfigDict(populate_by_name=True, frozen=True, 
arbitrary_types_allowed=True)
   
       def __init__(self, term: Union[str, UnboundTerm[Any], 
BoundReference[Any]], literal: Union[L, Literal[L]]):  # pylint: disable=W0621
           super().__init__(term=_to_unbound_term(term), 
value=_to_literal(literal))  # pylint: disable=W0621
   ```



##########
pyiceberg/expressions/__init__.py:
##########
@@ -743,12 +750,52 @@ def as_bound(self) -> Type[BoundNotIn[L]]:
         return BoundNotIn[L]
 
 
-class LiteralPredicate(UnboundPredicate[L], ABC):
-    literal: Literal[L]
+class LiteralPredicate(IcebergBaseModel, UnboundPredicate[L], ABC):
+    type: TypingLiteral["lt", "lt-eq", "gt", "gt-eq", "eq", "not-eq", 
"starts-with", "not-starts-with"] = Field(alias="type")
+    term: UnboundTerm[Any]
+    value: Literal[L] = Field(alias="literal", serialization_alias="value")
+
+    model_config = ConfigDict(populate_by_name=True, frozen=True, 
arbitrary_types_allowed=True)
+
+    def __init__(
+        self,
+        term: Union[str, UnboundTerm[Any], BoundReference[Any]],
+        literal: Union[L, Literal[L], None] = None,
+        **data: Any,
+    ) -> None:  # pylint: disable=W0621
+        extra = dict(data)
+
+        literal_candidates = []
+        if literal is not None:
+            literal_candidates.append(literal)
+        if "literal" in extra:
+            literal_candidates.append(extra.pop("literal"))
+        if "value" in extra:
+            literal_candidates.append(extra.pop("value"))
+
+        literal_candidates = [candidate for candidate in literal_candidates if 
candidate is not None]
+
+        if not literal_candidates:
+            raise TypeError("LiteralPredicate requires a literal or value 
argument")
+        if len(literal_candidates) > 1:
+            raise TypeError("literal/value provided multiple times")
+
+        init = cast("Callable[..., None]", IcebergBaseModel.__init__)
+        init(self, term=_to_unbound_term(term), 
literal=_to_literal(literal_candidates[0]), **extra)
+
+    @field_validator("term", mode="before")
+    @classmethod
+    def _convert_term(cls, value: Any) -> UnboundTerm[Any]:
+        return _to_unbound_term(value)
+
+    @field_validator("value", mode="before")
+    @classmethod
+    def _convert_value(cls, value: Any) -> Literal[Any]:
+        return _to_literal(value)

Review Comment:
   This error im not sure how to tackle it: 
   ```
     tests/expressions/test_expressions.py:433: error: Argument "term" to 
"NotEqualTo" has incompatible type "BoundReference[str]"; expected "str | 
UnboundTerm[Any]"  [arg-type]
   ```
   
   The test errors can be easily fixed by:
   
   ```suggestion
       value: Literal[L] = Field()
       model_config = ConfigDict(populate_by_name=True, frozen=True, 
arbitrary_types_allowed=True)
   
       def __init__(self, term: Union[str, UnboundTerm[Any], 
BoundReference[Any]], literal: Union[L, Literal[L]]):  # pylint: disable=W0621
           super().__init__(term=_to_unbound_term(term), 
value=_to_literal(literal))  # pylint: disable=W0621
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to