Skip to content

fix: DecimalField no longer raises InvalidOperation for values over 28 digits - #2349

Merged
waketzheng merged 6 commits into
tortoise:developfrom
moneebullah25:fix/decimalfield-high-precision
Oct 19, 2026
Merged

waketzheng merged 6 commits into
tortoise:developfrom
moneebullah25:fix/decimalfield-high-precision

Conversation

@moneebullah25

Copy link
Copy Markdown
Contributor

Description

DecimalField.to_python_value quantized with the active decimal context, whose default precision is 28 significant digits. A value with more digits than that (for example a max_digits=40 field) raised decimal.InvalidOperation. The quantize now runs in a copy of the active context with its precision raised to at least max_digits, so the caller's rounding mode and traps are kept and values that really exceed max_digits still raise.

Motivation and Context

Fixes #2347

How Has This Been Tested?

  • TORTOISE_TEST_DB=sqlite://:memory: .venv/bin/python -m pytest tests/fields/test_decimal.py (26 passed)
  • Added tests for high-precision values, the 28/30 digit boundaries (including overflow still raising) and for keeping the context rounding mode.
  • ruff format --check and ruff check (0.15.9) on the changed files, codespell and mypy tortoise/fields/data.py tests/fields/test_decimal.py all pass.
  • Only the sqlite in-memory run was done; other databases were not run.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed

codspeed Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

⚠️ 21 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 24 untouched benchmarks


Comparing moneebullah25:fix/decimalfield-high-precision (809a507) with develop (1d8df1e)

Open in CodSpeed

@waketzheng
waketzheng merged commit a45ce6a into tortoise:develop Oct 19, 2026
25 checks passed
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.

DecimalField.to_python_value raises decimal.InvalidOperation for values with more than 28 significant digits (e.g. DecimalField(40, 10))

2 participants