python-intl 0.9.0__tar.gz → 0.10.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-intl
3
- Version: 0.9.0
3
+ Version: 0.10.0
4
4
  Summary: Python implementation of the Intl JavaScript API
5
5
  Author: David Danier
6
6
  Author-email: David Danier <david.danier@gmail.com>
7
7
  License-Expression: MIT
8
8
  License-File: LICENSE
9
- Requires-Dist: pyicu>=2.16.2
9
+ Requires-Dist: pyicu>=2.17.0
10
10
  Requires-Python: >=3.12
11
11
  Project-URL: Repository, https://github.com/ddanier/python-intl
12
12
  Description-Content-Type: text/markdown
@@ -184,4 +184,5 @@ are unsure about whether your changes really suits the project please create an
184
184
  issue first, to talk about this.
185
185
 
186
186
  Please do not contribute AI generated code unless we explicitly talked about
187
- this and agreed upon doing so first. In general I do not want AI contributions.
187
+ this and agreed upon doing so first. In general I do not want vibe coded
188
+ contributions.
@@ -171,4 +171,5 @@ are unsure about whether your changes really suits the project please create an
171
171
  issue first, to talk about this.
172
172
 
173
173
  Please do not contribute AI generated code unless we explicitly talked about
174
- this and agreed upon doing so first. In general I do not want AI contributions.
174
+ this and agreed upon doing so first. In general I do not want vibe coded
175
+ contributions.
@@ -1,12 +1,12 @@
1
1
  [project]
2
2
  name = "python-intl"
3
- version = "0.9.0"
3
+ version = "0.10.0"
4
4
  description = "Python implementation of the Intl JavaScript API"
5
5
  readme = "README.md"
6
6
  license = "MIT"
7
7
  license-files = ["LICENSE"]
8
8
  requires-python = ">=3.12"
9
- dependencies = ["pyicu>=2.16.2"]
9
+ dependencies = ["pyicu>=2.17.0"]
10
10
 
11
11
  [[project.authors]]
12
12
  name = "David Danier"
@@ -19,6 +19,7 @@ Repository = "https://github.com/ddanier/python-intl"
19
19
  dev = [
20
20
  "basedpyright>=1.40.1",
21
21
  "mypy>=2.3.1",
22
+ "packaging>=26.3",
22
23
  "pylsp-mypy>=0.8.0",
23
24
  "pyright>=1.1.411",
24
25
  "pytest>=9.1.1",
@@ -108,6 +109,8 @@ ignore_missing_imports = true
108
109
  [tool.basedpyright]
109
110
  pythonVersion = "3.12"
110
111
  reportAny = false
112
+ reportExplicitAny = false
113
+ reportUnusedCallResult = false
111
114
  reportMatchNotExhaustive = false
112
115
  reportUnknownLambdaType = false
113
116
  reportPrivateUsage = false
@@ -123,5 +126,5 @@ module-name = "python_intl"
123
126
  module-root = ""
124
127
 
125
128
  [build-system]
126
- requires = ["uv_build>=0.12.3,<0.13.0"]
129
+ requires = ["uv_build>=0.12.3,<0.14.0"]
127
130
  build-backend = "uv_build"
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "python-intl"
3
- version = "0.9.0"
3
+ version = "0.10.0"
4
4
  description = "Python implementation of the Intl JavaScript API"
5
5
  readme = "README.md"
6
6
  authors = [
@@ -10,7 +10,7 @@ license = "MIT"
10
10
  license-files = ["LICENSE"]
11
11
  requires-python = ">=3.12"
12
12
  dependencies = [
13
- "pyicu>=2.16.2",
13
+ "pyicu>=2.17.0",
14
14
  ]
15
15
 
16
16
  [project.urls]
@@ -20,6 +20,7 @@ Repository = "https://github.com/ddanier/python-intl"
20
20
  dev = [
21
21
  "basedpyright>=1.40.1",
22
22
  "mypy>=2.3.1",
23
+ "packaging>=26.3",
23
24
  "pylsp-mypy>=0.8.0",
24
25
  "pyright>=1.1.411",
25
26
  "pytest>=9.1.1",
@@ -111,6 +112,8 @@ ignore_missing_imports = true
111
112
  [tool.basedpyright]
112
113
  pythonVersion = "3.12"
113
114
  reportAny = false
115
+ reportExplicitAny = false
116
+ reportUnusedCallResult = false
114
117
  reportMatchNotExhaustive = false
115
118
  reportUnknownLambdaType = false
116
119
  reportPrivateUsage = false
@@ -123,7 +126,7 @@ reportUnknownParameterType = false
123
126
  reportUnknownArgumentType = false
124
127
 
125
128
  [build-system]
126
- requires = ["uv_build>=0.12.3,<0.13.0"]
129
+ requires = ["uv_build>=0.12.3,<0.14.0"]
127
130
  build-backend = "uv_build"
128
131
 
129
132
  [tool.uv.build-backend]
@@ -5,8 +5,8 @@ from .collator import (
5
5
  from .datetimeformat import (
6
6
  DateTimeFormat as DateTimeFormat,
7
7
  DateTimeFormatOptions as DateTimeFormatOptions,
8
- DateTimeIntervalPatternPart as DateTimeIntervalPatternPart,
9
- DateTimePatternPart as DateTimePatternPart,
8
+ DateTimeFormattedPart as DateTimeFormattedPart,
9
+ DateTimeIntervalFormattedPart as DateTimeIntervalFormattedPart,
10
10
  FormatPatternNotFoundError as FormatPatternNotFoundError,
11
11
  )
12
12
  from .numberformat import (
@@ -1,7 +1,7 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import dataclasses
4
- from functools import cache, cached_property
4
+ from functools import cached_property
5
5
  from typing import TYPE_CHECKING, Literal, override
6
6
 
7
7
  import icu
@@ -30,7 +30,7 @@ if TYPE_CHECKING:
30
30
  YearFormatT,
31
31
  )
32
32
 
33
- type DatetimePatternPartTypeT = Literal[
33
+ type DatetimeFormattedPartTypeT = Literal[
34
34
  "literal", "unknown",
35
35
  "era", "year", "month", "weekday", "day", "day_period",
36
36
  "hour", "minute", "second", "fraction_second_digits",
@@ -58,7 +58,7 @@ if TYPE_CHECKING:
58
58
  time_zone_name: NotRequired[TimezoneNameFormatT]
59
59
 
60
60
  _PATTERN_SYMBOLS = "GyYuUrQqMLqQdDFgEecabBhHkKmsSAzZOvVxX" # includes unused
61
- _PATTERN_SYMBOL_TO_TYPE: dict[str, DatetimePatternPartTypeT] = {
61
+ _PATTERN_SYMBOL_TO_TYPE: dict[str, DatetimeFormattedPartTypeT] = {
62
62
  "G": "era",
63
63
  "y": "year",
64
64
  "Y": "year",
@@ -89,27 +89,36 @@ _PATTERN_SYMBOL_TO_TYPE: dict[str, DatetimePatternPartTypeT] = {
89
89
  "x": "time_zone_name",
90
90
  "X": "time_zone_name",
91
91
  }
92
- _PATTERN_FIELD_TO_TYPE: dict[icu.UDateTimePatternField, DatetimePatternPartTypeT] = {
93
- icu.DateFormat.ERA_FIELD: "era",
94
- icu.DateFormat.YEAR_FIELD: "year",
95
- icu.DateFormat.MONTH_FIELD: "month",
96
- icu.DateFormat.DAY_OF_WEEK_FIELD: "weekday",
97
- icu.DateFormat.DATE_FIELD: "day",
98
- icu.DateFormat.AM_PM_FIELD: "day_period",
99
- # TODO(ddanier): Use this instead once PyICU has those values in the enum:
100
- # https://gitlab.pyicu.org/main/pyicu/-/work_items/180
101
- # icu.DateFormat.AM_PM_MIDNIGHT_NOON_FIELD: "day_period",
102
- # icu.DateFormat.FLEXIBLE_DAY_PERIOD_FIELD: "day_period",
103
- 35: "day_period",
104
- 36: "day_period",
105
- icu.DateFormat.HOUR0_FIELD: "hour",
106
- icu.DateFormat.HOUR1_FIELD: "hour",
107
- icu.DateFormat.HOUR_OF_DAY0_FIELD: "hour",
108
- icu.DateFormat.HOUR_OF_DAY1_FIELD: "hour",
109
- icu.DateFormat.MINUTE_FIELD: "minute",
110
- icu.DateFormat.SECOND_FIELD: "second",
111
- icu.DateFormat.MILLISECOND_FIELD: "fraction_second_digits",
112
- icu.DateFormat.TIMEZONE_FIELD: "time_zone_name",
92
+ _PATTERN_FIELD_TO_TYPE: dict[icu.UDateTimePatternField, DatetimeFormattedPartTypeT] = {
93
+ icu.UDateFormatField.ERA: "era",
94
+ icu.UDateFormatField.YEAR: "year",
95
+ icu.UDateFormatField.MONTH: "month",
96
+ icu.UDateFormatField.DATE: "day",
97
+ icu.UDateFormatField.HOUR_OF_DAY0: "hour",
98
+ icu.UDateFormatField.HOUR_OF_DAY1: "hour",
99
+ icu.UDateFormatField.MINUTE: "minute",
100
+ icu.UDateFormatField.SECOND: "second",
101
+ icu.UDateFormatField.FRACTIONAL_SECOND: "fraction_second_digits",
102
+ icu.UDateFormatField.DAY_OF_WEEK: "weekday",
103
+ icu.UDateFormatField.AM_PM: "day_period",
104
+ icu.UDateFormatField.HOUR1: "hour",
105
+ icu.UDateFormatField.HOUR0: "hour",
106
+ icu.UDateFormatField.TIMEZONE: "time_zone_name",
107
+ icu.UDateFormatField.EXTENDED_YEAR: "year",
108
+ icu.UDateFormatField.TIMEZONE_RFC: "time_zone_name",
109
+ icu.UDateFormatField.TIMEZONE_GENERIC: "time_zone_name",
110
+ icu.UDateFormatField.STANDALONE_DAY: "weekday",
111
+ icu.UDateFormatField.STANDALONE_MONTH: "month",
112
+ icu.UDateFormatField.TIMEZONE_SPECIAL: "time_zone_name",
113
+ icu.UDateFormatField.YEAR_NAME: "year",
114
+ icu.UDateFormatField.AM_PM_MIDNIGHT_NOON: "day_period",
115
+ icu.UDateFormatField.FLEXIBLE_DAY_PERIOD: "day_period",
116
+ }
117
+ _ICU_HOUR_CYCLE_TO_HOUR_CYCLE: dict[icu.UDateTimePatternField, HourCycleT] = {
118
+ icu.UDateFormatHourCycle.HOUR_CYCLE_11: "h11",
119
+ icu.UDateFormatHourCycle.HOUR_CYCLE_12: "h12",
120
+ icu.UDateFormatHourCycle.HOUR_CYCLE_23: "h23",
121
+ icu.UDateFormatHourCycle.HOUR_CYCLE_24: "h24",
113
122
  }
114
123
  _PATTERN_QUOTE = "'"
115
124
 
@@ -172,7 +181,10 @@ class DateTimeFormatOptions:
172
181
  }
173
182
 
174
183
 
175
- def _options_to_possible_skeletons(options: DateTimeFormatOptions) -> Iterable[str]: # noqa: PLR0912, PLR0915
184
+ def _options_to_possible_skeletons( # noqa: PLR0912, PLR0915
185
+ generator: icu.DateTimePatternGenerator,
186
+ options: DateTimeFormatOptions,
187
+ ) -> Iterable[str]:
176
188
  skeleton_parts: list[str | tuple[str, ...]] = []
177
189
 
178
190
  # Note: The parts should be ordered from big to small.
@@ -225,35 +237,39 @@ def _options_to_possible_skeletons(options: DateTimeFormatOptions) -> Iterable[s
225
237
  case "narrow":
226
238
  skeleton_parts.append("BBBBB")
227
239
 
228
- match (options.hour_cycle, options.hour12, options.hour):
229
- case ("h11", _, "numeric"):
230
- skeleton_parts.append(("aK", "K"))
231
- case ("h11", _, "2-digit"):
232
- skeleton_parts.append(("aKK", "KK"))
233
- case ("h12", _, "numeric"):
234
- skeleton_parts.append(("ah", "h"))
235
- case ("h12", _, "2-digit"):
236
- skeleton_parts.append(("ahh", "hh"))
237
- case ("h23", _, "numeric"):
240
+ hour_cycle = options.hour_cycle
241
+ if not hour_cycle:
242
+ if options.hour and not options.hour12:
243
+ hour_cycle = _ICU_HOUR_CYCLE_TO_HOUR_CYCLE[generator.getDefaultHourCycle()]
244
+ elif options.hour12 is not None:
245
+ match (options.hour12, generator.getDefaultHourCycle()):
246
+ case (True, icu.UDateFormatHourCycle.HOUR_CYCLE_11):
247
+ hour_cycle = "h11"
248
+ case (True, icu.UDateFormatHourCycle.HOUR_CYCLE_12):
249
+ hour_cycle = "h12"
250
+ case (True, icu.UDateFormatHourCycle.HOUR_CYCLE_23):
251
+ hour_cycle = "h11"
252
+ case (True, icu.UDateFormatHourCycle.HOUR_CYCLE_24):
253
+ hour_cycle = "h12"
254
+ case (False, _):
255
+ hour_cycle = "h23"
256
+ match (hour_cycle, options.hour):
257
+ case ("h11", "numeric"):
258
+ skeleton_parts.append("K")
259
+ case ("h11", "2-digit"):
260
+ skeleton_parts.append("KK")
261
+ case ("h12", "numeric"):
262
+ skeleton_parts.append("h")
263
+ case ("h12", "2-digit"):
264
+ skeleton_parts.append("hh")
265
+ case ("h23", "numeric"):
238
266
  skeleton_parts.append("H")
239
- case ("h23", _, "2-digit"):
267
+ case ("h23", "2-digit"):
240
268
  skeleton_parts.append("HH")
241
- case ("h24", _, "numeric"):
269
+ case ("h24", "numeric"):
242
270
  skeleton_parts.append("k")
243
- case ("h24", _, "2-digit"):
271
+ case ("h24", "2-digit"):
244
272
  skeleton_parts.append("kk")
245
- case (_, True, "numeric"):
246
- skeleton_parts.append(("ah", "h"))
247
- case (_, True, "2-digit"):
248
- skeleton_parts.append(("ahh", "hh"))
249
- case (_, False, "numeric"):
250
- skeleton_parts.append("H")
251
- case (_, False, "2-digit"):
252
- skeleton_parts.append("HH")
253
- case (_, _, "numeric"):
254
- skeleton_parts.append("j")
255
- case (_, _, "2-digit"):
256
- skeleton_parts.append(("jj", "j"))
257
273
 
258
274
  match options.minute:
259
275
  case "numeric":
@@ -314,8 +330,8 @@ class FormatPatternNotFoundError(Exception):
314
330
 
315
331
 
316
332
  @dataclasses.dataclass(kw_only=True, frozen=True, slots=True)
317
- class DateTimePatternPart:
318
- type: DatetimePatternPartTypeT
333
+ class DateTimeFormattedPart:
334
+ type: DatetimeFormattedPartTypeT
319
335
  value: str
320
336
  _pattern: str | None = None
321
337
 
@@ -328,8 +344,8 @@ class DateTimePatternPart:
328
344
 
329
345
 
330
346
  @dataclasses.dataclass(kw_only=True, frozen=True, slots=True)
331
- class DateTimeIntervalPatternPart:
332
- type: DatetimePatternPartTypeT
347
+ class DateTimeIntervalFormattedPart:
348
+ type: DatetimeFormattedPartTypeT
333
349
  value: str
334
350
  source: Literal["start_range", "end_range", "shared"]
335
351
 
@@ -341,14 +357,11 @@ class DateTimeIntervalPatternPart:
341
357
  }
342
358
 
343
359
 
344
- @cache
345
360
  def _options_to_format_pattern(
346
- locale: icu.Locale,
361
+ generator: icu.DateTimePatternGenerator,
347
362
  options: DateTimeFormatOptions,
348
363
  ) -> _MatchedFormatPattern:
349
- possible_skeletons = list(_options_to_possible_skeletons(options))
350
-
351
- generator = icu.DateTimePatternGenerator.createInstance(locale)
364
+ possible_skeletons = list(_options_to_possible_skeletons(generator, options))
352
365
 
353
366
  # Try a perfect match
354
367
  for skeleton in possible_skeletons:
@@ -410,9 +423,13 @@ class DateTimeFormat:
410
423
  else:
411
424
  self.options = DateTimeFormatOptions(**options)
412
425
 
426
+ @cached_property
427
+ def _icu_datetime_pattern_generator(self) -> icu.DateTimePatternGenerator:
428
+ return icu.DateTimePatternGenerator.createInstance(self.locale._icu_locale)
429
+
413
430
  @cached_property
414
431
  def _matched_pattern(self) -> _MatchedFormatPattern:
415
- return _options_to_format_pattern(self.locale._icu_locale, self.options)
432
+ return _options_to_format_pattern(self._icu_datetime_pattern_generator, self.options)
416
433
 
417
434
  @cached_property
418
435
  def _icu_pattern(self) -> str:
@@ -425,7 +442,12 @@ class DateTimeFormat:
425
442
  def format(self, datetime_: dt.datetime, /) -> str:
426
443
  return self._icu_date_format.format(datetime_)
427
444
 
428
- def format_to_parts(self, datetime_: dt.datetime, /) -> Iterable[DateTimePatternPart]:
445
+ def format_to_parts(self, datetime_: dt.datetime, /) -> Iterable[DateTimeFormattedPart]:
446
+ # Sadly there is no formatToValue() function available inside the SimpleDateFormat class
447
+ # ICU provides (formatToValue() could return a "formatted" object one could iterate
448
+ # over to get the different parts). This means we need to parse the pattern ourselved here
449
+ # and create the parts ourselves. Luckily we can adapt the ICU code for parsing the pattern
450
+ # easily and thus still have a relatively sane implementation.
429
451
  # Based on https://github.com/unicode-org/icu/blob/6fb634d81d10dd4667fb3fbcd1f19d9b9b926e62/icu4c/source/i18n/smpdtfmt.cpp#L1060
430
452
  pattern = self._icu_pattern
431
453
  in_quote = False
@@ -438,7 +460,7 @@ class DateTimeFormat:
438
460
  char = pattern[i]
439
461
 
440
462
  if char != prev_char and count > 0:
441
- yield DateTimePatternPart(
463
+ yield DateTimeFormattedPart(
442
464
  type=_PATTERN_SYMBOL_TO_TYPE.get(prev_char, "unknown"),
443
465
  value=icu.SimpleDateFormat(prev_char * count, self.locale._icu_locale).format(datetime_),
444
466
  _pattern=prev_char * count,
@@ -453,7 +475,7 @@ class DateTimeFormat:
453
475
  in_quote = not in_quote
454
476
  elif not in_quote and char in _PATTERN_SYMBOLS:
455
477
  if literal_chars:
456
- yield DateTimePatternPart(
478
+ yield DateTimeFormattedPart(
457
479
  type="literal",
458
480
  value="".join(literal_chars),
459
481
  )
@@ -466,21 +488,23 @@ class DateTimeFormat:
466
488
  i += 1
467
489
 
468
490
  if count > 0:
469
- yield DateTimePatternPart(
491
+ yield DateTimeFormattedPart(
470
492
  type=_PATTERN_SYMBOL_TO_TYPE.get(prev_char, "unknown"),
471
493
  value=icu.SimpleDateFormat(prev_char * count, self.locale._icu_locale).format(datetime_),
472
494
  _pattern=prev_char * count,
473
495
  )
474
496
  assert not literal_chars # noqa: S101
475
497
  elif literal_chars:
476
- yield DateTimePatternPart(
498
+ yield DateTimeFormattedPart(
477
499
  type="literal",
478
500
  value="".join(literal_chars),
479
501
  )
480
502
 
481
503
  @cached_property
482
504
  def _icu_dateinterval_format(self) -> icu.DateIntervalFormat:
483
- possible_skeletons = list(_options_to_possible_skeletons(self.options))
505
+ possible_skeletons = list(
506
+ _options_to_possible_skeletons(self._icu_datetime_pattern_generator, self.options),
507
+ )
484
508
  return icu.DateIntervalFormat.createInstance(possible_skeletons[0], self.locale._icu_locale)
485
509
 
486
510
  def format_range(
@@ -495,7 +519,7 @@ class DateTimeFormat:
495
519
  self,
496
520
  start_datetime: dt.datetime,
497
521
  end_datetime: dt.datetime,
498
- ) -> Iterable[DateTimeIntervalPatternPart]:
522
+ ) -> Iterable[DateTimeIntervalFormattedPart]:
499
523
  icu_date_interval = icu.DateInterval(start_datetime, end_datetime)
500
524
  formatted = self._icu_dateinterval_format.formatToValue(icu_date_interval)
501
525
 
@@ -524,14 +548,14 @@ class DateTimeFormat:
524
548
  for part in formatted:
525
549
  span = _PartSpan.from_constrained_fieldposition(part)
526
550
  if span.start > last_end:
527
- yield DateTimeIntervalPatternPart(
551
+ yield DateTimeIntervalFormattedPart(
528
552
  type="literal",
529
553
  value=result_string[last_end:span.start],
530
554
  source=source_of(_PartSpan(start=last_end, end=span.start)),
531
555
  )
532
556
 
533
557
  if part.getCategory() == icu.UFieldCategory.DATE:
534
- yield DateTimeIntervalPatternPart(
558
+ yield DateTimeIntervalFormattedPart(
535
559
  type=_PATTERN_FIELD_TO_TYPE.get(part.getField(), "unknown"),
536
560
  value=result_string[span.start:span.end],
537
561
  source=source_of(span),
@@ -541,7 +565,7 @@ class DateTimeFormat:
541
565
 
542
566
  # Ensure we didn't miss anything at the end
543
567
  if last_end < len(result_string):
544
- yield DateTimeIntervalPatternPart(
568
+ yield DateTimeIntervalFormattedPart(
545
569
  type="literal",
546
570
  value=result_string[last_end:],
547
571
  source=source_of(_PartSpan(start=last_end, end=len(result_string))),
File without changes