@@ -621,7 +621,7 @@ class date:
621
621
622
622
"""
623
623
624
- def __new__ (cls , year , month , day ) :
624
+ def __new__ (cls , year : int , month : int , day : int ) -> "date" :
625
625
"""Creates a new date object.
626
626
627
627
:param int year: Year within range, MINYEAR <= year <= MAXYEAR
@@ -638,31 +638,31 @@ def __new__(cls, year, month, day):
638
638
639
639
# Instance attributes (read-only)
640
640
@property
641
- def year (self ):
641
+ def year (self ) -> int :
642
642
"""Between MINYEAR and MAXYEAR inclusive."""
643
643
return self ._year
644
644
645
645
@property
646
- def month (self ):
646
+ def month (self ) -> int :
647
647
"""Between 1 and 12 inclusive."""
648
648
return self ._month
649
649
650
650
@property
651
- def day (self ):
651
+ def day (self ) -> int :
652
652
"""Between 1 and the number of days in the given month of the given year."""
653
653
return self ._day
654
654
655
655
# Class Methods
656
656
@classmethod
657
- def fromtimestamp (cls , t ) :
657
+ def fromtimestamp (cls , t : float ) -> "date" :
658
658
"""Return the local date corresponding to the POSIX timestamp,
659
659
such as is returned by time.time().
660
660
"""
661
661
tm_struct = _time .localtime (t )
662
662
return cls (tm_struct [0 ], tm_struct [1 ], tm_struct [2 ])
663
663
664
664
@classmethod
665
- def fromordinal (cls , ordinal ) :
665
+ def fromordinal (cls , ordinal : int ) -> "date" :
666
666
"""Return the date corresponding to the proleptic Gregorian ordinal,
667
667
where January 1 of year 1 has ordinal 1.
668
668
@@ -673,7 +673,7 @@ def fromordinal(cls, ordinal):
673
673
return cls (y , m , d )
674
674
675
675
@classmethod
676
- def fromisoformat (cls , date_string ) :
676
+ def fromisoformat (cls , date_string : str ) -> "date" :
677
677
"""Return a date object constructed from an ISO date format.
678
678
Valid format is ``YYYY-MM-DD``
679
679
@@ -687,12 +687,12 @@ def fromisoformat(cls, date_string):
687
687
raise ValueError (_INVALID_ISO_ERROR .format (date_string ))
688
688
689
689
@classmethod
690
- def today (cls ):
690
+ def today (cls ) -> "date" :
691
691
"""Return the current local date."""
692
692
return cls .fromtimestamp (_time .time ())
693
693
694
694
# 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 ):
696
696
"""Return a date with the same value, except for those parameters
697
697
given new values by whichever keyword arguments are specified.
698
698
If no keyword arguments are specified - values are obtained from
@@ -701,36 +701,36 @@ def replace(self, year=None, month=None, day=None):
701
701
"""
702
702
raise NotImplementedError ()
703
703
704
- def timetuple (self ):
704
+ def timetuple (self ) -> _time . struct_time :
705
705
"""Return a time.struct_time such as returned by time.localtime().
706
706
The hours, minutes and seconds are 0, and the DST flag is -1.
707
707
708
708
"""
709
709
return _build_struct_time (self ._year , self ._month , self ._day , 0 , 0 , 0 , - 1 )
710
710
711
- def toordinal (self ):
711
+ def toordinal (self ) -> int :
712
712
"""Return the proleptic Gregorian ordinal of the date, where January 1 of
713
713
year 1 has ordinal 1.
714
714
"""
715
715
return _ymd2ord (self ._year , self ._month , self ._day )
716
716
717
- def weekday (self ):
717
+ def weekday (self ) -> int :
718
718
"""Return the day of the week as an integer, where Monday is 0 and Sunday is 6."""
719
719
return (self .toordinal () + 6 ) % 7
720
720
721
721
# ISO date
722
- def isoweekday (self ):
722
+ def isoweekday (self ) -> int :
723
723
"""Return the day of the week as an integer, where Monday is 1 and Sunday is 7."""
724
724
return self .toordinal () % 7 or 7
725
725
726
- def isoformat (self ):
726
+ def isoformat (self ) -> str :
727
727
"""Return a string representing the date in ISO 8601 format, YYYY-MM-DD:"""
728
728
return "%04d-%02d-%02d" % (self ._year , self ._month , self ._day )
729
729
730
730
# For a date d, str(d) is equivalent to d.isoformat()
731
731
__str__ = isoformat
732
732
733
- def __repr__ (self ):
733
+ def __repr__ (self ) -> str :
734
734
"""Convert to formal string, for repr()."""
735
735
return "%s(%d, %d, %d)" % (
736
736
"datetime." + self .__class__ .__name__ ,
@@ -740,48 +740,48 @@ def __repr__(self):
740
740
)
741
741
742
742
# Supported comparisons
743
- def __eq__ (self , other ) :
743
+ def __eq__ (self , other : "date" ) -> bool :
744
744
if isinstance (other , date ):
745
745
return self ._cmp (other ) == 0
746
746
return NotImplemented
747
747
748
- def __le__ (self , other ) :
748
+ def __le__ (self , other : "date" ) -> bool :
749
749
if isinstance (other , date ):
750
750
return self ._cmp (other ) <= 0
751
751
return NotImplemented
752
752
753
- def __lt__ (self , other ) :
753
+ def __lt__ (self , other : "date" ) -> bool :
754
754
if isinstance (other , date ):
755
755
return self ._cmp (other ) < 0
756
756
return NotImplemented
757
757
758
- def __ge__ (self , other ) :
758
+ def __ge__ (self , other : "date" ) -> bool :
759
759
if isinstance (other , date ):
760
760
return self ._cmp (other ) >= 0
761
761
return NotImplemented
762
762
763
- def __gt__ (self , other ) :
763
+ def __gt__ (self , other : "date" ) -> bool :
764
764
if isinstance (other , date ):
765
765
return self ._cmp (other ) > 0
766
766
return NotImplemented
767
767
768
- def _cmp (self , other ) :
768
+ def _cmp (self , other : "date" ) -> int :
769
769
assert isinstance (other , date )
770
770
y , m , d = self ._year , self ._month , self ._day
771
771
y2 , m2 , d2 = other .year , other .month , other .day
772
772
return _cmp ((y , m , d ), (y2 , m2 , d2 ))
773
773
774
- def __hash__ (self ):
774
+ def __hash__ (self ) -> int :
775
775
if self ._hashcode == - 1 :
776
776
self ._hashcode = hash (self ._getstate ())
777
777
return self ._hashcode
778
778
779
779
# Pickle support
780
- def _getstate (self ):
780
+ def _getstate (self ) -> Tuple [ bytes ] :
781
781
yhi , ylo = divmod (self ._year , 256 )
782
782
return (bytes ([yhi , ylo , self ._month , self ._day ]),)
783
783
784
- def _setstate (self , string ) :
784
+ def _setstate (self , string : bytes ) -> None :
785
785
yhi , ylo , self ._month , self ._day = string
786
786
self ._year = yhi * 256 + ylo
787
787
0 commit comments