fix(es,ca): guard to_currency() cents-part for integer inputs#659
Open
fbindakheel wants to merge 1 commit into
Open
fix(es,ca): guard to_currency() cents-part for integer inputs#659fbindakheel wants to merge 1 commit into
fbindakheel wants to merge 1 commit into
Conversation
Integer inputs with no cents cause the base to_currency() to omit the cents segment, leaving list_result with a single element. The ES and CA overrides unconditionally accessed list_result[1], raising IndexError for any whole-unit amount (e.g. num2words(100, lang='es', to='currency')). Guard the cents-part processing with len(list_result) > 1. Add regression tests for both languages.
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.
Bug
to_currency()crashes withIndexErrorfor Spanish (es) and Catalan (ca) when the input is an integer that resolves to a whole currency unit (zero cents). For example:This affects all whole-unit integer inputs —
0,100(1 euro),200,2100, etc.Root cause
Both
lang_ES.pyandlang_CA.pyoverrideto_currency()by calling the base implementation, then splitting the result on the separator (con/amb) to get[dollars_part, cents_part]. They then unconditionally apply grammar fixes tolist_result[1].However, the base
to_currency()omits the cents segment entirely when the input has no cents (i.e. integer input whereright == 0andhas_decimalisFalse). This leaveslist_resultwith only one element, solist_result[1]raisesIndexError.The existing tests only used float inputs like
1.00,21.00— floats always produce a cents segment (con cero céntimos), so the integer path was never exercised.Fix
Guard the cents-part post-processing with
if len(list_result) > 1:in both files. The dollars-part handling is unaffected.Tests
Added regression tests in
tests/test_es.pyandtests/test_ca.pycovering integer whole-unit amounts and feminine currencies. Verified the new tests fail on the original code and pass with the fix. All 1495 tests pass.