fix: allow for numeric coercion after python_evalish_coerce#489
Open
tristan-f-r wants to merge 1 commit into
Open
fix: allow for numeric coercion after python_evalish_coerce#489tristan-f-r wants to merge 1 commit into
tristan-f-r wants to merge 1 commit into
Conversation
My assumption that BeforeValidators ran after numeric coercion was wrong. Since the only return types for `python_evalish_coerce` are from `is_numpy_friendly`, we can bake in a check for `python_evalish_coerce`'s guard statement that checks if the input is coercable to a float: this does not cover all of the behavior for Pydantic's coersion (https://pydantic.dev/docs/validation/latest/concepts/conversion_table), but it will at least allow pydantic to coerce strings. [Optimally, we would trigger pydantic's coercion logic ourselves, but it doesn't seem to expose a nice API for this.]
Collaborator
|
I'll review and test this as a replacement for #488 |
ntalluri
reviewed
Jun 15, 2026
Collaborator
There was a problem hiding this comment.
Reminder to add the test cases that tony had in #488
Collaborator
Author
There was a problem hiding this comment.
A few of these tests (link) would only serve to test Pydantic itself, but the tests which check for specific scientific notation would be good to ensure that the weak float test does capture all of the behavior we want.
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.
My assumption that BeforeValidators ran after numeric coercion was wrong.
Since the only return types for
python_evalish_coerceare fromis_numpy_friendly, we can bake in a check forpython_evalish_coerce's guard statement that checks if the input is coercable to a float: this does not cover all of the behavior for Pydantic's coersion (https://pydantic.dev/docs/validation/latest/concepts/conversion_table), but it will at least allow pydantic to coerce numeric strings. [Optimally, we would trigger pydantic's coercion logic ourselves, but it doesn't seem to expose a nice API for this.]