Repository navigation
Widen expression term fields to accept strings - #4549
Anurag-S1ngh wants to merge 2 commits into
Conversation
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.
rahulsmahadev
left a comment
There was a problem hiding this comment.
Thanks @Anurag-S1ngh! Valid predicates construct, validate, dump, pickle and serialize to the same REST plan-request JSON as on main.
It looks like #3965 already fixed #3101 on main by giving each predicate class an explicit __init__: with mypy 1.19.1 and no pydantic plugin, the issue's EqualTo("my_column", 42) passes on main and fails on 0.12.0, which predates #3965. So one option would be to close #3101 instead, since widening the field makes readers see str | UnboundTerm. This passes on main and fails here:
def bind_term(pred: EqualTo, schema: Schema) -> None:
pred.term.bind(schema) # Item "str" of "str | UnboundTerm" has no attribute "bind" [union-attr]That's also why the three cast()s are needed, and the two new self.term.bind(...) calls in #4513 fail mypy once both are merged.
A smaller suggestion: the new test passes on main too (inline).
| assert restored == expr | ||
|
|
||
|
|
||
| def test_unbound_predicate_validates_a_string_term() -> None: |
There was a problem hiding this comment.
This passes on main too, where model_validate already turns "x" into a Reference, and it still passes with either BeforeValidator removed.
There was a problem hiding this comment.
You are right, Reference is a pydantic root model, so model_validate already coerced the string on main. Removed the test since it does not cover the typing change; the change itself is the widened field annotation, verified with mypy on a string-term snippet.
|
Checked current main with mypy 1.19.1, pydantic 2.14.4, and the pydantic plugin disabled. EqualTo, In, and IsNull with string terms all type-check, and pred.term.bind(schema) works, so the issue does not reproduce after #3965. Widening term to str | UnboundTerm causes readers to hit union-attr on .bind(), which is why the casts were needed, and the new .bind() calls in #4513 would fail. Closing this PR as superseded by #3965. #3101 can be closed as fixed by #3965. |
Closes #3101
Rationale for this change
mypy generates constructor signatures from pydantic field annotations, not from the explicit
__init__overrides.UnboundPredicateandLiteralPredicatedeclaredterm: UnboundTerm, soEqualTo("col", value)failed witharg-typeunless the pydantic mypy plugin was enabled.Widen the field to
str | UnboundTermand normalize strings withBeforeValidator. Binding casts the validated field back toUnboundTerm, andSetPredicate.__getnewargs__follows the widened type.Are these changes tested?
pytest tests/expressions(619 passed)EqualTo,In, andIsNullwith string terms, without the pydantic pluginruff checkandruff format --checkon the changed filesAre there any user-facing changes?
No. This is a typing compatibility fix; runtime behavior is unchanged.