1818from collections import deque
1919import sys
2020
21+ try :
22+ from typing import Dict , List , Tuple , Deque , Union , TextIO , BinaryIO , Any
23+ except ImportError :
24+ Dict , List , Tuple , Deque , Union , TextIO , BinaryIO , Any = None
25+
2126try :
2227 from collections import OrderedDict
2328except ImportError :
4146
4247
4348def decode_int (x , f ):
49+ # type: (bytes, int) -> Tuple[int, int]
4450 f += 1
4551 newf = x .index (b'e' , f )
4652 n = int (x [f :newf ])
@@ -55,6 +61,7 @@ def decode_int(x, f):
5561
5662
5763def decode_string (x , f , try_decode_utf8 = True , force_decode_utf8 = False ):
64+ # type: (bytes, int, bool, bool) -> Tuple[bytes, int]
5865 """Decode torrent bencoded 'string' in x starting at f.
5966
6067 An attempt is made to convert the string to a python string from utf-8.
@@ -88,6 +95,7 @@ def decode_string(x, f, try_decode_utf8=True, force_decode_utf8=False):
8895
8996
9097def decode_list (x , f ):
98+ # type: (bytes, int) -> Tuple[List, int]
9199 r , f = [], f + 1
92100
93101 while x [f :f + 1 ] != b'e' :
@@ -98,6 +106,7 @@ def decode_list(x, f):
98106
99107
100108def decode_dict_py26 (x , f ):
109+ # type: (bytes, int) -> Tuple[Dict[str, Any], int]
101110 r , f = {}, f + 1
102111
103112 while x [f ] != 'e' :
@@ -108,6 +117,7 @@ def decode_dict_py26(x, f):
108117
109118
110119def decode_dict (x , f , force_sort = True ):
120+ # type: (bytes, int, bool) -> Tuple[OrderedDict[str, Any], int]
111121 """Decode bencoded data to an OrderedDict.
112122
113123 The BitTorrent standard states that:
@@ -121,6 +131,7 @@ def decode_dict(x, f, force_sort=True):
121131 represented in x, as many other encoders and decoders do not force this
122132 property.
123133 """
134+
124135 r , f = OrderedDict (), f + 1
125136
126137 while x [f :f + 1 ] != b'e' :
@@ -155,6 +166,7 @@ def decode_dict(x, f, force_sort=True):
155166
156167
157168def bdecode (value ):
169+ # type: (bytes) -> Union[Tuple, List, OrderedDict, bool, int, str, bytes]
158170 """
159171 Decode bencode formatted byte string ``value``.
160172
@@ -183,25 +195,30 @@ def __init__(self, s):
183195
184196
185197def encode_bencached (x , r ):
198+ # type: (Bencached, Deque[bytes]) -> None
186199 r .append (x .bencoded )
187200
188201
189202def encode_int (x , r ):
203+ # type: (int, Deque[bytes]) -> None
190204 r .extend ((b'i' , str (x ).encode ('utf-8' ), b'e' ))
191205
192206
193207def encode_bool (x , r ):
208+ # type: (bool, Deque[bytes]) -> None
194209 if x :
195210 encode_int (1 , r )
196211 else :
197212 encode_int (0 , r )
198213
199214
200215def encode_bytes (x , r ):
216+ # type: (bytes, Deque[bytes]) -> None
201217 r .extend ((str (len (x )).encode ('utf-8' ), b':' , x ))
202218
203219
204220def encode_string (x , r ):
221+ # type: (str, Deque[bytes]) -> None
205222 try :
206223 s = x .encode ('utf-8' )
207224 except UnicodeDecodeError :
@@ -211,6 +228,7 @@ def encode_string(x, r):
211228
212229
213230def encode_list (x , r ):
231+ # type: (List, Deque[bytes]) -> None
214232 r .append (b'l' )
215233
216234 for i in x :
@@ -220,6 +238,7 @@ def encode_list(x, r):
220238
221239
222240def encode_dict (x , r ):
241+ # type: (Dict, Deque[bytes]) -> None
223242 r .append (b'd' )
224243 ilist = list (x .items ())
225244 ilist .sort ()
@@ -268,6 +287,7 @@ def encode_dict(x, r):
268287
269288
270289def bencode (value ):
290+ # type: (Union[Tuple, List, OrderedDict, Dict, bool, int, str, bytes]) -> bytes
271291 """
272292 Encode ``value`` into the bencode format.
273293
@@ -292,6 +312,7 @@ def bencode(value):
292312
293313
294314def bread (fd ):
315+ # type: (Union[bytes, str, pathlib.Path, pathlib.PurePath, TextIO, BinaryIO]) -> bytes
295316 """Return bdecoded data from filename, file, or file-like object.
296317
297318 if fd is a bytes/string or pathlib.Path-like object, it is opened and
@@ -309,6 +330,7 @@ def bread(fd):
309330
310331
311332def bwrite (data , fd ):
333+ # type: (Union[Tuple, List, OrderedDict, Dict, bool, int, str, bytes], Union[bytes, str, pathlib.Path, pathlib.PurePath, TextIO, BinaryIO]) -> None
312334 """Write data in bencoded form to filename, file, or file-like object.
313335
314336 if fd is bytes/string or pathlib.Path-like object, it is opened and
0 commit comments