Skip to content

Fix float coercion for numeric filter parameters - #2876

Open
RamiNoodle733 wants to merge 3 commits into
simonw:mainfrom
RamiNoodle733:agent/fix-numeric-filter-floats-1681
Open

Fix float coercion for numeric filter parameters#2876
RamiNoodle733 wants to merge 3 commits into
simonw:mainfrom
RamiNoodle733:agent/fix-numeric-filter-floats-1681

Conversation

@RamiNoodle733

Copy link
Copy Markdown

Fixes #1681.

Numeric comparison filters currently coerce parameters only when str.isdigit() is true. That converts positive integers, but leaves values such as 3.5, -2, and 1e3 as 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=True filters. 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:

  • Reviewed the final branch diff against current main; only datasette/filters.py and the focused regression test are changed.
  • Full project tests were not run locally because this automation environment does not have a Datasette checkout/development dependencies and outbound Git access is unavailable. The added test is intended to run in the repository CI.

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.

Copilot AI lite review requested due to automatic review settings August 11, 2026 12:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 coerce numeric=True filter parameters to int or float when possible.
  • Update TemplatedFilter.where_clause() to apply the new coercion for all numeric filters (instead of relying on str.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.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Potential bug in numeric handling where_clause for filters

2 participants