Skip to content

Fix floor and ceil for non-finite Python floats - #383

Open
guocn wants to merge 1 commit into
jmespath:developfrom
guocn:fix/nonfinite-floor-ceil-381
Open

guocn wants to merge 1 commit into
jmespath:developfrom
guocn:fix/nonfinite-floor-ceil-381

Conversation

@guocn

@guocn guocn commented Oct 13, 2026 •

Copy link
Copy Markdown

Fixes #381.

Problem and change

Python's json.loads() accepts Infinity, -Infinity, and NaN by default. JMESPath classifies these Python floats as numbers, but math.floor() / math.ceil() cannot convert them to integers and raise bare OverflowError / ValueError during search().

Preserve non-finite float arguments unchanged in these two functions, one of the behaviors suggested in the issue. Finite floats still use the standard-library rounding behavior. The check is restricted to floats so arbitrary-size Python integers (e.g. 10 ** 400) do not undergo a float conversion and continue to work. Existing argument type validation is unchanged.

Tests

  • Before the implementation change, python -m pytest tests/test_functions.py -q: 1 failed, 7 passed, with the new regression failing on floor(Infinity) with OverflowError.
  • After: python -m pytest tests -q: 994 passed, 1 skipped, with 14 existing deprecated-string-literal warnings (Python 3.13.5).
  • Regression tests cover both functions with JSON-loaded positive/negative infinity and NaN, finite positive/negative floats, zero, integers including a very large integer, and rejection of booleans/null/strings/arrays/objects.
  • Focused Ruff correctness checks (E9,F63,F7,F82) and git diff --check pass.

Tests ran in an independent venv with an empty environment, automatic third-party pytest plugins disabled, and networking disabled through a network namespace. No production services were changed.

Scope / disclosure

This only defines behavior for Python's non-finite float extension; it does not change the JSON/JMESPath numeric specification, to_number(), or other numeric functions. There has not yet been maintainer confirmation of which proposed behavior is preferred; preserving non-finite values is the deliberately narrow choice here, and feedback is welcome.

Prepared with AI assistance. No independent human code review is claimed.

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.

floor() and ceil() raise OverflowError / ValueError on Infinity and NaN

1 participant