Add support for other systemic aliases specified in CLDR root.xml - #1310
Conversation
Add support for other systemic aliases specified in CLDR root.xmlroot.xml
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1310 +/- ##
==========================================
+ Coverage 92.19% 92.20% +0.01%
==========================================
Files 27 27
Lines 4764 4761 -3
==========================================
- Hits 4392 4390 -2
+ Misses 372 371 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR extends Babel’s CLDR import and locale-data resolution to ingest additional systemic <alias> rules from CLDR root.xml, so missing length variants (units, date fields, compact decimals, currency formats) resolve via aliases rather than ad-hoc fallbacks—eliminating some rare KeyError crashes and improving formatting in edge cases.
Changes:
- Import and materialize additional CLDR
root.xmlalias relationships duringscripts/import_cldr.pyingestion (decimal formats, unit lengths/unit aliases, date field aliases). - Enhance locale-data alias resolution/merging in
babel/localedata.pyto support “alias + dict overrides” safely. - Simplify unit and timedelta formatting lookups to rely on alias-resolving locale data instead of hard-coded fallbacks, with new regression tests covering the reported cases.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| scripts/import_cldr.py | Imports additional CLDR root.xml aliases (decimal formats, unit patterns/display names, date fields) into generated locale data. |
| babel/localedata.py | Extends merge + alias resolution to support dict overrides layered on top of aliases. |
| babel/units.py | Switches format_unit to rely on alias-resolving locale data for length fallback/alias behavior. |
| babel/dates.py | Switches format_timedelta unit/date-field lookup to rely on alias-resolving locale data and avoids a direct KeyError path. |
| tests/test_units.py | Adds regression tests for unit aliases, length aliases, and person-variant alias behavior. |
| tests/test_numbers.py | Adds regression tests for compact decimal and currency format aliasing. |
| tests/test_dates.py | Adds regression tests for date field length aliasing and missing-pattern edge cases. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
tomasr8
left a comment
There was a problem hiding this comment.
Not super familiar with this, but nothing jumps out at me. The comments were very helpful though :)
| elif isinstance(data, tuple): | ||
| alias, others = data | ||
| data = alias.resolve(base) | ||
| if others: # Apply overrides on a copy. |
There was a problem hiding this comment.
Sorry, I'm not that familiar with this part of the codebase - why is this needed? And should it be tested?
There was a problem hiding this comment.
others were simply ignored, which caused some chains of override/alias to fail (which are needed here).
There's an explicit test now :)
We had some manual fallbacks for missing field/unit lengths, and ignored many `<alias>`es in CLDR data entirely. This PR imports those aliases and uses them to resolve missing lengths, improving output in corner cases and fixes some rare KeyError crashes too. There's a quirk related to person-variant unit aliases, which are only expressed in CLDR for the short length. If one follows the spec to the letter, a request for a long/narrow person-variant unit will route through the short length and lose the requested length. We use the same "workaround" as ICU to preserve the length, by importing these as whole-unit aliases. Not totally spec- compliant, but the end result is simply better. Fixes #1076
We had some manual fallbacks for missing field/unit lengths, and ignored many
<alias>es in CLDR data entirely.This PR imports those aliases and uses them to resolve missing lengths, improving output in corner cases and fixes some rare KeyError crashes too.
There's a quirk related to person-variant unit aliases, which are only expressed in CLDR for the short length. If one follows the spec to the letter, a request for a long/narrow person-variant unit will route through the short length and lose the requested length.
We use the same "workaround" as ICU to preserve the length, by importing these as whole-unit aliases. Not totally spec-compliant, but the end result is simply better.
Fixes #1076.