11import datetime
22import struct
33from collections import namedtuple
4+ import typing as t
45
56
67class ExtType (namedtuple ("ExtType" , "code data" )):
78 """ExtType represents ext type in msgpack."""
9+ code : int
10+ data : bytes
811
9- def __new__ (cls , code , data ):
12+ def __new__ (cls , code : int , data : bytes ):
1013 if not isinstance (code , int ):
1114 raise TypeError ("code must be int" )
1215 if not isinstance (data , bytes ):
1316 raise TypeError ("data must be bytes" )
1417 if not 0 <= code <= 127 :
1518 raise ValueError ("code must be 0~127" )
16- return super ().__new__ (cls , code , data )
19+ return super ().__new__ (cls , code , data ) # type: ignore
1720
1821
1922class Timestamp :
@@ -28,7 +31,7 @@ class Timestamp:
2831
2932 __slots__ = ["seconds" , "nanoseconds" ]
3033
31- def __init__ (self , seconds , nanoseconds = 0 ):
34+ def __init__ (self , seconds : int , nanoseconds = 0 ):
3235 """Initialize a Timestamp object.
3336
3437 :param int seconds:
@@ -54,21 +57,21 @@ def __repr__(self):
5457 """String representation of Timestamp."""
5558 return f"Timestamp(seconds={ self .seconds } , nanoseconds={ self .nanoseconds } )"
5659
57- def __eq__ (self , other ):
60+ def __eq__ (self , other : t . Any ):
5861 """Check for equality with another Timestamp object"""
5962 if type (other ) is self .__class__ :
6063 return self .seconds == other .seconds and self .nanoseconds == other .nanoseconds
6164 return False
6265
63- def __ne__ (self , other ):
66+ def __ne__ (self , other : t . Any ):
6467 """not-equals method (see :func:`__eq__()`)"""
6568 return not self .__eq__ (other )
6669
6770 def __hash__ (self ):
6871 return hash ((self .seconds , self .nanoseconds ))
6972
7073 @staticmethod
71- def from_bytes (b ):
74+ def from_bytes (b : bytes ):
7275 """Unpack bytes into a `Timestamp` object.
7376
7477 Used for pure-Python msgpack unpacking.
@@ -116,7 +119,7 @@ def to_bytes(self):
116119 return data
117120
118121 @staticmethod
119- def from_unix (unix_sec ):
122+ def from_unix (unix_sec : int | float ):
120123 """Create a Timestamp from posix timestamp in seconds.
121124
122125 :param unix_float: Posix timestamp in seconds.
@@ -135,7 +138,7 @@ def to_unix(self):
135138 return self .seconds + self .nanoseconds / 1e9
136139
137140 @staticmethod
138- def from_unix_nano (unix_ns ):
141+ def from_unix_nano (unix_ns : int ):
139142 """Create a Timestamp from posix timestamp in nanoseconds.
140143
141144 :param int unix_ns: Posix timestamp in nanoseconds.
@@ -162,7 +165,7 @@ def to_datetime(self):
162165 )
163166
164167 @staticmethod
165- def from_datetime (dt ):
168+ def from_datetime (dt : datetime . datetime ):
166169 """Create a Timestamp from datetime with tzinfo.
167170
168171 :rtype: Timestamp
0 commit comments