Fix float coercion for numeric filter parameters - #2876
Open
RamiNoodle733 wants to merge 3 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves Datasette’s numeric comparison filters so they correctly coerce filter parameter values beyond simple positive integers (e.g. floats, signed numbers, and exponent notation) before binding them as SQLite parameters—important for computed columns/views where SQLite column affinity may not help.
Changes:
- Add
_coerce_numeric_filter_value()helper to coercenumeric=Truefilter parameters tointorfloatwhen possible. - Update
TemplatedFilter.where_clause()to apply the new coercion for all numeric filters (instead of relying onstr.isdigit()). - Add a regression test covering floats, signed ints/floats, exponent notation, and non-numeric input.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| datasette/filters.py | Replaces isdigit()-only integer coercion with a helper that supports ints, floats, and exponent notation for numeric=True filters. |
| tests/test_numeric_filter_values.py | Adds focused regression coverage for numeric parameter coercion behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Fixes #1681.
Numeric comparison filters currently coerce parameters only when
str.isdigit()is true. That converts positive integers, but leaves values such as3.5,-2, and1e3as text. This matters in particular for computed columns and views where SQLite has no column affinity available to coerce the bound parameter.This change adds a small numeric coercion helper used by the existing
numeric=Truefilters. It preserves integer values as integers, falls back to floats for valid floating-point values, and leaves non-numeric input unchanged.A focused regression test covers floats, signed integers, signed floats, exponent notation, and non-numeric input.
Validation:
main; onlydatasette/filters.pyand the focused regression test are changed.AI assistance disclosure: this contribution was prepared using an AI coding assistant. The issue, current implementation, competing PRs, final diff, and repository state were inspected through GitHub; the assistant is responsible for the proposed change and has not claimed tests that were not executed.