Repository navigation
fix: raise NumberFormatError for invalid string in NumberPattern.apply - #1411
Open
2025sl93041-gkb wants to merge 1 commit into
Open
2025sl93041-gkb wants to merge 1 commit into
2025sl93041-gkb wants to merge 1 commit 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
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.