@@ -1211,17 +1211,17 @@ class datetime(date):
1211
1211
# pylint: disable=redefined-outer-name
1212
1212
def __new__ (
1213
1213
cls ,
1214
- year ,
1215
- month ,
1216
- day ,
1217
- hour = 0 ,
1218
- minute = 0 ,
1219
- second = 0 ,
1220
- microsecond = 0 ,
1221
- tzinfo = None ,
1214
+ year : int ,
1215
+ month : int ,
1216
+ day : int ,
1217
+ hour : int = 0 ,
1218
+ minute : int = 0 ,
1219
+ second : int = 0 ,
1220
+ microsecond : int = 0 ,
1221
+ tzinfo : Optional [ tzinfo ] = None ,
1222
1222
* ,
1223
- fold = 0
1224
- ):
1223
+ fold : int = 0
1224
+ ) -> "datetime" :
1225
1225
_check_date_fields (year , month , day )
1226
1226
_check_time_fields (hour , minute , second , microsecond , fold )
1227
1227
_check_tzinfo_arg (tzinfo )
@@ -1241,57 +1241,57 @@ def __new__(
1241
1241
1242
1242
# Read-only instance attributes
1243
1243
@property
1244
- def year (self ):
1244
+ def year (self ) -> int :
1245
1245
"""Between MINYEAR and MAXYEAR inclusive."""
1246
1246
return self ._year
1247
1247
1248
1248
@property
1249
- def month (self ):
1249
+ def month (self ) -> int :
1250
1250
"""Between 1 and 12 inclusive."""
1251
1251
return self ._month
1252
1252
1253
1253
@property
1254
- def day (self ):
1254
+ def day (self ) -> int :
1255
1255
"""Between 1 and the number of days in the given month of the given year."""
1256
1256
return self ._day
1257
1257
1258
1258
@property
1259
- def hour (self ):
1259
+ def hour (self ) -> int :
1260
1260
"""In range(24)."""
1261
1261
return self ._hour
1262
1262
1263
1263
@property
1264
- def minute (self ):
1264
+ def minute (self ) -> int :
1265
1265
"""In range (60)"""
1266
1266
return self ._minute
1267
1267
1268
1268
@property
1269
- def second (self ):
1269
+ def second (self ) -> int :
1270
1270
"""In range (60)"""
1271
1271
return self ._second
1272
1272
1273
1273
@property
1274
- def microsecond (self ):
1274
+ def microsecond (self ) -> int :
1275
1275
"""In range (1000000)"""
1276
1276
return self ._microsecond
1277
1277
1278
1278
@property
1279
- def tzinfo (self ):
1279
+ def tzinfo (self ) -> Optional [ tzinfo ] :
1280
1280
"""The object passed as the tzinfo argument to the datetime constructor,
1281
1281
or None if none was passed.
1282
1282
"""
1283
1283
return self ._tzinfo
1284
1284
1285
1285
@property
1286
- def fold (self ):
1286
+ def fold (self ) -> int :
1287
1287
"""Fold."""
1288
1288
return self ._fold
1289
1289
1290
1290
# Class methods
1291
1291
1292
1292
# pylint: disable=protected-access
1293
1293
@classmethod
1294
- def _fromtimestamp (cls , t , utc , tz ) :
1294
+ def _fromtimestamp (cls , t : float , utc : bool , tz : Optional [ "tzinfo" ]) -> "datetime" :
1295
1295
"""Construct a datetime from a POSIX timestamp (like time.time()).
1296
1296
A timezone info object may be passed in as well.
1297
1297
"""
@@ -1330,11 +1330,11 @@ def _fromtimestamp(cls, t, utc, tz):
1330
1330
1331
1331
## pylint: disable=arguments-differ
1332
1332
@classmethod
1333
- def fromtimestamp (cls , timestamp , tz = None ):
1333
+ def fromtimestamp (cls , timestamp : float , tz : Optional [ "tzinfo" ] = None ) -> "datetime" :
1334
1334
return cls ._fromtimestamp (timestamp , tz is not None , tz )
1335
1335
1336
1336
@classmethod
1337
- def fromisoformat (cls , date_string ) :
1337
+ def fromisoformat (cls , date_string : str ) -> "datetime" :
1338
1338
"""Return a datetime object constructed from an ISO date format.
1339
1339
Valid format is ``YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]]``
1340
1340
@@ -1357,17 +1357,17 @@ def fromisoformat(cls, date_string):
1357
1357
return cls .combine (dateval , timeval )
1358
1358
1359
1359
@classmethod
1360
- def now (cls , timezone = None ):
1360
+ def now (cls , timezone : Optional [ "tzinfo" ] = None ) -> "datetime" :
1361
1361
"""Return the current local date and time."""
1362
1362
return cls .fromtimestamp (_time .time (), tz = timezone )
1363
1363
1364
1364
@classmethod
1365
- def utcfromtimestamp (cls , timestamp ) :
1365
+ def utcfromtimestamp (cls , timestamp : float ) -> "datetime" :
1366
1366
"""Return the UTC datetime corresponding to the POSIX timestamp, with tzinfo None"""
1367
1367
return cls ._fromtimestamp (timestamp , True , None )
1368
1368
1369
1369
@classmethod
1370
- def combine (cls , date , time , tzinfo = True ):
1370
+ def combine (cls , date : date , time : time , tzinfo : bool = True ) -> "datetime" :
1371
1371
"""Return a new datetime object whose date components are equal to the
1372
1372
given date object’s, and whose time components are equal to the given time object’s.
1373
1373
@@ -1391,7 +1391,7 @@ def combine(cls, date, time, tzinfo=True):
1391
1391
)
1392
1392
1393
1393
# Instance methods
1394
- def _mktime (self ):
1394
+ def _mktime (self ) -> int :
1395
1395
"""Return integer POSIX timestamp."""
1396
1396
epoch = datetime (1970 , 1 , 1 )
1397
1397
max_fold_seconds = 24 * 3600
@@ -1426,11 +1426,11 @@ def local(u):
1426
1426
# a solution. This means t is in the gap.
1427
1427
return (max , min )[self ._fold ](u1 , u2 )
1428
1428
1429
- def date (self ):
1429
+ def date (self ) -> date :
1430
1430
"""Return date object with same year, month and day."""
1431
1431
return _date_class (self ._year , self ._month , self ._day )
1432
1432
1433
- def time (self ):
1433
+ def time (self ) -> time :
1434
1434
"""Return time object with same hour, minute, second, microsecond and fold.
1435
1435
tzinfo is None. See also method timetz().
1436
1436
@@ -1439,7 +1439,7 @@ def time(self):
1439
1439
self ._hour , self ._minute , self ._second , self ._microsecond , fold = self ._fold
1440
1440
)
1441
1441
1442
- def dst (self ):
1442
+ def dst (self ) -> Optional [ timedelta ] :
1443
1443
"""If tzinfo is None, returns None, else returns self.tzinfo.dst(self),
1444
1444
and raises an exception if the latter doesn’t return None or a timedelta
1445
1445
object with magnitude less than one day.
@@ -1451,7 +1451,7 @@ def dst(self):
1451
1451
_check_utc_offset ("dst" , offset )
1452
1452
return offset
1453
1453
1454
- def timetuple (self ):
1454
+ def timetuple (self ) -> _time . struct_time :
1455
1455
"""Return local time tuple compatible with time.localtime()."""
1456
1456
dst = self .dst ()
1457
1457
if dst is None :
@@ -1464,7 +1464,7 @@ def timetuple(self):
1464
1464
self .year , self .month , self .day , self .hour , self .minute , self .second , dst
1465
1465
)
1466
1466
1467
- def utcoffset (self ):
1467
+ def utcoffset (self ) -> Optional [ timedelta ] :
1468
1468
"""If tzinfo is None, returns None, else returns
1469
1469
self.tzinfo.utcoffset(self), and raises an exception
1470
1470
if the latter doesn’t return None or a timedelta object
@@ -1477,22 +1477,22 @@ def utcoffset(self):
1477
1477
_check_utc_offset ("utcoffset" , offset )
1478
1478
return offset
1479
1479
1480
- def toordinal (self ):
1480
+ def toordinal (self ) -> int :
1481
1481
"""Return the proleptic Gregorian ordinal of the date."""
1482
1482
return _ymd2ord (self ._year , self ._month , self ._day )
1483
1483
1484
- def timestamp (self ):
1484
+ def timestamp (self ) -> float :
1485
1485
"Return POSIX timestamp as float"
1486
1486
if not self ._tzinfo is None :
1487
1487
return (self - _EPOCH ).total_seconds ()
1488
1488
s = self ._mktime ()
1489
1489
return s + self .microsecond / 1e6
1490
1490
1491
- def weekday (self ):
1491
+ def weekday (self ) -> int :
1492
1492
"""Return the day of the week as an integer, where Monday is 0 and Sunday is 6."""
1493
1493
return (self .toordinal () + 6 ) % 7
1494
1494
1495
- def ctime (self ):
1495
+ def ctime (self ) -> str :
1496
1496
"Return string representing the datetime."
1497
1497
weekday = self .toordinal () % 7 or 7
1498
1498
return "%s %s %2d %02d:%02d:%02d %04d" % (
@@ -1505,7 +1505,7 @@ def ctime(self):
1505
1505
self ._year ,
1506
1506
)
1507
1507
1508
- def __repr__ (self ):
1508
+ def __repr__ (self ) -> str :
1509
1509
"""Convert to formal string, for repr()."""
1510
1510
L = [
1511
1511
self ._year ,
@@ -1527,7 +1527,7 @@ def __repr__(self):
1527
1527
s = s [:- 1 ] + ", tzinfo=%r" % self ._tzinfo + ")"
1528
1528
return s
1529
1529
1530
- def isoformat (self , sep = "T" , timespec = "auto" ):
1530
+ def isoformat (self , sep : str = "T" , timespec : str = "auto" ) -> str :
1531
1531
"""Return a string representing the date and time in
1532
1532
ISO8601 format.
1533
1533
@@ -1548,23 +1548,23 @@ def isoformat(self, sep="T", timespec="auto"):
1548
1548
1549
1549
return s
1550
1550
1551
- def __str__ (self ):
1551
+ def __str__ (self ) -> str :
1552
1552
"Convert to string, for str()."
1553
1553
return self .isoformat (sep = " " )
1554
1554
1555
1555
def replace (
1556
1556
self ,
1557
- year = None ,
1558
- month = None ,
1559
- day = None ,
1560
- hour = None ,
1561
- minute = None ,
1562
- second = None ,
1563
- microsecond = None ,
1564
- tzinfo = True ,
1557
+ year : Optional [ int ] = None ,
1558
+ month : Optional [ str ] = None ,
1559
+ day : Optional [ str ] = None ,
1560
+ hour : Optional [ str ] = None ,
1561
+ minute : Optional [ str ] = None ,
1562
+ second : Optional [ str ] = None ,
1563
+ microsecond : Optional [ str ] = None ,
1564
+ tzinfo : bool = True ,
1565
1565
* ,
1566
- fold = None
1567
- ):
1566
+ fold : Optional [ int ] = None
1567
+ ) -> "datetime" :
1568
1568
"""Return a datetime with the same attributes,
1569
1569
except for those attributes given new values by
1570
1570
whichever keyword arguments are specified.
@@ -1593,32 +1593,32 @@ def replace(
1593
1593
)
1594
1594
1595
1595
# Comparisons of datetime objects.
1596
- def __eq__ (self , other ) :
1596
+ def __eq__ (self , other : Any ) -> bool :
1597
1597
if not isinstance (other , datetime ):
1598
1598
return False
1599
1599
return self ._cmp (other , allow_mixed = True ) == 0
1600
1600
1601
- def __le__ (self , other ) :
1601
+ def __le__ (self , other : "datetime" ) -> bool :
1602
1602
if not isinstance (other , datetime ):
1603
1603
_cmperror (self , other )
1604
1604
return self ._cmp (other ) <= 0
1605
1605
1606
- def __lt__ (self , other ) :
1606
+ def __lt__ (self , other : "datetime" ) -> bool :
1607
1607
if not isinstance (other , datetime ):
1608
1608
_cmperror (self , other )
1609
1609
return self ._cmp (other ) < 0
1610
1610
1611
- def __ge__ (self , other ) :
1611
+ def __ge__ (self , other : "datetime" ) -> bool :
1612
1612
if not isinstance (other , datetime ):
1613
1613
_cmperror (self , other )
1614
1614
return self ._cmp (other ) >= 0
1615
1615
1616
- def __gt__ (self , other ) :
1616
+ def __gt__ (self , other : "datetime" ) -> bool :
1617
1617
if not isinstance (other , datetime ):
1618
1618
_cmperror (self , other )
1619
1619
return self ._cmp (other ) > 0
1620
1620
1621
- def _cmp (self , other , allow_mixed = False ):
1621
+ def _cmp (self , other : "datetime" , allow_mixed : bool = False ) -> int :
1622
1622
assert isinstance (other , datetime )
1623
1623
mytz = self ._tzinfo
1624
1624
ottz = other .tzinfo
@@ -1667,7 +1667,7 @@ def _cmp(self, other, allow_mixed=False):
1667
1667
return - 1
1668
1668
return 1 if diff else 0
1669
1669
1670
- def __add__ (self , other ) :
1670
+ def __add__ (self , other : timedelta ) -> "datetime" :
1671
1671
"Add a datetime and a timedelta."
1672
1672
if not isinstance (other , timedelta ):
1673
1673
return NotImplemented
@@ -1690,7 +1690,7 @@ def __add__(self, other):
1690
1690
1691
1691
__radd__ = __add__
1692
1692
1693
- def __sub__ (self , other ) :
1693
+ def __sub__ (self , other : Union [ "datetime" , timedelta ]) -> "datetime" :
1694
1694
"Subtract two datetimes, or a datetime and a timedelta."
1695
1695
if not isinstance (other , datetime ):
1696
1696
if isinstance (other , timedelta ):
@@ -1714,7 +1714,7 @@ def __sub__(self, other):
1714
1714
raise TypeError ("cannot mix naive and timezone-aware time" )
1715
1715
return base + otoff - myoff
1716
1716
1717
- def __hash__ (self ):
1717
+ def __hash__ (self ) -> int :
1718
1718
if self ._hashcode == - 1 :
1719
1719
t = self
1720
1720
tzoff = t .utcoffset ()
@@ -1728,7 +1728,7 @@ def __hash__(self):
1728
1728
)
1729
1729
return self ._hashcode
1730
1730
1731
- def _getstate (self ):
1731
+ def _getstate (self ) -> Tuple [ bytes ] :
1732
1732
protocol = 3
1733
1733
yhi , ylo = divmod (self ._year , 256 )
1734
1734
us2 , us3 = divmod (self ._microsecond , 256 )
0 commit comments