Repository navigation
Widen expression term fields to accept strings - #4549
Open
Anurag-S1ngh wants to merge 2 commits into
Open
Anurag-S1ngh wants to merge 2 commits into
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
Contributor
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).
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.