23
23
import decimal
24
24
import re
25
25
import warnings
26
- from typing import TYPE_CHECKING , Any , cast , overload
26
+ from typing import TYPE_CHECKING , Any , overload
27
27
28
28
from babel .core import Locale , default_locale , get_global
29
29
from babel .localedata import LocaleDataDict
@@ -428,7 +428,7 @@ def get_decimal_quantum(precision: int | decimal.Decimal) -> decimal.Decimal:
428
428
429
429
def format_decimal (
430
430
number : float | decimal .Decimal | str ,
431
- format : str | NumberPattern | None = None ,
431
+ format : str | None = None ,
432
432
locale : Locale | str | None = LC_NUMERIC ,
433
433
decimal_quantization : bool = True ,
434
434
group_separator : bool = True ,
@@ -474,8 +474,8 @@ def format_decimal(
474
474
number format.
475
475
"""
476
476
locale = Locale .parse (locale )
477
- if format is None :
478
- format = locale .decimal_formats [ format ]
477
+ if not format :
478
+ format = locale .decimal_formats . get ( format )
479
479
pattern = parse_pattern (format )
480
480
return pattern .apply (
481
481
number , locale , decimal_quantization = decimal_quantization , group_separator = group_separator )
@@ -513,15 +513,15 @@ def format_compact_decimal(
513
513
number , format = _get_compact_format (number , compact_format , locale , fraction_digits )
514
514
# Did not find a format, fall back.
515
515
if format is None :
516
- format = locale .decimal_formats [ None ]
516
+ format = locale .decimal_formats . get ( None )
517
517
pattern = parse_pattern (format )
518
518
return pattern .apply (number , locale , decimal_quantization = False )
519
519
520
520
521
521
def _get_compact_format (
522
522
number : float | decimal .Decimal | str ,
523
523
compact_format : LocaleDataDict ,
524
- locale : Locale ,
524
+ locale : Locale | str | None ,
525
525
fraction_digits : int ,
526
526
) -> tuple [decimal .Decimal , NumberPattern | None ]:
527
527
"""Returns the number after dividing by the unit and the format pattern to use.
@@ -543,7 +543,7 @@ def _get_compact_format(
543
543
break
544
544
# otherwise, we need to divide the number by the magnitude but remove zeros
545
545
# equal to the number of 0's in the pattern minus 1
546
- number = cast ( decimal . Decimal , number / (magnitude // (10 ** (pattern .count ("0" ) - 1 ) )))
546
+ number = number / (magnitude // (10 ** (pattern .count ("0" ) - 1 )))
547
547
# round to the number of fraction digits requested
548
548
rounded = round (number , fraction_digits )
549
549
# if the remaining number is singular, use the singular format
@@ -565,7 +565,7 @@ class UnknownCurrencyFormatError(KeyError):
565
565
def format_currency (
566
566
number : float | decimal .Decimal | str ,
567
567
currency : str ,
568
- format : str | NumberPattern | None = None ,
568
+ format : str | None = None ,
569
569
locale : Locale | str | None = LC_NUMERIC ,
570
570
currency_digits : bool = True ,
571
571
format_type : Literal ["name" , "standard" , "accounting" ] = "standard" ,
@@ -680,7 +680,7 @@ def format_currency(
680
680
def _format_currency_long_name (
681
681
number : float | decimal .Decimal | str ,
682
682
currency : str ,
683
- format : str | NumberPattern | None = None ,
683
+ format : str | None = None ,
684
684
locale : Locale | str | None = LC_NUMERIC ,
685
685
currency_digits : bool = True ,
686
686
format_type : Literal ["name" , "standard" , "accounting" ] = "standard" ,
@@ -706,7 +706,7 @@ def _format_currency_long_name(
706
706
707
707
# Step 5.
708
708
if not format :
709
- format = locale .decimal_formats [ format ]
709
+ format = locale .decimal_formats . get ( format )
710
710
711
711
pattern = parse_pattern (format )
712
712
@@ -758,15 +758,13 @@ def format_compact_currency(
758
758
# compress adjacent spaces into one
759
759
format = re .sub (r'(\s)\s+' , r'\1' , format ).strip ()
760
760
break
761
- if format is None :
762
- raise ValueError ('No compact currency format found for the given number and locale.' )
763
761
pattern = parse_pattern (format )
764
762
return pattern .apply (number , locale , currency = currency , currency_digits = False , decimal_quantization = False )
765
763
766
764
767
765
def format_percent (
768
766
number : float | decimal .Decimal | str ,
769
- format : str | NumberPattern | None = None ,
767
+ format : str | None = None ,
770
768
locale : Locale | str | None = LC_NUMERIC ,
771
769
decimal_quantization : bool = True ,
772
770
group_separator : bool = True ,
@@ -810,15 +808,15 @@ def format_percent(
810
808
"""
811
809
locale = Locale .parse (locale )
812
810
if not format :
813
- format = locale .percent_formats [ format ]
811
+ format = locale .percent_formats . get ( format )
814
812
pattern = parse_pattern (format )
815
813
return pattern .apply (
816
814
number , locale , decimal_quantization = decimal_quantization , group_separator = group_separator )
817
815
818
816
819
817
def format_scientific (
820
818
number : float | decimal .Decimal | str ,
821
- format : str | NumberPattern | None = None ,
819
+ format : str | None = None ,
822
820
locale : Locale | str | None = LC_NUMERIC ,
823
821
decimal_quantization : bool = True ,
824
822
) -> str :
@@ -849,7 +847,7 @@ def format_scientific(
849
847
"""
850
848
locale = Locale .parse (locale )
851
849
if not format :
852
- format = locale .scientific_formats [ format ]
850
+ format = locale .scientific_formats . get ( format )
853
851
pattern = parse_pattern (format )
854
852
return pattern .apply (
855
853
number , locale , decimal_quantization = decimal_quantization )
@@ -858,7 +856,7 @@ def format_scientific(
858
856
class NumberFormatError (ValueError ):
859
857
"""Exception raised when a string cannot be parsed into a number."""
860
858
861
- def __init__ (self , message : str , suggestions : list [ str ] | None = None ) -> None :
859
+ def __init__ (self , message : str , suggestions : str | None = None ) -> None :
862
860
super ().__init__ (message )
863
861
#: a list of properly formatted numbers derived from the invalid input
864
862
self .suggestions = suggestions
@@ -1142,7 +1140,7 @@ def scientific_notation_elements(self, value: decimal.Decimal, locale: Locale |
1142
1140
1143
1141
def apply (
1144
1142
self ,
1145
- value : float | decimal .Decimal | str ,
1143
+ value : float | decimal .Decimal ,
1146
1144
locale : Locale | str | None ,
1147
1145
currency : str | None = None ,
1148
1146
currency_digits : bool = True ,
@@ -1213,9 +1211,9 @@ def apply(
1213
1211
number = '' .join ([
1214
1212
self ._quantize_value (value , locale , frac_prec , group_separator ),
1215
1213
get_exponential_symbol (locale ),
1216
- exp_sign , # type: ignore # exp_sign is always defined here
1217
- self ._format_int (str ( exp ), self . exp_prec [ 0 ], self . exp_prec [ 1 ], locale ) # type: ignore # exp is always defined here
1218
- ])
1214
+ exp_sign ,
1215
+ self ._format_int (
1216
+ str ( exp ), self . exp_prec [ 0 ], self . exp_prec [ 1 ], locale ) ])
1219
1217
1220
1218
# Is it a significant digits pattern?
1221
1219
elif '@' in self .pattern :
@@ -1236,8 +1234,9 @@ def apply(
1236
1234
number if self .number_pattern != '' else '' ,
1237
1235
self .suffix [is_negative ]])
1238
1236
1239
- if '¤' in retval and currency is not None :
1240
- retval = retval .replace ('¤¤¤' , get_currency_name (currency , value , locale ))
1237
+ if '¤' in retval :
1238
+ retval = retval .replace ('¤¤¤' ,
1239
+ get_currency_name (currency , value , locale ))
1241
1240
retval = retval .replace ('¤¤' , currency .upper ())
1242
1241
retval = retval .replace ('¤' , get_currency_symbol (currency , locale ))
1243
1242
0 commit comments