33
33
from micropython import const
34
34
35
35
try :
36
- from typing import Any , Union , Optional , Tuple
36
+ from typing import Type , Any , Union , Optional , Tuple
37
+ NotImplementedType = Type [NotImplemented ]
37
38
except ImportError :
38
39
pass
39
40
@@ -311,14 +312,14 @@ class timedelta:
311
312
# pylint: disable=too-many-arguments, too-many-locals, too-many-statements
312
313
def __new__ (
313
314
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" :
322
323
323
324
# Check that all inputs are ints or floats.
324
325
if not all (
@@ -417,22 +418,22 @@ def __new__(
417
418
418
419
# Instance attributes (read-only)
419
420
@property
420
- def days (self ):
421
+ def days (self ) -> int :
421
422
"""Days, Between -999999999 and 999999999 inclusive"""
422
423
return self ._days
423
424
424
425
@property
425
- def seconds (self ):
426
+ def seconds (self ) -> int :
426
427
"""Seconds, Between 0 and 86399 inclusive"""
427
428
return self ._seconds
428
429
429
430
@property
430
- def microseconds (self ):
431
+ def microseconds (self ) -> int :
431
432
"""Microseconds, Between 0 and 999999 inclusive"""
432
433
return self ._microseconds
433
434
434
435
# Instance methods
435
- def total_seconds (self ):
436
+ def total_seconds (self ) -> float :
436
437
"""Return the total number of seconds contained in the duration."""
437
438
# If the duration is less than a threshold duration, and microseconds
438
439
# is nonzero, then the result is a float. Otherwise, the result is a
@@ -444,7 +445,7 @@ def total_seconds(self):
444
445
seconds += self ._microseconds / 10 ** 6
445
446
return seconds
446
447
447
- def __repr__ (self ):
448
+ def __repr__ (self ) -> str :
448
449
args = []
449
450
if self ._days :
450
451
args .append ("days=%d" % self ._days )
@@ -460,7 +461,7 @@ def __repr__(self):
460
461
", " .join (args ),
461
462
)
462
463
463
- def __str__ (self ):
464
+ def __str__ (self ) -> str :
464
465
mm , ss = divmod (self ._seconds , 60 )
465
466
hh , mm = divmod (mm , 60 )
466
467
s = "%d:%02d:%02d" % (hh , mm , ss )
@@ -475,10 +476,10 @@ def plural(n):
475
476
return s
476
477
477
478
# Supported operations
478
- def __neg__ (self ):
479
+ def __neg__ (self ) -> "timedelta" :
479
480
return timedelta (- self ._days , - self ._seconds , - self ._microseconds )
480
481
481
- def __add__ (self , other ) :
482
+ def __add__ (self , other : "timedelta" ) -> "timedelta" :
482
483
if isinstance (other , timedelta ):
483
484
return timedelta (
484
485
self ._days + other ._days ,
@@ -487,7 +488,7 @@ def __add__(self, other):
487
488
)
488
489
return NotImplemented
489
490
490
- def __sub__ (self , other ) :
491
+ def __sub__ (self , other : "timedelta" ) -> "timedelta" :
491
492
if isinstance (other , timedelta ):
492
493
return timedelta (
493
494
self ._days - other ._days ,
@@ -496,30 +497,30 @@ def __sub__(self, other):
496
497
)
497
498
return NotImplemented
498
499
499
- def _to_microseconds (self ):
500
+ def _to_microseconds (self ) -> int :
500
501
return (self ._days * (24 * 3600 ) + self ._seconds ) * 1000000 + self ._microseconds
501
502
502
- def __floordiv__ (self , other ) :
503
+ def __floordiv__ (self , other : Union [ int , "timedelta" ]) -> Union [ int , "timedelta" ] :
503
504
if not isinstance (other , (int , timedelta )):
504
505
return NotImplemented
505
506
usec = self ._to_microseconds ()
506
507
if isinstance (other , timedelta ):
507
508
return usec // other ._to_microseconds ()
508
509
return timedelta (0 , 0 , usec // other )
509
510
510
- def __mod__ (self , other ) :
511
+ def __mod__ (self , other : "timedelta" ) -> "timedelta" :
511
512
if isinstance (other , timedelta ):
512
513
r = self ._to_microseconds () % other ._to_microseconds ()
513
514
return timedelta (0 , 0 , r )
514
515
return NotImplemented
515
516
516
- def __divmod__ (self , other ) :
517
+ def __divmod__ (self , other : "timedelta" ) -> "timedelta" :
517
518
if isinstance (other , timedelta ):
518
519
q , r = divmod (self ._to_microseconds (), other ._to_microseconds ())
519
520
return q , timedelta (0 , 0 , r )
520
521
return NotImplemented
521
522
522
- def __mul__ (self , other ) :
523
+ def __mul__ (self , other : float ) -> "timedelta" :
523
524
if isinstance (other , int ):
524
525
# for CPython compatibility, we cannot use
525
526
# our __class__ here, but need a real timedelta
@@ -536,45 +537,45 @@ def __mul__(self, other):
536
537
__rmul__ = __mul__
537
538
538
539
# Supported comparisons
539
- def __eq__ (self , other ) :
540
+ def __eq__ (self , other : "timedelta" ) -> bool :
540
541
if not isinstance (other , timedelta ):
541
542
return False
542
543
return self ._cmp (other ) == 0
543
544
544
- def __ne__ (self , other ) :
545
+ def __ne__ (self , other : "timedelta" ) -> bool :
545
546
if not isinstance (other , timedelta ):
546
547
return True
547
548
return self ._cmp (other ) != 0
548
549
549
- def __le__ (self , other ) :
550
+ def __le__ (self , other : "timedelta" ) -> bool :
550
551
if not isinstance (other , timedelta ):
551
552
_cmperror (self , other )
552
553
return self ._cmp (other ) <= 0
553
554
554
- def __lt__ (self , other ) :
555
+ def __lt__ (self , other : "timedelta" ) -> bool :
555
556
if not isinstance (other , timedelta ):
556
557
_cmperror (self , other )
557
558
return self ._cmp (other ) < 0
558
559
559
- def __ge__ (self , other ) :
560
+ def __ge__ (self , other : "timedelta" ) -> bool :
560
561
if not isinstance (other , timedelta ):
561
562
_cmperror (self , other )
562
563
return self ._cmp (other ) >= 0
563
564
564
- def __gt__ (self , other ) :
565
+ def __gt__ (self , other : "timedelta" ) -> bool :
565
566
if not isinstance (other , timedelta ):
566
567
_cmperror (self , other )
567
568
return self ._cmp (other ) > 0
568
569
569
570
# pylint: disable=no-self-use, protected-access
570
- def _cmp (self , other ) :
571
+ def _cmp (self , other : "timedelta" ) -> int :
571
572
assert isinstance (other , timedelta )
572
573
return _cmp (self ._getstate (), other ._getstate ())
573
574
574
- def __bool__ (self ):
575
+ def __bool__ (self ) -> bool :
575
576
return self ._days != 0 or self ._seconds != 0 or self ._microseconds != 0
576
577
577
- def _getstate (self ):
578
+ def _getstate (self ) -> Tuple [ int , int , int ] :
578
579
return (self ._days , self ._seconds , self ._microseconds )
579
580
580
581
0 commit comments