@@ -68,17 +68,13 @@ class Formatter:
68
68
69
69
_FROM_FORMAT_RE : re .Pattern [str ] = re .compile (r"(?<!\\\[)" + _TOKENS + r"(?!\\\])" )
70
70
71
- _LOCALIZABLE_TOKENS : ClassVar [
72
- dict [str , str | Callable [[Locale ], Sequence [str ]] | None ]
73
- ] = {
71
+ _LOCALIZABLE_TOKENS : ClassVar [dict [str , str | Callable [[Locale ], Sequence [str ]] | None ]] = {
74
72
"Qo" : None ,
75
73
"MMMM" : "months.wide" ,
76
74
"MMM" : "months.abbreviated" ,
77
75
"Mo" : None ,
78
76
"DDDo" : None ,
79
- "Do" : lambda locale : tuple (
80
- rf"\d+{ o } " for o in locale .get ("custom.ordinal" ).values ()
81
- ),
77
+ "Do" : lambda locale : tuple (rf"\d+{ o } " for o in locale .get ("custom.ordinal" ).values ()),
82
78
"dddd" : "days.wide" ,
83
79
"ddd" : "days.abbreviated" ,
84
80
"dd" : "days.short" ,
@@ -242,9 +238,7 @@ class Formatter:
242
238
"z" : str ,
243
239
}
244
240
245
- def format (
246
- self , dt : pendulum .DateTime , fmt : str , locale : str | Locale | None = None
247
- ) -> str :
241
+ def format (self , dt : pendulum .DateTime , fmt : str , locale : str | Locale | None = None ) -> str :
248
242
"""
249
243
Formats a DateTime instance with a given format and locale.
250
244
@@ -303,9 +297,7 @@ def _format_token(self, dt: pendulum.DateTime, token: str, locale: Locale) -> st
303
297
304
298
return token
305
299
306
- def _format_localizable_token (
307
- self , dt : pendulum .DateTime , token : str , locale : Locale
308
- ) -> str :
300
+ def _format_localizable_token (self , dt : pendulum .DateTime , token : str , locale : Locale ) -> str :
309
301
"""
310
302
Formats a DateTime instance
311
303
with a given localizable token and locale.
@@ -377,8 +369,7 @@ def parse(
377
369
"""
378
370
escaped_fmt = re .escape (fmt )
379
371
380
- tokens = self ._FROM_FORMAT_RE .findall (escaped_fmt )
381
- if not tokens :
372
+ if not self ._FROM_FORMAT_RE .search (escaped_fmt ):
382
373
raise ValueError ("The given time string does not match the given format" )
383
374
384
375
if not locale :
@@ -402,11 +393,9 @@ def parse(
402
393
"timestamp" : None ,
403
394
}
404
395
405
- pattern = self ._FROM_FORMAT_RE .sub (
406
- lambda m : self ._replace_tokens (m .group (0 ), loaded_locale ), escaped_fmt
407
- )
396
+ pattern = self ._FROM_FORMAT_RE .sub (lambda m : self ._replace_tokens (m .group (0 ), loaded_locale ), escaped_fmt )
408
397
409
- if not re .search ( "^" + pattern + "$" , time ):
398
+ if not re .fullmatch ( pattern , time ):
410
399
raise ValueError (f"String does not match format { fmt } " )
411
400
412
401
def _get_parsed_values (m : Match [str ]) -> Any :
@@ -416,9 +405,7 @@ def _get_parsed_values(m: Match[str]) -> Any:
416
405
417
406
return self ._check_parsed (parsed , now )
418
407
419
- def _check_parsed (
420
- self , parsed : dict [str , Any ], now : pendulum .DateTime
421
- ) -> dict [str , Any ]:
408
+ def _check_parsed (self , parsed : dict [str , Any ], now : pendulum .DateTime ) -> dict [str , Any ]:
422
409
"""
423
410
Checks validity of parsed elements.
424
411
@@ -619,9 +606,7 @@ def _get_parsed_value(
619
606
620
607
parsed ["tz" ] = pendulum .timezone (value )
621
608
622
- def _get_parsed_locale_value (
623
- self , token : str , value : str , parsed : dict [str , Any ], locale : Locale
624
- ) -> None :
609
+ def _get_parsed_locale_value (self , token : str , value : str , parsed : dict [str , Any ], locale : Locale ) -> None :
625
610
if token == "MMMM" :
626
611
unit = "month"
627
612
match = "months.wide"
@@ -680,11 +665,7 @@ def _replace_tokens(self, token: str, locale: Locale) -> str:
680
665
if callable (values ):
681
666
candidates = values (locale )
682
667
else :
683
- candidates = tuple (
684
- locale .translation (
685
- cast ("str" , self ._LOCALIZABLE_TOKENS [token ])
686
- ).values ()
687
- )
668
+ candidates = tuple (locale .translation (cast ("str" , self ._LOCALIZABLE_TOKENS [token ])).values ())
688
669
else :
689
670
candidates = cast ("Sequence[str]" , self ._REGEX_TOKENS [token ])
690
671
0 commit comments