22
33from datetime import timedelta
44from typing import TYPE_CHECKING
5- from typing import cast
65from typing import overload
76
87import pendulum
@@ -356,6 +355,14 @@ def __neg__(self) -> Self:
356355 def _to_microseconds (self ) -> int :
357356 return (self ._days * (24 * 3600 ) + self ._seconds ) * 1000000 + self ._microseconds
358357
358+ @staticmethod
359+ def _timedelta_microseconds (delta : timedelta ) -> int :
360+ if isinstance (delta , Duration ):
361+ return delta ._to_microseconds ()
362+ return (
363+ delta .days * SECONDS_PER_DAY + delta .seconds
364+ ) * US_PER_SECOND + delta .microseconds
365+
359366 def __mul__ (self , other : int | float ) -> Self :
360367 if isinstance (other , int ):
361368 return self .__class__ (
@@ -386,10 +393,7 @@ def __floordiv__(self, other: int | timedelta) -> int | Duration:
386393
387394 usec = self ._to_microseconds ()
388395 if isinstance (other , timedelta ):
389- return cast (
390- "int" ,
391- usec // other ._to_microseconds (), # type: ignore[attr-defined]
392- )
396+ return usec // self ._timedelta_microseconds (other )
393397
394398 if isinstance (other , int ):
395399 return self .__class__ (
@@ -412,10 +416,7 @@ def __truediv__(self, other: int | float | timedelta) -> Self | float:
412416
413417 usec = self ._to_microseconds ()
414418 if isinstance (other , timedelta ):
415- return cast (
416- "float" ,
417- usec / other ._to_microseconds (), # type: ignore[attr-defined]
418- )
419+ return usec / self ._timedelta_microseconds (other )
419420
420421 if isinstance (other , int ):
421422 return self .__class__ (
@@ -441,7 +442,7 @@ def __truediv__(self, other: int | float | timedelta) -> Self | float:
441442
442443 def __mod__ (self , other : timedelta ) -> Self :
443444 if isinstance (other , timedelta ):
444- r = self ._to_microseconds () % other . _to_microseconds () # type: ignore[attr-defined]
445+ r = self ._to_microseconds () % self . _timedelta_microseconds ( other )
445446
446447 return self .__class__ (0 , 0 , r )
447448
@@ -451,7 +452,7 @@ def __divmod__(self, other: timedelta) -> tuple[int, Duration]:
451452 if isinstance (other , timedelta ):
452453 q , r = divmod (
453454 self ._to_microseconds (),
454- other . _to_microseconds (), # type: ignore[attr-defined]
455+ self . _timedelta_microseconds ( other ),
455456 )
456457
457458 return q , self .__class__ (0 , 0 , r )
0 commit comments