Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions pyiceberg/expressions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Expand Down Expand Up @@ -508,7 +508,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)]
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestion. I considered Annotated[UnboundTerm, BeforeValidator(_to_unbound_term)] first, but it does not fix the issue.

Without the pydantic mypy plugin (which this project does not use), mypy generates __init__ signatures from the outer Annotated type. So Annotated[UnboundTerm, ...] still produces:

error: Argument "term" has incompatible type "str"; expected "UnboundTerm"  [arg-type]

I verified this with a minimal repro — only str | UnboundTerm as the outer type makes mypy accept str in the constructor without the plugin. The type: ignore[union-attr] on the 3 bind() calls is the trade-off, but the validator guarantees the runtime type is always UnboundTerm.


def __init__(self, term: str | UnboundTerm, **kwargs: Any) -> None:
super().__init__(term=_to_unbound_term(term), **kwargs)
Expand Down Expand Up @@ -540,7 +540,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 = self.term.bind(schema, case_sensitive) # type: ignore[union-attr]
bound_type = self.as_bound
return bound_type(bound_term) # type: ignore[misc]

Expand Down Expand Up @@ -696,7 +696,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 = self.term.bind(schema, case_sensitive) # type: ignore[union-attr]
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

Expand All @@ -716,7 +716,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)

Expand Down Expand Up @@ -870,7 +870,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)

Expand All @@ -885,7 +885,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 = self.term.bind(schema, case_sensitive) # type: ignore[union-attr]
lit = self.literal.to(bound_term.ref().field.field_type)

if isinstance(lit, AboveMax):
Expand Down