Skip to content

Fix compact decimal plural selection after rounding - #1430

Merged
akx merged 3 commits into
python-babel:masterfrom
0718lol:fix/compact-plural-after-rounding
Nov 24, 2026
Merged

akx merged 3 commits into
python-babel:masterfrom
0718lol:fix/compact-plural-after-rounding

Conversation

@0718lol

@0718lol 0718lol commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1429.

Select compact decimal plural forms using the rounded displayed value rather than the unrounded scaled value. Normalize optional fractional zeros before plural selection, since they are not displayed. For example, Russian 1100 now renders as 1 тысяча instead of 1 тысячи, and Polish 1900 renders as 2 tysiące instead of 2 tysiąca.

Also update the separate exact-1 pattern selection (if rounded == 1 and "1" in compact_format) per maintainer feedback so values that round to 1 match Node's Intl formatter (e.g., French 1234 -> mille).

Add 13 regression cases covering positive and negative values, retained fractions, trimmed zeros, and five locales. Update the existing Italian and French 1234 expectations in tests/test_numbers_format_decimal.py to mille.

Validation: CLDR 48.2 imported using the repository script; Python 3.12.15 on macOS; full suite passed except pre-existing test_input_paths_is_treated_as_list due to -- in local checkout directory name. Ruff and git diff --check pass. The new regression tests fail on the original implementation.

Developed and tested with Codex assistance.

Comment thread babel/numbers.py Resolved
@akx akx self-assigned this Nov 4, 2026
@akx akx added this to the Babel 2.19 milestone Nov 4, 2026
@0718lol

0718lol commented Nov 4, 2026 via email

Copy link
Copy Markdown
Contributor Author

@akx

akx commented Nov 5, 2026

Copy link
Copy Markdown
Member

@0718lol Sorry, I can't read that (without Google Translate anyway). Are you able to enact the changes requested?

As requested in review: the special case should test the rounded display
value instead of the scaled input, so e.g. fr 1234 with fraction_digits=0
formats as 'mille' (matching Node ICU), while fr -1234 keeps '-1 millier'.

Adds the two French cases to test_compact_plural_uses_displayed_number and
updates the matching expectation in test_compact.
@0718lol

0718lol commented Nov 8, 2026

Copy link
Copy Markdown
Contributor Author

Done, pushed as f6411c5:

  • babel/numbers.py: the special case now tests the rounded display value (if rounded == 1 ...), exactly as suggested.
  • Added ('fr', 1234, 0, 'mille') and ('fr', -1234, 0, '-1 millier') to test_compact_plural_uses_displayed_number — both behave as you described.
  • Updated the pre-existing expectation in tests/test_numbers_format_decimal.py::test_compact (fr 1234 is now 'mille', matching Node ICU and the Italian case right above it).

Verification: the two new cases fail before the change and pass after. Full tests/ run: 6689 passed, 1180 skipped, 1 xfailed, and one failure in test_extract.py::test_input_paths_is_treated_as_list that I verified also fails on the unmodified branch HEAD locally (it is triggered by my checkout directory name containing --, not by this patch).

Sorry for the earlier reply in Chinese. This update was prepared with AI assistance under my direction, as disclosed in the PR description.

@akx
akx requested review from akx and a balanced review from Copilot and removed request for akx November 9, 2026 09:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The exact-1 pattern now applies to scaled values that round to 1, contradicting the description and requiring either a description update or restoration of the exact comparison.

1 open issue
What changed in this PR

Updates Babel’s compact decimal formatting to select plural forms from the rounded displayed value, addressing #1429.

Changes:

  • Uses rounded values with optional trailing zeros removed for plural selection.
  • Extends literal exact-1 pattern selection to values that round to 1.
  • Adds 13 regression cases across five locales and updates Italian and French expectations.
File Description
tests/​test_numbers.py Adds coverage for rounding, signs, retained fractions, and trimmed zeros.
tests/​test_numbers_format_decimal.py Updates Italian and French compact-format expectations.
babel/​numbers.py Uses normalized rounded values for plural rules and rounded values for exact-1 matching.

Review effort: Balanced

Comment thread babel/numbers.py Resolved
@0718lol

0718lol commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Hi @akx, I've also updated the PR description to document the rounded == 1 change. Whenever you have a moment, could you please approve the workflow run for f6411c5 and take another look? Thanks!

Otherwise e.g. French 1_100_000 fell back to "other" ("1 millions") instead of "one" ("1 million").

@akx akx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed another commit to fix one more corner case. Thanks.

@akx
akx enabled auto-merge November 24, 2026 19:15
@codecov

codecov Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.69%. Comparing base (63c168e) to head (6f66bd0).
⚠️ Report is 41 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1430      +/-   ##
==========================================
+ Coverage   93.66%   93.69%   +0.03%     
==========================================
  Files          27       27              
  Lines        4971     5047      +76     
==========================================
+ Hits         4656     4729      +73     
- Misses        315      318       +3     
Flag Coverage Δ
macos-14-3.10 ?
macos-14-3.11 ?
macos-14-3.12 ?
macos-14-3.13 ?
macos-14-3.14 ?
macos-14-3.15 ?
macos-14-3.8 ?
macos-14-3.9 ?
macos-14-pypy3.10 ?
macos-26-3.10 92.78% <100.00%> (?)
macos-26-3.11 92.72% <100.00%> (?)
macos-26-3.12 92.98% <100.00%> (?)
macos-26-3.13 92.98% <100.00%> (?)
macos-26-3.14 92.96% <100.00%> (?)
macos-26-3.15 92.96% <100.00%> (?)
macos-26-3.9 92.72% <100.00%> (?)
macos-26-pypy3.11 92.72% <100.00%> (?)
macos-26-pypy3.12 92.98% <100.00%> (?)
ubuntu-24.04-3.10 92.80% <100.00%> (+0.08%) ⬆️
ubuntu-24.04-3.11 92.74% <100.00%> (+0.09%) ⬆️
ubuntu-24.04-3.12 93.00% <100.00%> (+0.08%) ⬆️
ubuntu-24.04-3.13 93.00% <100.00%> (+0.08%) ⬆️
ubuntu-24.04-3.14 92.98% <100.00%> (+0.08%) ⬆️
ubuntu-24.04-3.15 92.98% <100.00%> (+0.08%) ⬆️
ubuntu-24.04-3.8 ?
ubuntu-24.04-3.9 92.74% <100.00%> (+0.09%) ⬆️
ubuntu-24.04-pypy3.10 ?
ubuntu-24.04-pypy3.11 92.74% <100.00%> (?)
ubuntu-24.04-pypy3.12 93.00% <100.00%> (?)
windows-2022-3.10 ?
windows-2022-3.11 ?
windows-2022-3.12 ?
windows-2022-3.13 ?
windows-2022-3.14 ?
windows-2022-3.15 ?
windows-2022-3.8 ?
windows-2022-3.9 ?
windows-2022-pypy3.10 ?
windows-2025-3.10 92.80% <100.00%> (?)
windows-2025-3.11 92.74% <100.00%> (?)
windows-2025-3.12 92.99% <100.00%> (?)
windows-2025-3.13 92.99% <100.00%> (?)
windows-2025-3.14 92.97% <100.00%> (?)
windows-2025-3.15 92.97% <100.00%> (?)
windows-2025-3.9 92.73% <100.00%> (?)
windows-2025-pypy3.11 92.74% <100.00%> (?)
windows-2025-pypy3.12 92.99% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@codspeed

codspeed Bot commented Nov 24, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing 0718lol:fix/compact-plural-after-rounding (6f66bd0) with master (1a0763a)

Open in CodSpeed

@akx
akx disabled auto-merge November 24, 2026 19:24
@akx
akx merged commit fcead2c into python-babel:master Nov 24, 2026
36 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compact decimal plural forms use the value before rounding

3 participants