Skip to content

Commit f42ed4e

Browse files
committed
Update tests and code for CLDR 48.2
- Unit IDs renamed in 48 still work (with deprecation warnings raised)
1 parent 4fdfc58 commit f42ed4e

9 files changed

Lines changed: 45 additions & 15 deletions

File tree

babel/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ def get_cldr_version() -> str:
13721372
function is a string representing a version number, e.g. '47'.
13731373
13741374
>>> get_cldr_version()
1375-
'47'
1375+
'48'
13761376
13771377
.. versionadded:: 2.18
13781378

babel/numbers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ def format_currency(
741741
>>> format_currency(1099.98, 'JPY', locale='en_US')
742742
'\\xa51,100'
743743
>>> format_currency(1099.98, 'COP', '#,##0.00', locale='es_ES')
744-
'1.099,98'
744+
'1.100'
745745
746746
However, the number of decimal digits can be overridden from the currency
747747
information, by setting the last parameter to ``False``:

babel/units.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
from __future__ import annotations
22

33
import decimal
4+
import warnings
45
from typing import Literal
56

67
from babel.core import Locale
78
from babel.numbers import LC_NUMERIC, format_decimal
89

10+
_DEPRECATED_UNIT_IDS: dict[str, str] = {
11+
# Unit IDs deprecated in CLDR 48
12+
"concentr-permillion": "concentr-part-per-1e6",
13+
"concentr-portion": "concentr-part",
14+
"concentr-portion-per-1e9": "concentr-part-per-1e9",
15+
"permillion": "part-per-1e6",
16+
"portion": "part",
17+
"portion-per-1e9": "part-per-1e9",
18+
}
19+
920

1021
class UnknownUnitError(ValueError):
1122
def __init__(self, unit: str, locale: Locale) -> None:
@@ -66,6 +77,19 @@ def _find_unit_pattern(unit_id: str, locale: Locale | str | None = None) -> str
6677
unit_patterns: dict[str, str] = locale._data["unit_patterns"]
6778
if unit_id in unit_patterns:
6879
return unit_id
80+
81+
# Only if a direct hit didn't work, try deprecated units...
82+
new_id = _DEPRECATED_UNIT_IDS.get(unit_id)
83+
if new_id is not None:
84+
warnings.warn(
85+
f"Unit identifier {unit_id!r} is deprecated; use {new_id!r} instead.",
86+
DeprecationWarning,
87+
stacklevel=4,
88+
)
89+
if new_id in unit_patterns:
90+
return new_id
91+
92+
# ... and finally fuzzy matching.
6993
for unit_pattern in sorted(unit_patterns, key=len):
7094
if unit_pattern.endswith(unit_id):
7195
return unit_pattern

tests/test_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,4 +413,4 @@ def test_locale_parse_empty():
413413

414414

415415
def test_get_cldr_version():
416-
assert core.get_cldr_version() == "47"
416+
assert core.get_cldr_version() == "48"

tests/test_dates.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,10 @@ def test_format_time(timezone_getter):
613613
def test_format_skeleton(timezone_getter):
614614
dt = datetime(2007, 4, 1, 15, 30)
615615
assert (dates.format_skeleton('yMEd', dt, locale='en_US') == 'Sun, 4/1/2007')
616-
assert (dates.format_skeleton('yMEd', dt, locale='th') == 'อา. 1/4/2007')
616+
assert (dates.format_skeleton('yMEd', dt, locale='th') == 'อาทิตย์ 1/4/2007')
617617

618618
assert (dates.format_skeleton('EHm', dt, locale='en') == 'Sun 15:30')
619-
assert (dates.format_skeleton('EHm', dt, tzinfo=timezone_getter('Asia/Bangkok'), locale='th') == 'อา. 22:30 น.')
619+
assert (dates.format_skeleton('EHm', dt, tzinfo=timezone_getter('Asia/Bangkok'), locale='th') == 'อาทิตย์ 22:30 น.')
620620

621621

622622
@pytest.mark.parametrize(('skeleton', 'expected'), [
@@ -1194,8 +1194,8 @@ def test_issue_1192():
11941194
# The actual returned value here is not actually strictly specified ("get_timezone_name"
11951195
# is not an operation specified as such). Issue #1192 concerned this invocation returning
11961196
# the invalid "no inheritance marker" value; _that_ should never be returned here.
1197-
# IOW, if the below "Hawaii-Aleutian Time" changes with e.g. CLDR updates, that's fine.
1198-
assert dates.get_timezone_name('Pacific/Honolulu', 'short', locale='en_GB') == "Hawaii-Aleutian Time"
1197+
# IOW, if the below "United States (Honolulu) Time" changes with e.g. CLDR updates, that's fine.
1198+
assert dates.get_timezone_name('Pacific/Honolulu', 'short', locale='en_GB') == "United States (Honolulu) Time"
11991199

12001200

12011201
@pytest.mark.xfail

tests/test_lists.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
(['string1', 'string2'], 'en', 'string1 and string2'),
1010
(['string1', 'string2', 'string3'], 'en', 'string1, string2, and string3'),
1111
(['string1', 'string2', 'string3'], 'zh', 'string1、string2和string3'),
12-
(['string1', 'string2', 'string3', 'string4'], 'ne', 'string1,string2, string3 र string4'),
12+
(['string1', 'string2', 'string3', 'string4'], 'ne', 'string1, string2, string3 र string4'),
1313
])
1414
def test_format_list(list, locale, expected):
1515
assert lists.format_list(list, locale=locale) == expected

tests/test_numbers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_list_currencies():
4040

4141
assert list_currencies(locale='pa_Arab') == {'PKR', 'INR', 'EUR'}
4242

43-
assert len(list_currencies()) == 307
43+
assert len(list_currencies()) == 308
4444

4545

4646
def test_validate_currency():
@@ -297,7 +297,7 @@ def test_format_currency_format_type():
297297
assert (numbers.format_currency(1099.98, 'JPY', locale='en_US')
298298
== '\xa51,100')
299299
assert (numbers.format_currency(1099.98, 'COP', '#,##0.00', locale='es_ES')
300-
== '1.099,98')
300+
== '1.100')
301301
assert (numbers.format_currency(1099.98, 'JPY', locale='en_US',
302302
currency_digits=False)
303303
== '\xa51,099.98')
@@ -320,7 +320,7 @@ def test_format_compact_currency():
320320
assert numbers.format_compact_currency(123, 'EUR', locale='yav', format_type="short") == '€\xa0123'
321321
assert numbers.format_compact_currency(12345, 'EUR', locale='yav', format_type="short") == '€\xa012K'
322322
assert numbers.format_compact_currency(123456789, 'EUR', locale='de_DE', fraction_digits=1) == '123,5\xa0Mio.\xa0€'
323-
assert numbers.format_compact_currency(123456789, 'USD', locale='ar_EG', fraction_digits=2, format_type="short", numbering_system="default") == '123٫46\xa0مليون\xa0US$'
323+
assert numbers.format_compact_currency(123456789, 'USD', locale='ar_EG', fraction_digits=2, format_type="short", numbering_system="default") == '\u200f123٫46\xa0مليون\xa0US$'
324324

325325

326326
def test_format_compact_currency_invalid_format_type():

tests/test_support_format.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_format_currency(ar_eg_format, en_us_format):
5353
def test_format_compact_currency(ar_eg_format, en_us_format):
5454
assert en_us_format.compact_currency(1099.98, 'USD') == '$1K'
5555
assert en_us_format.compact_currency(Decimal("1099.98"), 'USD') == '$1K'
56-
assert ar_eg_format.compact_currency(1099.98, 'EGP') == '1\xa0ألف\xa0ج.م.\u200f'
56+
assert ar_eg_format.compact_currency(1099.98, 'EGP') == '\u200f1\xa0ألف\xa0ج.م.\u200f'
5757

5858

5959
def test_format_percent(ar_eg_format, en_us_format):

tests/test_units.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
('speed-light-speed', 1, '1 světlo'),
99
('speed-light-speed', 2, '2 světla'),
1010
('speed-light-speed', 5, '5 světel'),
11-
('concentr-portion-per-1e9', 1, '1 částice na miliardu'),
12-
('concentr-portion-per-1e9', 2, '2 částice na miliardu'),
13-
('concentr-portion-per-1e9', 5, '5 částic na miliardu'),
11+
('concentr-part-per-1e9', 1, '1 částice na miliardu'),
12+
('concentr-part-per-1e9', 2, '2 částice na miliardu'),
13+
('concentr-part-per-1e9', 5, '5 částic na miliardu'),
1414
('duration-night', 1, '1 noc'),
1515
('duration-night', 2, '2 noci'),
1616
('duration-night', 5, '5 nocí'),
@@ -29,3 +29,9 @@ def test_new_cldr46_units(unit, count, expected):
2929
])
3030
def test_issue_1217(count, unit, locale, length, expected):
3131
assert format_unit(count, unit, length, locale=locale) == expected
32+
33+
34+
def test_deprecated_unit_ids():
35+
for id in ("concentr-permillion", "concentr-portion", "concentr-portion-per-1e9"):
36+
with pytest.warns(DeprecationWarning, match=id):
37+
format_unit(1, id, locale='en')

0 commit comments

Comments
 (0)