Skip to content

Commit 153a1fc

Browse files
committed
Added typing information
1 parent a92c639 commit 153a1fc

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

bencode/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
from collections import deque
1919
import 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+
2126
try:
2227
from collections import OrderedDict
2328
except ImportError:
@@ -41,6 +46,7 @@
4146

4247

4348
def 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

5763
def 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

9097
def 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

100108
def 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

110119
def 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

157168
def 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

185197
def encode_bencached(x, r):
198+
# type: (Bencached, Deque[bytes]) -> None
186199
r.append(x.bencoded)
187200

188201

189202
def encode_int(x, r):
203+
# type: (int, Deque[bytes]) -> None
190204
r.extend((b'i', str(x).encode('utf-8'), b'e'))
191205

192206

193207
def 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

200215
def encode_bytes(x, r):
216+
# type: (bytes, Deque[bytes]) -> None
201217
r.extend((str(len(x)).encode('utf-8'), b':', x))
202218

203219

204220
def 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

213230
def 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

222240
def 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

270289
def 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

294314
def 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

311332
def 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

Comments
 (0)