Skip to content

fix: allow for numeric coercion after python_evalish_coerce#489

Open
tristan-f-r wants to merge 1 commit into
Reed-CompBio:mainfrom
tristan-f-r:float-coercion
Open

fix: allow for numeric coercion after python_evalish_coerce#489
tristan-f-r wants to merge 1 commit into
Reed-CompBio:mainfrom
tristan-f-r:float-coercion

Conversation

@tristan-f-r

@tristan-f-r tristan-f-r commented Jun 14, 2026

Copy link
Copy Markdown
Collaborator

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 numeric strings. [Optimally, we would trigger pydantic's coercion logic ourselves, but it doesn't seem to expose a nice API for this.]

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.]
@tristan-f-r tristan-f-r added the bug Something isn't working label Jun 14, 2026
@tristan-f-r tristan-f-r changed the title fix: allow for coercions after python_evalish_coerce fix: allow for numeric coercion after python_evalish_coerce Jun 14, 2026
@agitter

agitter commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

I'll review and test this as a replacement for #488

Comment thread test/test_config.py

@ntalluri ntalluri Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reminder to add the test cases that tony had in #488

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants