Skip to content

Respect unit-name boundaries when selecting unit patterns - #1453

Open
liugedragon wants to merge 1 commit into
python-babel:masterfrom
liugedragon:fix/unit-identifier-boundaries
Open

liugedragon wants to merge 1 commit into
python-babel:masterfrom
liugedragon:fix/unit-identifier-boundaries

Conversation

@liugedragon

Copy link
Copy Markdown

format_compound_unit(5, "meter", denominator_unit="hour", locale="en") currently returns 5 kilometers per hour. The suffix lookup matches meter-per-hour inside speed-kilometer-per-hour, so a label can silently use a different unit without converting its value.

Require a unit-name boundary before a shortened suffix. Exact qualified IDs and deprecated-ID normalization keep their existing paths. An unavailable predefined combination now falls back to the existing per-unit formatter:

from babel.units import format_compound_unit

format_compound_unit(5, "meter", denominator_unit="hour", locale="en")
# Before: '5 kilometers per hour'
# After:  '5 meters per hour'

Also retain an unresolved denominator's original name. With a numeric denominator of 1, an unknown unit previously became None, then an empty suffix, and could format as an unrelated unit. It now raises UnknownUnitError with the supplied name, consistently with other numeric denominator values. Preformatted string denominators continue to ignore their unit.

Validation on Windows / Python 3.13.4 with CLDR 49:

  • Unit suite with 102 new cases: unchanged upstream cb0c7a7 has 57 failed, 75 passed; patched suite has 132 passed.
  • Unit suite and module examples: 142 passed.
  • python -m pytest --doctest-modules babel tests -q: 8,483 passed, 7 skipped, 2 xfailed.
  • Pinned Ruff 0.14.10 and git diff --check: passed.
  • Independent old/new sweep across 14 locales: 6,664 lookups of actual qualified and unqualified unit identifiers are unchanged. Among 3,150 ordinary compound combinations, only the 14 meter/hour false matches change, one per locale.

This does not tighten every possible shorthand into strict identifier validation or change unit data, plural selection, or number formatting.

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.

1 participant