Skip to content

Normalize negative inputs in generated JavaScript plural rules - #1414

Merged
akx merged 2 commits into
python-babel:masterfrom
parijat1222q:fix-javascript-negative-plural
Nov 2, 2026
Merged

akx merged 2 commits into
python-babel:masterfrom
parijat1222q:fix-javascript-negative-plural

Conversation

@parijat1222q

Copy link
Copy Markdown
Contributor

Normalize n with Math.abs once before evaluating generated JavaScript plural rules. This matches Python's extract_operands and the Unicode definition of plural operands as absolute values.

For {'one': 'n is 1', 'few': 'n in 2..4'}, Python selects one for -1 and few for -2, while the current generated JavaScript selects other for both.

Update the generated-source expectations and add always-on normalization checks for empty, direct, integer, modulo and range rules. Four execution tests compare the generated function with Python for negative integers/fractions, zero and positive controls. They use Node.js when available and skip only those execution tests otherwise, without adding a required dependency. All four execution regressions fail on unchanged master.

Validation against 9a3ef47, Linux, imported CLDR 48.2:

  • All 19 configured Tox environments pass: CPython 3.8-3.15, PyPy 3.10, and the listed pytz/setuptools/Jinja variants, including coverage. CPython 3.15: 7,924 passed, 7 skipped, 2 expected failures. PyPy: 7,923 passed, 7 skipped, 3 expected failures.
  • Plural tests: 47 passed, including the Node.js 22.23.3 execution regressions.
  • Configured, pinned pre-commit hooks on all files, source/wheel build, and git diff --check pass.
  • Separate generated-JavaScript/Python comparison: 29,795 integer checks across 59 distinct cardinal/ordinal rules pass. Rules referencing existing unsupported compact-exponent operands were excluded from that extra comparison; they are not changed here.

Test runs used process-local TZ=UTC: two unrelated host-timezone assertions fail with Asia/Calcutta on both unchanged master and this branch. No tests were deselected. The existing pytest parametrization warning remains. Windows/macOS were not run locally; upstream CI is still pending.

Fixes #1413

Use absolute values before evaluating generated plural rules, matching
Python and the Unicode plural operand definition. Cover source output
and JavaScript execution for negative and positive inputs.

Fixes python-babel#1413

@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.

The fix itself is fine. Just get rid of the unnecessary tests, please.

Comment thread tests/test_plural.py Resolved
Comment thread tests/test_plural.py Resolved
@parijat1222q
parijat1222q requested a review from akx November 2, 2026 08:30
@akx
akx enabled auto-merge (squash) November 2, 2026 10:26
@codecov

codecov Bot commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.56%. Comparing base (9a3ef47) to head (0c92865).
⚠️ Report is 14 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1414      +/-   ##
==========================================
+ Coverage   93.51%   93.56%   +0.05%     
==========================================
  Files          27       27              
  Lines        4932     4943      +11     
==========================================
+ Hits         4612     4625      +13     
+ Misses        320      318       -2     
Flag Coverage Δ
macos-14-3.10 92.65% <100.00%> (+0.05%) ⬆️
macos-14-3.11 92.59% <100.00%> (+0.05%) ⬆️
macos-14-3.12 92.79% <100.00%> (+0.05%) ⬆️
macos-14-3.13 92.79% <100.00%> (+0.05%) ⬆️
macos-14-3.14 92.77% <100.00%> (+0.05%) ⬆️
macos-14-3.15 92.77% <100.00%> (+0.05%) ⬆️
macos-14-3.8 92.53% <100.00%> (+0.05%) ⬆️
macos-14-3.9 92.59% <100.00%> (+0.05%) ⬆️
macos-14-pypy3.10 92.65% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.10 92.67% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.11 92.61% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.12 92.81% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.13 92.81% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.14 92.79% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.15 92.79% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.8 92.55% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-3.9 92.61% <100.00%> (+0.05%) ⬆️
ubuntu-24.04-pypy3.10 92.67% <100.00%> (+0.05%) ⬆️
windows-2022-3.10 92.66% <100.00%> (+0.05%) ⬆️
windows-2022-3.11 92.60% <100.00%> (+0.05%) ⬆️
windows-2022-3.12 92.81% <100.00%> (+0.05%) ⬆️
windows-2022-3.13 92.81% <100.00%> (+0.05%) ⬆️
windows-2022-3.14 92.79% <100.00%> (+0.05%) ⬆️
windows-2022-3.15 92.79% <100.00%> (+0.05%) ⬆️
windows-2022-3.8 92.64% <100.00%> (+0.05%) ⬆️
windows-2022-3.9 92.60% <100.00%> (+0.05%) ⬆️
windows-2022-pypy3.10 92.66% <100.00%> (+0.05%) ⬆️

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 2, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing parijat1222q:fix-javascript-negative-plural (0c92865) with master (8517068)

Open in CodSpeed

@akx
akx merged commit add1bc0 into python-babel:master Nov 2, 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.

Generated JavaScript plural rules misclassify negative counts

2 participants