Skip to content

Commit 2292ecb

Browse files
committed
Add type annotations for timedelta
1 parent fe5f2ab commit 2292ecb

File tree

1 file changed

+33
-32
lines changed

1 file changed

+33
-32
lines changed

adafruit_datetime.py

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
from micropython import const
3434

3535
try:
36-
from typing import Any, Union, Optional, Tuple
36+
from typing import Type, Any, Union, Optional, Tuple
37+
NotImplementedType = Type[NotImplemented]
3738
except ImportError:
3839
pass
3940

@@ -311,14 +312,14 @@ class timedelta:
311312
# pylint: disable=too-many-arguments, too-many-locals, too-many-statements
312313
def __new__(
313314
cls,
314-
days=0,
315-
seconds=0,
316-
microseconds=0,
317-
milliseconds=0,
318-
minutes=0,
319-
hours=0,
320-
weeks=0,
321-
):
315+
days: int = 0,
316+
seconds:int = 0,
317+
microseconds: int = 0,
318+
milliseconds: int = 0,
319+
minutes: int = 0,
320+
hours: int = 0,
321+
weeks: int = 0,
322+
) -> "timedelta":
322323

323324
# Check that all inputs are ints or floats.
324325
if not all(
@@ -417,22 +418,22 @@ def __new__(
417418

418419
# Instance attributes (read-only)
419420
@property
420-
def days(self):
421+
def days(self) -> int:
421422
"""Days, Between -999999999 and 999999999 inclusive"""
422423
return self._days
423424

424425
@property
425-
def seconds(self):
426+
def seconds(self) -> int:
426427
"""Seconds, Between 0 and 86399 inclusive"""
427428
return self._seconds
428429

429430
@property
430-
def microseconds(self):
431+
def microseconds(self) -> int:
431432
"""Microseconds, Between 0 and 999999 inclusive"""
432433
return self._microseconds
433434

434435
# Instance methods
435-
def total_seconds(self):
436+
def total_seconds(self) -> float:
436437
"""Return the total number of seconds contained in the duration."""
437438
# If the duration is less than a threshold duration, and microseconds
438439
# is nonzero, then the result is a float. Otherwise, the result is a
@@ -444,7 +445,7 @@ def total_seconds(self):
444445
seconds += self._microseconds / 10 ** 6
445446
return seconds
446447

447-
def __repr__(self):
448+
def __repr__(self) -> str:
448449
args = []
449450
if self._days:
450451
args.append("days=%d" % self._days)
@@ -460,7 +461,7 @@ def __repr__(self):
460461
", ".join(args),
461462
)
462463

463-
def __str__(self):
464+
def __str__(self) -> str:
464465
mm, ss = divmod(self._seconds, 60)
465466
hh, mm = divmod(mm, 60)
466467
s = "%d:%02d:%02d" % (hh, mm, ss)
@@ -475,10 +476,10 @@ def plural(n):
475476
return s
476477

477478
# Supported operations
478-
def __neg__(self):
479+
def __neg__(self) -> "timedelta":
479480
return timedelta(-self._days, -self._seconds, -self._microseconds)
480481

481-
def __add__(self, other):
482+
def __add__(self, other: "timedelta") -> "timedelta":
482483
if isinstance(other, timedelta):
483484
return timedelta(
484485
self._days + other._days,
@@ -487,7 +488,7 @@ def __add__(self, other):
487488
)
488489
return NotImplemented
489490

490-
def __sub__(self, other):
491+
def __sub__(self, other: "timedelta") -> "timedelta":
491492
if isinstance(other, timedelta):
492493
return timedelta(
493494
self._days - other._days,
@@ -496,30 +497,30 @@ def __sub__(self, other):
496497
)
497498
return NotImplemented
498499

499-
def _to_microseconds(self):
500+
def _to_microseconds(self) -> int:
500501
return (self._days * (24 * 3600) + self._seconds) * 1000000 + self._microseconds
501502

502-
def __floordiv__(self, other):
503+
def __floordiv__(self, other: Union[int, "timedelta"]) -> Union[int, "timedelta"]:
503504
if not isinstance(other, (int, timedelta)):
504505
return NotImplemented
505506
usec = self._to_microseconds()
506507
if isinstance(other, timedelta):
507508
return usec // other._to_microseconds()
508509
return timedelta(0, 0, usec // other)
509510

510-
def __mod__(self, other):
511+
def __mod__(self, other: "timedelta") -> "timedelta":
511512
if isinstance(other, timedelta):
512513
r = self._to_microseconds() % other._to_microseconds()
513514
return timedelta(0, 0, r)
514515
return NotImplemented
515516

516-
def __divmod__(self, other):
517+
def __divmod__(self, other: "timedelta") -> "timedelta":
517518
if isinstance(other, timedelta):
518519
q, r = divmod(self._to_microseconds(), other._to_microseconds())
519520
return q, timedelta(0, 0, r)
520521
return NotImplemented
521522

522-
def __mul__(self, other):
523+
def __mul__(self, other: float) -> "timedelta":
523524
if isinstance(other, int):
524525
# for CPython compatibility, we cannot use
525526
# our __class__ here, but need a real timedelta
@@ -536,45 +537,45 @@ def __mul__(self, other):
536537
__rmul__ = __mul__
537538

538539
# Supported comparisons
539-
def __eq__(self, other):
540+
def __eq__(self, other: "timedelta") -> bool:
540541
if not isinstance(other, timedelta):
541542
return False
542543
return self._cmp(other) == 0
543544

544-
def __ne__(self, other):
545+
def __ne__(self, other: "timedelta") -> bool:
545546
if not isinstance(other, timedelta):
546547
return True
547548
return self._cmp(other) != 0
548549

549-
def __le__(self, other):
550+
def __le__(self, other: "timedelta") -> bool:
550551
if not isinstance(other, timedelta):
551552
_cmperror(self, other)
552553
return self._cmp(other) <= 0
553554

554-
def __lt__(self, other):
555+
def __lt__(self, other: "timedelta") -> bool:
555556
if not isinstance(other, timedelta):
556557
_cmperror(self, other)
557558
return self._cmp(other) < 0
558559

559-
def __ge__(self, other):
560+
def __ge__(self, other: "timedelta") -> bool:
560561
if not isinstance(other, timedelta):
561562
_cmperror(self, other)
562563
return self._cmp(other) >= 0
563564

564-
def __gt__(self, other):
565+
def __gt__(self, other: "timedelta") -> bool:
565566
if not isinstance(other, timedelta):
566567
_cmperror(self, other)
567568
return self._cmp(other) > 0
568569

569570
# pylint: disable=no-self-use, protected-access
570-
def _cmp(self, other):
571+
def _cmp(self, other: "timedelta") -> int:
571572
assert isinstance(other, timedelta)
572573
return _cmp(self._getstate(), other._getstate())
573574

574-
def __bool__(self):
575+
def __bool__(self) -> bool:
575576
return self._days != 0 or self._seconds != 0 or self._microseconds != 0
576577

577-
def _getstate(self):
578+
def _getstate(self) -> Tuple[int, int, int]:
578579
return (self._days, self._seconds, self._microseconds)
579580

580581

0 commit comments

Comments
 (0)