Skip to content

Commit a0dca86

Browse files
committed
Add type annotations for datetime
1 parent 0a14e5f commit a0dca86

File tree

1 file changed

+58
-58
lines changed

1 file changed

+58
-58
lines changed

adafruit_datetime.py

Lines changed: 58 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,17 +1211,17 @@ class datetime(date):
12111211
# pylint: disable=redefined-outer-name
12121212
def __new__(
12131213
cls,
1214-
year,
1215-
month,
1216-
day,
1217-
hour=0,
1218-
minute=0,
1219-
second=0,
1220-
microsecond=0,
1221-
tzinfo=None,
1214+
year: int,
1215+
month: int,
1216+
day: int,
1217+
hour: int = 0,
1218+
minute: int = 0,
1219+
second: int = 0,
1220+
microsecond: int = 0,
1221+
tzinfo: Optional[tzinfo] = None,
12221222
*,
1223-
fold=0
1224-
):
1223+
fold: int = 0
1224+
) -> "datetime":
12251225
_check_date_fields(year, month, day)
12261226
_check_time_fields(hour, minute, second, microsecond, fold)
12271227
_check_tzinfo_arg(tzinfo)
@@ -1241,57 +1241,57 @@ def __new__(
12411241

12421242
# Read-only instance attributes
12431243
@property
1244-
def year(self):
1244+
def year(self) -> int:
12451245
"""Between MINYEAR and MAXYEAR inclusive."""
12461246
return self._year
12471247

12481248
@property
1249-
def month(self):
1249+
def month(self) -> int:
12501250
"""Between 1 and 12 inclusive."""
12511251
return self._month
12521252

12531253
@property
1254-
def day(self):
1254+
def day(self) -> int:
12551255
"""Between 1 and the number of days in the given month of the given year."""
12561256
return self._day
12571257

12581258
@property
1259-
def hour(self):
1259+
def hour(self) -> int:
12601260
"""In range(24)."""
12611261
return self._hour
12621262

12631263
@property
1264-
def minute(self):
1264+
def minute(self) -> int:
12651265
"""In range (60)"""
12661266
return self._minute
12671267

12681268
@property
1269-
def second(self):
1269+
def second(self) -> int:
12701270
"""In range (60)"""
12711271
return self._second
12721272

12731273
@property
1274-
def microsecond(self):
1274+
def microsecond(self) -> int:
12751275
"""In range (1000000)"""
12761276
return self._microsecond
12771277

12781278
@property
1279-
def tzinfo(self):
1279+
def tzinfo(self) -> Optional[tzinfo]:
12801280
"""The object passed as the tzinfo argument to the datetime constructor,
12811281
or None if none was passed.
12821282
"""
12831283
return self._tzinfo
12841284

12851285
@property
1286-
def fold(self):
1286+
def fold(self) -> int:
12871287
"""Fold."""
12881288
return self._fold
12891289

12901290
# Class methods
12911291

12921292
# pylint: disable=protected-access
12931293
@classmethod
1294-
def _fromtimestamp(cls, t, utc, tz):
1294+
def _fromtimestamp(cls, t: float, utc: bool, tz: Optional["tzinfo"]) -> "datetime":
12951295
"""Construct a datetime from a POSIX timestamp (like time.time()).
12961296
A timezone info object may be passed in as well.
12971297
"""
@@ -1330,11 +1330,11 @@ def _fromtimestamp(cls, t, utc, tz):
13301330

13311331
## pylint: disable=arguments-differ
13321332
@classmethod
1333-
def fromtimestamp(cls, timestamp, tz=None):
1333+
def fromtimestamp(cls, timestamp: float, tz: Optional["tzinfo"] = None) -> "datetime":
13341334
return cls._fromtimestamp(timestamp, tz is not None, tz)
13351335

13361336
@classmethod
1337-
def fromisoformat(cls, date_string):
1337+
def fromisoformat(cls, date_string: str) -> "datetime":
13381338
"""Return a datetime object constructed from an ISO date format.
13391339
Valid format is ``YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]``
13401340
@@ -1357,17 +1357,17 @@ def fromisoformat(cls, date_string):
13571357
return cls.combine(dateval, timeval)
13581358

13591359
@classmethod
1360-
def now(cls, timezone=None):
1360+
def now(cls, timezone: Optional["tzinfo"]=None) -> "datetime":
13611361
"""Return the current local date and time."""
13621362
return cls.fromtimestamp(_time.time(), tz=timezone)
13631363

13641364
@classmethod
1365-
def utcfromtimestamp(cls, timestamp):
1365+
def utcfromtimestamp(cls, timestamp: float) -> "datetime":
13661366
"""Return the UTC datetime corresponding to the POSIX timestamp, with tzinfo None"""
13671367
return cls._fromtimestamp(timestamp, True, None)
13681368

13691369
@classmethod
1370-
def combine(cls, date, time, tzinfo=True):
1370+
def combine(cls, date: date, time: time, tzinfo: bool = True) -> "datetime":
13711371
"""Return a new datetime object whose date components are equal to the
13721372
given date object’s, and whose time components are equal to the given time object’s.
13731373
@@ -1391,7 +1391,7 @@ def combine(cls, date, time, tzinfo=True):
13911391
)
13921392

13931393
# Instance methods
1394-
def _mktime(self):
1394+
def _mktime(self) -> int:
13951395
"""Return integer POSIX timestamp."""
13961396
epoch = datetime(1970, 1, 1)
13971397
max_fold_seconds = 24 * 3600
@@ -1426,11 +1426,11 @@ def local(u):
14261426
# a solution. This means t is in the gap.
14271427
return (max, min)[self._fold](u1, u2)
14281428

1429-
def date(self):
1429+
def date(self) -> date:
14301430
"""Return date object with same year, month and day."""
14311431
return _date_class(self._year, self._month, self._day)
14321432

1433-
def time(self):
1433+
def time(self) -> time:
14341434
"""Return time object with same hour, minute, second, microsecond and fold.
14351435
tzinfo is None. See also method timetz().
14361436
@@ -1439,7 +1439,7 @@ def time(self):
14391439
self._hour, self._minute, self._second, self._microsecond, fold=self._fold
14401440
)
14411441

1442-
def dst(self):
1442+
def dst(self) -> Optional[timedelta]:
14431443
"""If tzinfo is None, returns None, else returns self.tzinfo.dst(self),
14441444
and raises an exception if the latter doesn’t return None or a timedelta
14451445
object with magnitude less than one day.
@@ -1451,7 +1451,7 @@ def dst(self):
14511451
_check_utc_offset("dst", offset)
14521452
return offset
14531453

1454-
def timetuple(self):
1454+
def timetuple(self) -> _time.struct_time:
14551455
"""Return local time tuple compatible with time.localtime()."""
14561456
dst = self.dst()
14571457
if dst is None:
@@ -1464,7 +1464,7 @@ def timetuple(self):
14641464
self.year, self.month, self.day, self.hour, self.minute, self.second, dst
14651465
)
14661466

1467-
def utcoffset(self):
1467+
def utcoffset(self) -> Optional[timedelta]:
14681468
"""If tzinfo is None, returns None, else returns
14691469
self.tzinfo.utcoffset(self), and raises an exception
14701470
if the latter doesn’t return None or a timedelta object
@@ -1477,22 +1477,22 @@ def utcoffset(self):
14771477
_check_utc_offset("utcoffset", offset)
14781478
return offset
14791479

1480-
def toordinal(self):
1480+
def toordinal(self) -> int:
14811481
"""Return the proleptic Gregorian ordinal of the date."""
14821482
return _ymd2ord(self._year, self._month, self._day)
14831483

1484-
def timestamp(self):
1484+
def timestamp(self) -> float:
14851485
"Return POSIX timestamp as float"
14861486
if not self._tzinfo is None:
14871487
return (self - _EPOCH).total_seconds()
14881488
s = self._mktime()
14891489
return s + self.microsecond / 1e6
14901490

1491-
def weekday(self):
1491+
def weekday(self) -> int:
14921492
"""Return the day of the week as an integer, where Monday is 0 and Sunday is 6."""
14931493
return (self.toordinal() + 6) % 7
14941494

1495-
def ctime(self):
1495+
def ctime(self) -> str:
14961496
"Return string representing the datetime."
14971497
weekday = self.toordinal() % 7 or 7
14981498
return "%s %s %2d %02d:%02d:%02d %04d" % (
@@ -1505,7 +1505,7 @@ def ctime(self):
15051505
self._year,
15061506
)
15071507

1508-
def __repr__(self):
1508+
def __repr__(self) -> str:
15091509
"""Convert to formal string, for repr()."""
15101510
L = [
15111511
self._year,
@@ -1527,7 +1527,7 @@ def __repr__(self):
15271527
s = s[:-1] + ", tzinfo=%r" % self._tzinfo + ")"
15281528
return s
15291529

1530-
def isoformat(self, sep="T", timespec="auto"):
1530+
def isoformat(self, sep: str = "T", timespec: str = "auto") -> str:
15311531
"""Return a string representing the date and time in
15321532
ISO8601 format.
15331533
@@ -1548,23 +1548,23 @@ def isoformat(self, sep="T", timespec="auto"):
15481548

15491549
return s
15501550

1551-
def __str__(self):
1551+
def __str__(self) -> str:
15521552
"Convert to string, for str()."
15531553
return self.isoformat(sep=" ")
15541554

15551555
def replace(
15561556
self,
1557-
year=None,
1558-
month=None,
1559-
day=None,
1560-
hour=None,
1561-
minute=None,
1562-
second=None,
1563-
microsecond=None,
1564-
tzinfo=True,
1557+
year: Optional[int] = None,
1558+
month: Optional[str] = None,
1559+
day: Optional[str] = None,
1560+
hour: Optional[str] = None,
1561+
minute: Optional[str] = None,
1562+
second: Optional[str] = None,
1563+
microsecond: Optional[str] = None,
1564+
tzinfo: bool = True,
15651565
*,
1566-
fold=None
1567-
):
1566+
fold: Optional[int] = None
1567+
) -> "datetime":
15681568
"""Return a datetime with the same attributes,
15691569
except for those attributes given new values by
15701570
whichever keyword arguments are specified.
@@ -1593,32 +1593,32 @@ def replace(
15931593
)
15941594

15951595
# Comparisons of datetime objects.
1596-
def __eq__(self, other):
1596+
def __eq__(self, other: Any) -> bool:
15971597
if not isinstance(other, datetime):
15981598
return False
15991599
return self._cmp(other, allow_mixed=True) == 0
16001600

1601-
def __le__(self, other):
1601+
def __le__(self, other: "datetime") -> bool:
16021602
if not isinstance(other, datetime):
16031603
_cmperror(self, other)
16041604
return self._cmp(other) <= 0
16051605

1606-
def __lt__(self, other):
1606+
def __lt__(self, other: "datetime") -> bool:
16071607
if not isinstance(other, datetime):
16081608
_cmperror(self, other)
16091609
return self._cmp(other) < 0
16101610

1611-
def __ge__(self, other):
1611+
def __ge__(self, other: "datetime") -> bool:
16121612
if not isinstance(other, datetime):
16131613
_cmperror(self, other)
16141614
return self._cmp(other) >= 0
16151615

1616-
def __gt__(self, other):
1616+
def __gt__(self, other: "datetime") -> bool:
16171617
if not isinstance(other, datetime):
16181618
_cmperror(self, other)
16191619
return self._cmp(other) > 0
16201620

1621-
def _cmp(self, other, allow_mixed=False):
1621+
def _cmp(self, other: "datetime", allow_mixed: bool = False) -> int:
16221622
assert isinstance(other, datetime)
16231623
mytz = self._tzinfo
16241624
ottz = other.tzinfo
@@ -1667,7 +1667,7 @@ def _cmp(self, other, allow_mixed=False):
16671667
return -1
16681668
return 1 if diff else 0
16691669

1670-
def __add__(self, other):
1670+
def __add__(self, other: timedelta) -> "datetime":
16711671
"Add a datetime and a timedelta."
16721672
if not isinstance(other, timedelta):
16731673
return NotImplemented
@@ -1690,7 +1690,7 @@ def __add__(self, other):
16901690

16911691
__radd__ = __add__
16921692

1693-
def __sub__(self, other):
1693+
def __sub__(self, other: Union["datetime", timedelta]) -> "datetime":
16941694
"Subtract two datetimes, or a datetime and a timedelta."
16951695
if not isinstance(other, datetime):
16961696
if isinstance(other, timedelta):
@@ -1714,7 +1714,7 @@ def __sub__(self, other):
17141714
raise TypeError("cannot mix naive and timezone-aware time")
17151715
return base + otoff - myoff
17161716

1717-
def __hash__(self):
1717+
def __hash__(self) -> int:
17181718
if self._hashcode == -1:
17191719
t = self
17201720
tzoff = t.utcoffset()
@@ -1728,7 +1728,7 @@ def __hash__(self):
17281728
)
17291729
return self._hashcode
17301730

1731-
def _getstate(self):
1731+
def _getstate(self) -> Tuple[bytes]:
17321732
protocol = 3
17331733
yhi, ylo = divmod(self._year, 256)
17341734
us2, us3 = divmod(self._microsecond, 256)

0 commit comments

Comments
 (0)