1010 :copyright: (c) 2013-2022 by the Babel Team.
1111 :license: BSD, see LICENSE for more details.
1212"""
13+ from __future__ import annotations
1314
1415import gettext
1516import locale
17+ from datetime import date as _date , datetime as _datetime , time as _time , timedelta as _timedelta
18+ from typing import TYPE_CHECKING , Literal
19+
20+ from pytz import BaseTzInfo
1621
1722from babel .core import Locale
18- from babel .dates import format_date , format_datetime , format_time , \
19- format_timedelta
20- from babel .numbers import format_decimal , format_currency , format_compact_currency , \
21- format_percent , format_scientific , format_compact_decimal
23+ from babel .dates import (format_date , format_datetime , format_time ,
24+ format_timedelta )
25+ from babel .numbers import (format_compact_currency , format_compact_decimal ,
26+ format_currency , format_decimal , format_percent ,
27+ format_scientific )
2228
29+ if TYPE_CHECKING :
30+ from babel .dates import _PredefinedTimeFormat
2331
2432class Format :
2533 """Wrapper class providing the various date and number formatting functions
@@ -34,7 +42,7 @@ class Format:
3442 u'1.234'
3543 """
3644
37- def __init__ (self , locale , tzinfo = None ):
45+ def __init__ (self , locale : Locale | str , tzinfo : BaseTzInfo | None = None ):
3846 """Initialize the formatter.
3947
4048 :param locale: the locale identifier or `Locale` instance
@@ -43,7 +51,7 @@ def __init__(self, locale, tzinfo=None):
4351 self .locale = Locale .parse (locale )
4452 self .tzinfo = tzinfo
4553
46- def date (self , date = None , format = 'medium' ):
54+ def date (self , date : _date | None = None , format : _PredefinedTimeFormat | str = 'medium' ) -> str :
4755 """Return a date formatted according to the given pattern.
4856
4957 >>> from datetime import date
@@ -53,7 +61,7 @@ def date(self, date=None, format='medium'):
5361 """
5462 return format_date (date , format , locale = self .locale )
5563
56- def datetime (self , datetime = None , format = 'medium' ):
64+ def datetime (self , datetime : _date | None = None , format : _PredefinedTimeFormat | str = 'medium' ) -> str :
5765 """Return a date and time formatted according to the given pattern.
5866
5967 >>> from datetime import datetime
@@ -65,7 +73,7 @@ def datetime(self, datetime=None, format='medium'):
6573 return format_datetime (datetime , format , tzinfo = self .tzinfo ,
6674 locale = self .locale )
6775
68- def time (self , time = None , format = 'medium' ):
76+ def time (self , time : _time | _datetime | None = None , format : _PredefinedTimeFormat | str = 'medium' ) -> str :
6977 """Return a time formatted according to the given pattern.
7078
7179 >>> from datetime import datetime
@@ -76,8 +84,10 @@ def time(self, time=None, format='medium'):
7684 """
7785 return format_time (time , format , tzinfo = self .tzinfo , locale = self .locale )
7886
79- def timedelta (self , delta , granularity = 'second' , threshold = .85 ,
80- format = 'long' , add_direction = False ):
87+ def timedelta (self , delta : _timedelta | int ,
88+ granularity : Literal ["year" , "month" , "week" , "day" , "hour" , "minute" , "second" ] = 'second' ,
89+ threshold : float = .85 , format : Literal ["narrow" , "short" , "medium" , "long" ] = 'long' ,
90+ add_direction : bool = False ) -> str :
8191 """Return a time delta according to the rules of the given locale.
8292
8393 >>> from datetime import timedelta
@@ -90,7 +100,7 @@ def timedelta(self, delta, granularity='second', threshold=.85,
90100 format = format , add_direction = add_direction ,
91101 locale = self .locale )
92102
93- def number (self , number ) :
103+ def number (self , number : float | decimal . Decimal | str ) -> str :
94104 """Return an integer number formatted for the locale.
95105
96106 >>> fmt = Format('en_US')
@@ -99,7 +109,7 @@ def number(self, number):
99109 """
100110 return format_decimal (number , locale = self .locale )
101111
102- def decimal (self , number , format = None ):
112+ def decimal (self , number : float | decimal . Decimal | str , format : str | None = None ) -> str :
103113 """Return a decimal number formatted for the locale.
104114
105115 >>> fmt = Format('en_US')
@@ -108,7 +118,8 @@ def decimal(self, number, format=None):
108118 """
109119 return format_decimal (number , format , locale = self .locale )
110120
111- def compact_decimal (self , number , format_type = 'short' , fraction_digits = 0 ):
121+ def compact_decimal (self , number : float | decimal .Decimal | str ,
122+ format_type : Literal ["short" , "long" ] = 'short' , fraction_digits : int = 0 ) -> str :
112123 """Return a number formatted in compact form for the locale.
113124
114125 >>> fmt = Format('en_US')
@@ -119,19 +130,20 @@ def compact_decimal(self, number, format_type='short', fraction_digits=0):
119130 fraction_digits = fraction_digits ,
120131 locale = self .locale )
121132
122- def currency (self , number , currency ) :
133+ def currency (self , number : float | decimal . Decimal | str , currency : str ) -> str :
123134 """Return a number in the given currency formatted for the locale.
124135 """
125136 return format_currency (number , currency , locale = self .locale )
126137
127- def compact_currency (self , number , currency , format_type = 'short' , fraction_digits = 0 ):
138+ def compact_currency (self , number : float | decimal .Decimal | str , currency : str ,
139+ format_type : Literal ['short' , 'long' ] = 'short' , fraction_digits : int = 0 ) -> str :
128140 """Return a number in the given currency formatted for the locale
129141 using the compact number format.
130142 """
131143 return format_compact_currency (number , currency , format_type = format_type ,
132144 fraction_digits = fraction_digits , locale = self .locale )
133145
134- def percent (self , number , format = None ):
146+ def percent (self , number : float | decimal . Decimal | str , format : str | None = None ) -> str :
135147 """Return a number formatted as percentage for the locale.
136148
137149 >>> fmt = Format('en_US')
@@ -140,7 +152,7 @@ def percent(self, number, format=None):
140152 """
141153 return format_percent (number , format , locale = self .locale )
142154
143- def scientific (self , number ) :
155+ def scientific (self , number : float | decimal . Decimal | str ) -> str :
144156 """Return a number formatted using scientific notation for the locale.
145157 """
146158 return format_scientific (number , locale = self .locale )
0 commit comments