Skip to content

Respect preformatted string numerators in compound-unit formatting - #1399

Open
nawaaaaaAaar wants to merge 1 commit into
python-babel:masterfrom
nawaaaaaAaar:fix/preformatted-compound-numerator
Open

nawaaaaaAaar wants to merge 1 commit into
python-babel:masterfrom
nawaaaaaAaar:fix/preformatted-compound-numerator

Conversation

@nawaaaaaAaar

@nawaaaaaAaar nawaaaaaAaar commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #1398.

The compound-unit fast path formats "1 kg" plus numerator_unit="kilometer" as "1 kg kilometer per hour", even though the documented string-value contract says to ignore the corresponding unit. This can corrupt display labels when a caller accepts either numeric quantities or already-localized strings alongside unit metadata.

Exclude preformatted string numerators from that numeric fast path. The existing general path already handles them correctly; numeric inputs keep the existing optimization and formatting.

Reproduction and regression evidence

Unchanged upstream: e2a17919f0eb988bfea2883b00793ca4d5d37c4f.

from babel.units import format_compound_unit
format_compound_unit("1 kg", "kilometer", denominator_unit="hour", locale="en")
# Before: '1 kg kilometer per hour'
# After:  '1 kg per hour'

The final 18 regression cases fail before the production change and pass afterward. They compare the explicitly documented ignored-unit behavior for two preformatted labels across English, Finnish and French, at all three display lengths.

Verification

  • CLDR 48.2 downloaded, SHA-512 verified and imported using the repository's own scripts/download_import_cldr.py.
  • Full suite on Python 3.12.13: 7,848 passed, eight skipped, two expected failures.
  • pytest -q --doctest-modules babel/units.py tests/test_units.py: 53 passed.
  • ruff check . and git diff --check: passed.
  • Source distribution and wheel built successfully using python -m build --no-isolation.
  • Local checks are Linux-only; this does not claim hosted CI or every supported runtime passed.

Assistance and scope

Prepared with AI assistance for investigation, implementation and test execution. No personal review, provenance attestation or maintainer approval by the account owner is asserted. This is a focused behavioral correction, not a change to unit data or plural rules.

Combined verification

Tested together with the independent fractional-duration fix in #1401: 7,893 passed, seven skipped, two expected failures, with one existing pytest deprecation warning, on Python 3.12.13. Full Ruff checks pass. This combined test used a separate local worktree; this PR's branch and scope remain unchanged.

Copilot AI balanced review requested due to automatic review settings October 22, 2026 12:10

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@akx
akx force-pushed the fix/preformatted-compound-numerator branch from ce706dd to 745fc45 Compare October 30, 2026 11:26
@codecov

codecov Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.56%. Comparing base (419a228) to head (745fc45).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1399   +/-   ##
=======================================
  Coverage   93.56%   93.56%           
=======================================
  Files          27       27           
  Lines        4939     4939           
=======================================
  Hits         4621     4621           
  Misses        318      318           
Flag Coverage Δ
macos-14-3.10 92.65% <100.00%> (ø)
macos-14-3.11 92.58% <100.00%> (ø)
macos-14-3.12 92.79% <100.00%> (ø)
macos-14-3.13 92.79% <100.00%> (ø)
macos-14-3.14 92.77% <100.00%> (ø)
macos-14-3.15 92.77% <100.00%> (ø)
macos-14-3.8 92.52% <100.00%> (ø)
macos-14-3.9 92.58% <100.00%> (ø)
macos-14-pypy3.10 92.65% <100.00%> (ø)
ubuntu-24.04-3.10 92.67% <100.00%> (ø)
ubuntu-24.04-3.11 92.60% <100.00%> (ø)
ubuntu-24.04-3.12 92.81% <100.00%> (ø)
ubuntu-24.04-3.13 92.81% <100.00%> (ø)
ubuntu-24.04-3.14 92.79% <100.00%> (ø)
ubuntu-24.04-3.15 92.79% <100.00%> (ø)
ubuntu-24.04-3.8 92.54% <100.00%> (ø)
ubuntu-24.04-3.9 92.60% <100.00%> (ø)
ubuntu-24.04-pypy3.10 92.67% <100.00%> (ø)
windows-2022-3.10 92.66% <100.00%> (ø)
windows-2022-3.11 92.60% <100.00%> (ø)
windows-2022-3.12 92.80% <100.00%> (ø)
windows-2022-3.13 92.80% <100.00%> (ø)
windows-2022-3.14 92.78% <100.00%> (ø)
windows-2022-3.15 92.78% <100.00%> (ø)
windows-2022-3.8 92.63% <100.00%> (ø)
windows-2022-3.9 92.59% <100.00%> (ø)
windows-2022-pypy3.10 92.66% <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 Oct 30, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 88 untouched benchmarks


Comparing nawaaaaaAaar:fix/preformatted-compound-numerator (745fc45) with master (419a228)

Open in CodSpeed

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

format_compound_unit adds an extra unit to preformatted string numerators

2 participants