@@ -1217,60 +1217,31 @@ def __hash__(self):
1217
1217
def __eq__ (self , other : object ) -> bool :
1218
1218
"""``==`` comparison with :class:`.Date` or :class:`datetime.date`."""
1219
1219
if not isinstance (other , (Date , _date )):
1220
- # TODO: 6.0 - return NotImplemented for non-Date objects
1221
- # return NotImplemented
1222
- return False
1220
+ return NotImplemented
1223
1221
return self .toordinal () == other .toordinal ()
1224
1222
1225
- def __ne__ (self , other : object ) -> bool :
1226
- """``!=`` comparison with :class:`.Date` or :class:`datetime.date`."""
1227
- # TODO: 6.0 - return NotImplemented for non-Date objects
1228
- # if not isinstance(other, (Date, date)):
1229
- # return NotImplemented
1230
- return not self .__eq__ (other )
1231
-
1232
1223
def __lt__ (self , other : Date | _date ) -> bool :
1233
1224
"""``<`` comparison with :class:`.Date` or :class:`datetime.date`."""
1234
1225
if not isinstance (other , (Date , _date )):
1235
- # TODO: 6.0 - return NotImplemented for non-Date objects
1236
- # return NotImplemented
1237
- raise TypeError (
1238
- "'<' not supported between instances of 'Date' and "
1239
- f"{ type (other ).__name__ !r} "
1240
- )
1226
+ return NotImplemented
1241
1227
return self .toordinal () < other .toordinal ()
1242
1228
1243
1229
def __le__ (self , other : Date | _date ) -> bool :
1244
1230
"""``<=`` comparison with :class:`.Date` or :class:`datetime.date`."""
1245
1231
if not isinstance (other , (Date , _date )):
1246
- # TODO: 6.0 - return NotImplemented for non-Date objects
1247
- # return NotImplemented
1248
- raise TypeError (
1249
- "'<=' not supported between instances of 'Date' and "
1250
- f"{ type (other ).__name__ !r} "
1251
- )
1232
+ return NotImplemented
1252
1233
return self .toordinal () <= other .toordinal ()
1253
1234
1254
1235
def __ge__ (self , other : Date | _date ) -> bool :
1255
1236
"""``>=`` comparison with :class:`.Date` or :class:`datetime.date`."""
1256
1237
if not isinstance (other , (Date , _date )):
1257
- # TODO: 6.0 - return NotImplemented for non-Date objects
1258
- # return NotImplemented
1259
- raise TypeError (
1260
- "'>=' not supported between instances of 'Date' and "
1261
- f"{ type (other ).__name__ !r} "
1262
- )
1238
+ return NotImplemented
1263
1239
return self .toordinal () >= other .toordinal ()
1264
1240
1265
1241
def __gt__ (self , other : Date | _date ) -> bool :
1266
1242
"""``>`` comparison with :class:`.Date` or :class:`datetime.date`."""
1267
1243
if not isinstance (other , (Date , _date )):
1268
- # TODO: 6.0 - return NotImplemented for non-Date objects
1269
- # return NotImplemented
1270
- raise TypeError (
1271
- "'>' not supported between instances of 'Date' and "
1272
- f"{ type (other ).__name__ !r} "
1273
- )
1244
+ return NotImplemented
1274
1245
return self .toordinal () > other .toordinal ()
1275
1246
1276
1247
def __add__ (self , other : Duration ) -> Date : # type: ignore[override]
@@ -1917,29 +1888,37 @@ def tzinfo(self) -> _tzinfo | None:
1917
1888
1918
1889
# OPERATIONS #
1919
1890
1920
- def _get_both_normalized_ticks (self , other : object , strict = True ):
1921
- if isinstance (other , (_time , Time )) and (
1922
- (self .utc_offset () is None ) ^ (other .utcoffset () is None )
1923
- ):
1891
+ @_t .overload
1892
+ def _get_both_normalized_ticks (
1893
+ self , other : Time | _time , strict : _t .Literal [True ] = True
1894
+ ) -> tuple [int , int ]: ...
1895
+
1896
+ @_t .overload
1897
+ def _get_both_normalized_ticks (
1898
+ self , other : Time | _time , strict : _t .Literal [False ]
1899
+ ) -> tuple [int , int ] | None : ...
1900
+
1901
+ def _get_both_normalized_ticks (
1902
+ self , other : Time | _time , strict : bool = True
1903
+ ) -> tuple [int , int ] | None :
1904
+ if (self .utc_offset () is None ) ^ (other .utcoffset () is None ):
1924
1905
if strict :
1925
1906
raise TypeError (
1926
1907
"can't compare offset-naive and offset-aware times"
1927
1908
)
1928
1909
else :
1929
- return None , None
1910
+ return None
1930
1911
other_ticks : int
1931
1912
if isinstance (other , Time ):
1932
1913
other_ticks = other .__ticks
1933
- elif isinstance (other , _time ):
1914
+ else :
1915
+ assert isinstance (other , _time )
1934
1916
other_ticks = int (
1935
1917
3600000000000 * other .hour
1936
1918
+ 60000000000 * other .minute
1937
1919
+ _NANO_SECONDS * other .second
1938
1920
+ 1000 * other .microsecond
1939
1921
)
1940
- else :
1941
- return None , None
1942
- assert isinstance (other , (Time , _time ))
1943
1922
utc_offset : _timedelta | None = other .utcoffset ()
1944
1923
if utc_offset is not None :
1945
1924
other_ticks -= int (utc_offset .total_seconds () * _NANO_SECONDS )
@@ -1959,43 +1938,40 @@ def __hash__(self):
1959
1938
1960
1939
def __eq__ (self , other : object ) -> bool :
1961
1940
"""`==` comparison with :class:`.Time` or :class:`datetime.time`."""
1962
- self_ticks , other_ticks = self . _get_both_normalized_ticks (
1963
- other , strict = False
1964
- )
1965
- if self_ticks is None :
1941
+ if not isinstance ( other , ( Time , _time )):
1942
+ return NotImplemented
1943
+ ticks = self . _get_both_normalized_ticks ( other , strict = False )
1944
+ if ticks is None :
1966
1945
return False
1946
+ self_ticks , other_ticks = ticks
1967
1947
return self_ticks == other_ticks
1968
1948
1969
- def __ne__ (self , other : object ) -> bool :
1970
- """`!=` comparison with :class:`.Time` or :class:`datetime.time`."""
1971
- return not self .__eq__ (other )
1972
-
1973
1949
def __lt__ (self , other : Time | _time ) -> bool :
1974
1950
"""`<` comparison with :class:`.Time` or :class:`datetime.time`."""
1975
- self_ticks , other_ticks = self ._get_both_normalized_ticks (other )
1976
- if self_ticks is None :
1951
+ if not isinstance (other , (Time , _time )):
1977
1952
return NotImplemented
1953
+ self_ticks , other_ticks = self ._get_both_normalized_ticks (other )
1978
1954
return self_ticks < other_ticks
1979
1955
1980
1956
def __le__ (self , other : Time | _time ) -> bool :
1981
1957
"""`<=` comparison with :class:`.Time` or :class:`datetime.time`."""
1982
- self_ticks , other_ticks = self ._get_both_normalized_ticks (other )
1983
- if self_ticks is None :
1958
+ if not isinstance (other , (Time , _time )):
1984
1959
return NotImplemented
1960
+ self_ticks , other_ticks = self ._get_both_normalized_ticks (other )
1985
1961
return self_ticks <= other_ticks
1986
1962
1987
1963
def __ge__ (self , other : Time | _time ) -> bool :
1988
1964
"""`>=` comparison with :class:`.Time` or :class:`datetime.time`."""
1989
- self_ticks , other_ticks = self ._get_both_normalized_ticks (other )
1990
- if self_ticks is None :
1965
+ if not isinstance (other , (Time , _time )):
1991
1966
return NotImplemented
1967
+ self_ticks , other_ticks = self ._get_both_normalized_ticks (other )
1992
1968
return self_ticks >= other_ticks
1993
1969
1994
1970
def __gt__ (self , other : Time | _time ) -> bool :
1995
1971
"""`>` comparison with :class:`.Time` or :class:`datetime.time`."""
1996
- self_ticks , other_ticks = self ._get_both_normalized_ticks (other )
1997
- if self_ticks is None :
1972
+ if not isinstance (other , (Time , _time )):
1998
1973
return NotImplemented
1974
+ self_ticks , other_ticks = self ._get_both_normalized_ticks (other )
1999
1975
return self_ticks > other_ticks
2000
1976
2001
1977
# INSTANCE METHODS #
@@ -2603,29 +2579,36 @@ def hour_minute_second_nanosecond(self) -> tuple[int, int, int, int]:
2603
2579
2604
2580
# OPERATIONS #
2605
2581
2606
- def _get_both_normalized (self , other , strict = True ):
2607
- if isinstance (other , (_datetime , DateTime )) and (
2608
- (self .utc_offset () is None ) ^ (other .utcoffset () is None )
2609
- ):
2582
+ @_t .overload
2583
+ def _get_both_normalized (
2584
+ self , other : _datetime | DateTime , strict : _t .Literal [True ] = True
2585
+ ) -> tuple [DateTime , DateTime | _datetime ]: ...
2586
+
2587
+ @_t .overload
2588
+ def _get_both_normalized (
2589
+ self , other : _datetime | DateTime , strict : _t .Literal [False ]
2590
+ ) -> tuple [DateTime , DateTime | _datetime ] | None : ...
2591
+
2592
+ def _get_both_normalized (
2593
+ self , other : _datetime | DateTime , strict : bool = True
2594
+ ) -> tuple [DateTime , DateTime | _datetime ] | None :
2595
+ if (self .utc_offset () is None ) ^ (other .utcoffset () is None ):
2610
2596
if strict :
2611
2597
raise TypeError (
2612
2598
"can't compare offset-naive and offset-aware datetimes"
2613
2599
)
2614
2600
else :
2615
- return None , None
2601
+ return None
2616
2602
self_norm = self
2617
2603
utc_offset = self .utc_offset ()
2618
2604
if utc_offset is not None :
2619
2605
self_norm -= utc_offset
2620
2606
self_norm = self_norm .replace (tzinfo = None )
2621
2607
other_norm = other
2622
- if isinstance (other , (_datetime , DateTime )):
2623
- utc_offset = other .utcoffset ()
2624
- if utc_offset is not None :
2625
- other_norm -= utc_offset
2626
- other_norm = other_norm .replace (tzinfo = None )
2627
- else :
2628
- return None , None
2608
+ utc_offset = other .utcoffset ()
2609
+ if utc_offset is not None :
2610
+ other_norm -= utc_offset
2611
+ other_norm = other_norm .replace (tzinfo = None )
2629
2612
return self_norm , other_norm
2630
2613
2631
2614
def __hash__ (self ):
@@ -2647,21 +2630,12 @@ def __eq__(self, other: object) -> bool:
2647
2630
return NotImplemented
2648
2631
if self .utc_offset () == other .utcoffset ():
2649
2632
return self .date () == other .date () and self .time () == other .time ()
2650
- self_norm , other_norm = self ._get_both_normalized (other , strict = False )
2651
- if self_norm is None :
2633
+ normalized = self ._get_both_normalized (other , strict = False )
2634
+ if normalized is None :
2652
2635
return False
2636
+ self_norm , other_norm = normalized
2653
2637
return self_norm == other_norm
2654
2638
2655
- def __ne__ (self , other : object ) -> bool :
2656
- """
2657
- ``!=`` comparison with another datetime.
2658
-
2659
- Accepts :class:`.DateTime` and :class:`datetime.datetime`.
2660
- """
2661
- if not isinstance (other , (DateTime , _datetime )):
2662
- return NotImplemented
2663
- return not self .__eq__ (other )
2664
-
2665
2639
def __lt__ ( # type: ignore[override]
2666
2640
self , other : _datetime | DateTime
2667
2641
) -> bool :
0 commit comments