Repository navigation
fix: raise NumberFormatError for invalid string in NumberPattern.apply - #1411
Open
2025sl93041-gkb wants to merge 2 commits into
Open
2025sl93041-gkb wants to merge 2 commits into
2025sl93041-gkb wants to merge 2 commits into
Conversation
Wrap `decimal.Decimal(str(value))` in `NumberPattern.apply()` and `_get_compact_format()` in a try/except to catch `decimal.InvalidOperation` and re-raise as `NumberFormatError`. Previously, passing an invalid string like `"not_a_number"` to any formatting function (format_decimal, format_currency, format_percent, format_compact_decimal) would propagate a raw `decimal.InvalidOperation` exception. The public API contract documents `NumberFormatError` as the error type for bad input; all other invalid-value paths already raise it. Closes python-babel#702
Author
|
Hi, following up on this PR. Let me know if there are any concerns with the fix or the test approach and I'll address them. |
chrikrah
suggested changes
Nov 10, 2026
chrikrah
left a comment
Contributor
There was a problem hiding this comment.
@2025sl93041-gkb except ArithmeticError stops catching this, which fits the Babel 3 milestone.
non-blocking: CHANGES.rst only changes in release-prep commits. Can the hunk go?
# git archive b9fe3a4 (head) and 9a3ef47 (base), Python 3.13, locale-data copied from the distro Babel 2.x install
$ python -c "from babel import numbers
try: numbers.format_decimal('abc', locale='en_US')
except ArithmeticError as e: print('caught', type(e).__name__)
except Exception as e: print('not caught', type(e).__name__)"
not caught NumberFormatError # base 9a3ef47: caught InvalidOperation
$ python -m pytest -q tests --ignore=tests/messages/test_setuptools_frontend.py
6 failed, 6711 passed, 1200 skipped, 1 xfailed # base 9a3ef47: 6 failed (same six, CLDR version), 6710 passed
Maintainers update CHANGES.rst only in release-prep commits.
This branch has not been deployed
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.
Summary
Closes #702.
When any formatting function (
format_decimal,format_currency,format_percent,format_compact_decimal, etc.) received an invalidstring value (e.g.
"not_a_number"), the internaldecimal.Decimal(str(value))call would raise a rawdecimal.InvalidOperation— a third-party implementation detail from thedecimalstandard library module — rather than Babel's ownNumberFormatError.This is a leaky abstraction: callers who want to catch bad-value errors
need to import and handle
decimal.InvalidOperationin addition to (orinstead of)
NumberFormatError.Root cause
NumberPattern.apply()(and the internal_get_compact_format()helper) both do:
decimal.Decimalraisesdecimal.InvalidOperationfor strings itcannot parse. No surrounding try/except was present.
Fix
Wrap the conversion in a try/except block and re-raise as
NumberFormatError:Both
NumberPattern.apply()and_get_compact_format()are fixed.Test
Added
test_number_pattern_apply_invalid_string_raises_number_format_errorin
tests/test_numbers.py. The test callsNumberPattern.apply()directly (via
parse_pattern('0.###')) with a mock locale so it workswithout requiring the full CLDR data build.