Skip to content

Commit cbe8641

Browse files
author
Sébastien Eustace
committed
Merge branch 'bryanforbes-update-for-pep-561'
2 parents 58b10e3 + d1b430f commit cbe8641

File tree

18 files changed

+186
-121
lines changed

18 files changed

+186
-121
lines changed

pendulum/__init__.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from __future__ import absolute_import
22

33
import datetime as _datetime
4-
from typing import Union
4+
from typing import Union, Optional
55

66
from .__version__ import __version__
77

@@ -59,7 +59,7 @@
5959
SECONDS_PER_DAY,
6060
)
6161

62-
_TEST_NOW = None
62+
_TEST_NOW = None # type: Optional[DateTime]
6363
_LOCALE = "en"
6464
_WEEK_STARTS_AT = MONDAY
6565
_WEEK_ENDS_AT = SUNDAY
@@ -68,7 +68,7 @@
6868

6969

7070
def _safe_timezone(obj):
71-
# type: (Union[str, int, float, _datetime.tzinfo]) -> _Timezone
71+
# type: (Optional[Union[str, float, _datetime.tzinfo, _Timezone]]) -> _Timezone
7272
"""
7373
Creates a timezone instance
7474
from a string, Timezone, TimezoneInfo or integer offset.
@@ -105,7 +105,7 @@ def datetime(
105105
minute=0, # type: int
106106
second=0, # type: int
107107
microsecond=0, # type: int
108-
tz=UTC, # type: Union[str, _Timezone]
108+
tz=UTC, # type: Optional[Union[str, float, _Timezone]]
109109
dst_rule=POST_TRANSITION, # type: str
110110
): # type: (...) -> DateTime
111111
"""
@@ -169,8 +169,8 @@ def time(hour, minute=0, second=0, microsecond=0): # type: (int, int, int, int)
169169

170170

171171
def instance(
172-
dt, tz=UTC # type: _datetime.datetime # type: Union[str, _Timezone, None]
173-
): # type: (...) -> DateTime
172+
dt, tz=UTC
173+
): # type: (_datetime.datetime, Optional[Union[str, _Timezone]]) -> DateTime
174174
"""
175175
Create a DateTime instance from a datetime one.
176176
"""
@@ -198,7 +198,7 @@ def instance(
198198
)
199199

200200

201-
def now(tz=None): # type: (Union[str, _Timezone, None]) -> DateTime
201+
def now(tz=None): # type: (Optional[Union[str, _Timezone]]) -> DateTime
202202
"""
203203
Get a DateTime instance for the current date and time.
204204
"""
@@ -248,7 +248,7 @@ def from_format(
248248
string, # type: str
249249
fmt, # type: str
250250
tz=UTC, # type: Union[str, _Timezone]
251-
locale=None, # type: Union[str, None]
251+
locale=None, # type: Optional[str]
252252
): # type: (...) -> DateTime
253253
"""
254254
Creates a DateTime instance from a specific format.
@@ -261,8 +261,8 @@ def from_format(
261261

262262

263263
def from_timestamp(
264-
timestamp, tz=UTC # type: Union[int, float] # type: Union[str, _Timezone]
265-
): # type: (...) -> DateTime
264+
timestamp, tz=UTC
265+
): # type: (Union[int, float], Union[str, _Timezone]) -> DateTime
266266
"""
267267
Create a DateTime instance from a timestamp.
268268
"""
@@ -305,9 +305,7 @@ def duration(
305305
)
306306

307307

308-
def period(
309-
start, end, absolute=False # type: DateTime # type: DateTime # type: bool
310-
): # type: (...) -> Period
308+
def period(start, end, absolute=False): # type: (DateTime, DateTime, bool) -> Period
311309
"""
312310
Create a Period instance.
313311
"""

pendulum/_extensions/helpers.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from collections import namedtuple
33

44
import datetime
5+
import typing
56

67
from ..constants import (
78
EPOCH_YEAR,
@@ -48,18 +49,18 @@ def __repr__(self):
4849
)
4950

5051

51-
def is_leap(year):
52+
def is_leap(year): # type: (int) -> bool
5253
return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)
5354

5455

55-
def is_long_year(year):
56+
def is_long_year(year): # type: (int) -> bool
5657
def p(y):
5758
return y + y // 4 - y // 100 + y // 400
5859

5960
return p(year) % 7 == 4 or p(year - 1) % 7 == 3
6061

6162

62-
def week_day(year, month, day):
63+
def week_day(year, month, day): # type: (int, int, int) -> int
6364
if month < 3:
6465
year -= 1
6566

@@ -78,14 +79,14 @@ def week_day(year, month, day):
7879
return w
7980

8081

81-
def days_in_year(year):
82+
def days_in_year(year): # type: (int) -> int
8283
if is_leap(year):
8384
return DAYS_PER_L_YEAR
8485

8586
return DAYS_PER_N_YEAR
8687

8788

88-
def timestamp(dt): # type: (datetime) -> int
89+
def timestamp(dt): # type: (datetime.datetime) -> int
8990
year = dt.year
9091

9192
result = (year - 1970) * 365 + MONTHS_OFFSETS[0][dt.month]
@@ -107,7 +108,9 @@ def timestamp(dt): # type: (datetime) -> int
107108
return result
108109

109110

110-
def local_time(unix_time, utc_offset, microseconds):
111+
def local_time(
112+
unix_time, utc_offset, microseconds
113+
): # type: (int, int, int) -> typing.Tuple[int, int, int, int, int, int, int]
111114
"""
112115
Returns a UNIX time as a broken down time
113116
for a particular transition type.
@@ -182,7 +185,9 @@ def local_time(unix_time, utc_offset, microseconds):
182185
return (year, month, day, hour, minute, second, microseconds)
183186

184187

185-
def precise_diff(d1, d2):
188+
def precise_diff(
189+
d1, d2
190+
): # type: (typing.Union[datetime.datetime, datetime.date], typing.Union[datetime.datetime, datetime.date]) -> PreciseDiff
186191
"""
187192
Calculate a precise difference between two datetimes.
188193
@@ -341,7 +346,7 @@ def precise_diff(d1, d2):
341346
)
342347

343348

344-
def _day_number(year, month, day):
349+
def _day_number(year, month, day): # type: (int, int, int) -> int
345350
month = (month + 9) % 12
346351
year = year - month // 10
347352

pendulum/datetime.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import datetime
77
import pendulum
88

9-
from typing import Union
9+
from typing import Union, Optional, TypeVar
1010

1111
from .date import Date
1212
from .time import Time
@@ -37,9 +37,13 @@
3737
W3C,
3838
)
3939

40+
_D = TypeVar("_D", bound="DateTime")
41+
4042

4143
class DateTime(datetime.datetime, Date):
4244

45+
EPOCH = None # type: DateTime
46+
4347
# Formats
4448

4549
_FORMATS = {
@@ -93,7 +97,7 @@ def __new__(
9397
return self
9498

9599
@classmethod
96-
def now(cls, tz=None): # type: (Union[str, Timezone, None]) -> DateTime
100+
def now(cls, tz=None): # type: (Optional[Union[str, Timezone]]) -> DateTime
97101
"""
98102
Get a DateTime instance for the current date and time.
99103
"""
@@ -214,21 +218,21 @@ def offset_hours(self):
214218
return self.get_offset() / SECONDS_PER_MINUTE / MINUTES_PER_HOUR
215219

216220
@property
217-
def timezone(self): # type: () -> Union[str, None]
221+
def timezone(self): # type: () -> Optional[Timezone]
218222
if not isinstance(self.tzinfo, Timezone):
219223
return
220224

221225
return self.tzinfo
222226

223227
@property
224-
def tz(self): # type: () -> Union[str, None]
228+
def tz(self): # type: () -> Optional[Timezone]
225229
return self.timezone
226230

227231
@property
228-
def timezone_name(self): # type: () -> Union[str, None]
232+
def timezone_name(self): # type: () -> Optional[str]
229233
tz = self.timezone
230234

231-
if self.timezone is None:
235+
if tz is None:
232236
return None
233237

234238
return tz.name
@@ -255,7 +259,7 @@ def date(self):
255259
def time(self):
256260
return Time(self.hour, self.minute, self.second, self.microsecond)
257261

258-
def naive(self): # type: () -> DateTime
262+
def naive(self): # type: (_D) -> _D
259263
"""
260264
Return the DateTime without timezone information.
261265
"""
@@ -602,7 +606,7 @@ def add(
602606
minutes=0,
603607
seconds=0,
604608
microseconds=0,
605-
): # type: (int, int, int, int, int, int, int) -> DateTime
609+
): # type: (_D, int, int, int, int, int, int, int, int) -> _D
606610
"""
607611
Add a duration to the instance.
608612
@@ -802,10 +806,10 @@ def diff(self, dt=None, abs=True):
802806

803807
def diff_for_humans(
804808
self,
805-
other=None, # type: Union['DateTime', None]
809+
other=None, # type: Optional[DateTime]
806810
absolute=False, # type: bool
807-
locale=None, # type:Union[str, None]
808-
): # type: (...) -> False
811+
locale=None, # type: Optional[str]
812+
): # type: (...) -> str
809813
"""
810814
Get the difference in a human readable format in the current locale.
811815

pendulum/formatting/difference_formatter.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from pendulum.utils._compat import decode
22

33
from ..locales.locale import Locale
4+
from ..period import Period
5+
6+
import typing
47

58

69
class DifferenceFormatter(object):
@@ -11,7 +14,9 @@ class DifferenceFormatter(object):
1114
def __init__(self, locale="en"):
1215
self._locale = Locale.load(locale)
1316

14-
def format(self, diff, is_now=True, absolute=False, locale=None):
17+
def format(
18+
self, diff, is_now=True, absolute=False, locale=None
19+
): # type: (Period, bool, bool, typing.Optional[str]) -> str
1520
"""
1621
Formats a difference.
1722

pendulum/formatting/formatter.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,9 @@ class Formatter:
229229
"z": str,
230230
}
231231

232-
def format(self, dt, fmt, locale=None):
232+
def format(
233+
self, dt, fmt, locale=None
234+
): # type: (pendulum.DateTime, str, typing.Optional[typing.Union[str, Locale]]) -> str
233235
"""
234236
Formats a DateTime instance with a given format and locale.
235237
@@ -260,7 +262,9 @@ def format(self, dt, fmt, locale=None):
260262

261263
return decode(result)
262264

263-
def _format_token(self, dt, token, locale):
265+
def _format_token(
266+
self, dt, token, locale
267+
): # type: (pendulum.DateTime, str, Locale) -> str
264268
"""
265269
Formats a DateTime instance with a given token and locale.
266270
@@ -306,7 +310,9 @@ def _format_token(self, dt, token, locale):
306310

307311
return "{}{:02d}{}{:02d}".format(sign, hour, separator, minute)
308312

309-
def _format_localizable_token(self, dt, token, locale):
313+
def _format_localizable_token(
314+
self, dt, token, locale
315+
): # type: (pendulum.DateTime, str, Locale) -> str
310316
"""
311317
Formats a DateTime instance
312318
with a given localizable token and locale.
@@ -360,8 +366,8 @@ def parse(
360366
time, # type: str
361367
fmt, # type: str
362368
now, # type: pendulum.DateTime
363-
locale=None, # type: typing.Union[str, None]
364-
): # type: (...) -> dict
369+
locale=None, # type: typing.Optional[str]
370+
): # type: (...) -> typing.Dict[str, typing.Any]
365371
"""
366372
Parses a time string matching a given format as a tuple.
367373
@@ -410,7 +416,9 @@ def parse(
410416

411417
return self._check_parsed(parsed, now)
412418

413-
def _check_parsed(self, parsed, now): # type: (dict, pendulum.DateTime) -> dict
419+
def _check_parsed(
420+
self, parsed, now
421+
): # type: (typing.Dict[str, typing.Any], pendulum.DateTime) -> typing.Dict[str, typing.Any]
414422
"""
415423
Checks validity of parsed elements.
416424
@@ -530,7 +538,7 @@ def _check_parsed(self, parsed, now): # type: (dict, pendulum.DateTime) -> dict
530538

531539
def _get_parsed_values(
532540
self, m, parsed, locale, now
533-
): # type: (..., dict, Locale, pendulum.DateTime) -> None
541+
): # type: (typing.Match[str], typing.Dict[str, typing.Any], Locale, pendulum.DateTime) -> None
534542
for token, index in m.re.groupindex.items():
535543
if token in self._LOCALIZABLE_TOKENS:
536544
self._get_parsed_locale_value(token, m.group(index), parsed, locale)
@@ -539,7 +547,7 @@ def _get_parsed_values(
539547

540548
def _get_parsed_value(
541549
self, token, value, parsed, now
542-
): # type: (str, str, dict, pendulum.DateTime) -> None
550+
): # type: (str, str, typing.Dict[str, typing.Any], pendulum.DateTime) -> None
543551
parsed_token = self._PARSE_TOKENS[token](value)
544552

545553
if "Y" in token:
@@ -599,7 +607,7 @@ def _get_parsed_value(
599607

600608
def _get_parsed_locale_value(
601609
self, token, value, parsed, locale
602-
): # type: (str, str, dict, Locale) -> None
610+
): # type: (str, str, typing.Dict[str, typing.Any], Locale) -> None
603611
if token == "MMMM":
604612
unit = "month"
605613
match = "months.wide"

0 commit comments

Comments
 (0)