aashish-g03 opened a new pull request, #3421:
URL: https://github.com/apache/iceberg-python/pull/3421

   # Rationale for this change
   
   Without the pydantic mypy plugin, mypy generates `__init__` signatures from 
field declarations rather than from the explicit `__init__` overrides on 
`UnboundPredicate` and its subclasses. This causes downstream users to get type 
errors when constructing predicates with string column names:
   
   ```python
   from pyiceberg.expressions import EqualTo
   
   expr = EqualTo(term="my_column", value=42)
   # mypy error: Argument "term" has incompatible type "str"; expected 
"UnboundTerm"
   ```
   
   Closes #3101.
   
   # What changes were included in this PR?
   
   Widen the `term` field declaration from `UnboundTerm` to `Annotated[str | 
UnboundTerm, BeforeValidator(_to_unbound_term)]` in `UnboundPredicate` and 
`LiteralPredicate`. This uses the same `Annotated` + `BeforeValidator` pattern 
already established in `partitioning.py` for `transform` field coercion.
   
   The `BeforeValidator` calls the existing `_to_unbound_term` helper, which 
coerces `str` to `Reference`. The stored value is always `UnboundTerm` at 
runtime.
   
   Changes:
   - `UnboundPredicate.term`: `UnboundTerm` -> `Annotated[str | UnboundTerm, 
BeforeValidator(_to_unbound_term)]`
   - `LiteralPredicate.term`: same widening (re-declares the field)
   - Added `# type: ignore[union-attr]` on 3 `bind()` call sites where 
`self.term.bind()` is called -- the validator guarantees the runtime type, but 
mypy sees the union
   - All existing `__init__` methods are preserved unchanged (they handle more 
than just term coercion)
   
   # How was this patch tested?
   
   All 757 existing expression and conversion tests pass. Verified with mypy 
that keyword construction `EqualTo(term="col", value=42)` no longer produces 
the `arg-type` error on `term`.
   
   **Note:** Positional construction `EqualTo("col", 42)` still produces mypy 
errors (`Too many positional arguments`) because mypy without the pydantic 
plugin cannot infer positional signatures from explicit `__init__` overrides. 
That is a broader issue affecting all Pydantic model constructors in the 
codebase and is outside the scope of this fix.


-- 
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