Skip to content

Commit 9181f37

Browse files
committed
Add type annotations for date
1 parent 8d5d633 commit 9181f37

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

adafruit_datetime.py

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ class date:
621621
622622
"""
623623

624-
def __new__(cls, year, month, day):
624+
def __new__(cls, year: int, month: int, day: int) -> "date":
625625
"""Creates a new date object.
626626
627627
:param int year: Year within range, MINYEAR <= year <= MAXYEAR
@@ -638,31 +638,31 @@ def __new__(cls, year, month, day):
638638

639639
# Instance attributes (read-only)
640640
@property
641-
def year(self):
641+
def year(self) -> int:
642642
"""Between MINYEAR and MAXYEAR inclusive."""
643643
return self._year
644644

645645
@property
646-
def month(self):
646+
def month(self) -> int:
647647
"""Between 1 and 12 inclusive."""
648648
return self._month
649649

650650
@property
651-
def day(self):
651+
def day(self) -> int:
652652
"""Between 1 and the number of days in the given month of the given year."""
653653
return self._day
654654

655655
# Class Methods
656656
@classmethod
657-
def fromtimestamp(cls, t):
657+
def fromtimestamp(cls, t: float) -> "date":
658658
"""Return the local date corresponding to the POSIX timestamp,
659659
such as is returned by time.time().
660660
"""
661661
tm_struct = _time.localtime(t)
662662
return cls(tm_struct[0], tm_struct[1], tm_struct[2])
663663

664664
@classmethod
665-
def fromordinal(cls, ordinal):
665+
def fromordinal(cls, ordinal: int) -> "date":
666666
"""Return the date corresponding to the proleptic Gregorian ordinal,
667667
where January 1 of year 1 has ordinal 1.
668668
@@ -673,7 +673,7 @@ def fromordinal(cls, ordinal):
673673
return cls(y, m, d)
674674

675675
@classmethod
676-
def fromisoformat(cls, date_string):
676+
def fromisoformat(cls, date_string: str) -> "date":
677677
"""Return a date object constructed from an ISO date format.
678678
Valid format is ``YYYY-MM-DD``
679679
@@ -687,12 +687,12 @@ def fromisoformat(cls, date_string):
687687
raise ValueError(_INVALID_ISO_ERROR.format(date_string))
688688

689689
@classmethod
690-
def today(cls):
690+
def today(cls) -> "date":
691691
"""Return the current local date."""
692692
return cls.fromtimestamp(_time.time())
693693

694694
# Instance Methods
695-
def replace(self, year=None, month=None, day=None):
695+
def replace(self, year: Optional[int] = None, month: Optional[int] = None, day: Optional[int] = None):
696696
"""Return a date with the same value, except for those parameters
697697
given new values by whichever keyword arguments are specified.
698698
If no keyword arguments are specified - values are obtained from
@@ -701,36 +701,36 @@ def replace(self, year=None, month=None, day=None):
701701
"""
702702
raise NotImplementedError()
703703

704-
def timetuple(self):
704+
def timetuple(self) -> _time.struct_time:
705705
"""Return a time.struct_time such as returned by time.localtime().
706706
The hours, minutes and seconds are 0, and the DST flag is -1.
707707
708708
"""
709709
return _build_struct_time(self._year, self._month, self._day, 0, 0, 0, -1)
710710

711-
def toordinal(self):
711+
def toordinal(self) -> int:
712712
"""Return the proleptic Gregorian ordinal of the date, where January 1 of
713713
year 1 has ordinal 1.
714714
"""
715715
return _ymd2ord(self._year, self._month, self._day)
716716

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

721721
# ISO date
722-
def isoweekday(self):
722+
def isoweekday(self) -> int:
723723
"""Return the day of the week as an integer, where Monday is 1 and Sunday is 7."""
724724
return self.toordinal() % 7 or 7
725725

726-
def isoformat(self):
726+
def isoformat(self) -> str:
727727
"""Return a string representing the date in ISO 8601 format, YYYY-MM-DD:"""
728728
return "%04d-%02d-%02d" % (self._year, self._month, self._day)
729729

730730
# For a date d, str(d) is equivalent to d.isoformat()
731731
__str__ = isoformat
732732

733-
def __repr__(self):
733+
def __repr__(self) -> str:
734734
"""Convert to formal string, for repr()."""
735735
return "%s(%d, %d, %d)" % (
736736
"datetime." + self.__class__.__name__,
@@ -740,48 +740,48 @@ def __repr__(self):
740740
)
741741

742742
# Supported comparisons
743-
def __eq__(self, other):
743+
def __eq__(self, other: "date") -> bool:
744744
if isinstance(other, date):
745745
return self._cmp(other) == 0
746746
return NotImplemented
747747

748-
def __le__(self, other):
748+
def __le__(self, other: "date") -> bool:
749749
if isinstance(other, date):
750750
return self._cmp(other) <= 0
751751
return NotImplemented
752752

753-
def __lt__(self, other):
753+
def __lt__(self, other: "date") -> bool:
754754
if isinstance(other, date):
755755
return self._cmp(other) < 0
756756
return NotImplemented
757757

758-
def __ge__(self, other):
758+
def __ge__(self, other: "date") -> bool:
759759
if isinstance(other, date):
760760
return self._cmp(other) >= 0
761761
return NotImplemented
762762

763-
def __gt__(self, other):
763+
def __gt__(self, other: "date") -> bool:
764764
if isinstance(other, date):
765765
return self._cmp(other) > 0
766766
return NotImplemented
767767

768-
def _cmp(self, other):
768+
def _cmp(self, other: "date") -> int:
769769
assert isinstance(other, date)
770770
y, m, d = self._year, self._month, self._day
771771
y2, m2, d2 = other.year, other.month, other.day
772772
return _cmp((y, m, d), (y2, m2, d2))
773773

774-
def __hash__(self):
774+
def __hash__(self) -> int:
775775
if self._hashcode == -1:
776776
self._hashcode = hash(self._getstate())
777777
return self._hashcode
778778

779779
# Pickle support
780-
def _getstate(self):
780+
def _getstate(self) -> Tuple[bytes]:
781781
yhi, ylo = divmod(self._year, 256)
782782
return (bytes([yhi, ylo, self._month, self._day]),)
783783

784-
def _setstate(self, string):
784+
def _setstate(self, string: bytes) -> None:
785785
yhi, ylo, self._month, self._day = string
786786
self._year = yhi * 256 + ylo
787787

0 commit comments

Comments
 (0)