Skip to content

Commit a1c102c

Browse files
authored
Merge 9fcbe13 into bb9acd8
2 parents bb9acd8 + 9fcbe13 commit a1c102c

File tree

16 files changed

+274
-117
lines changed

16 files changed

+274
-117
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ jobs:
3030
- python-version: 3.9
3131
env:
3232
TOXENV: black
33+
- python-version: 3.9
34+
env:
35+
TOXENV: typing
3336

3437
steps:
3538
- uses: actions/checkout@v2

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ _trial_temp
1010
.coverage
1111
coverage.xml
1212
.cache
13+
.mypy_cache/
14+
/index.txt
15+
.dmypy.json
16+
.hypothesis/

docs/w3lib.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ w3lib Package
2626

2727
.. automodule:: w3lib.url
2828
:members:
29+
30+
.. autoclass:: ParseDataURIResult
31+
:members:

mypy.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[mypy]
2+
exclude = .*flycheck_.*
3+
show_error_codes = True
4+
check_untyped_defs = True
5+
6+
[mypy-w3lib.*]
7+
# All non-tests functions must be typed.
8+
disallow_untyped_defs = True
9+
10+
[mypy-tests.*]
11+
# Allow test functions to be untyped
12+
disallow_untyped_defs = False

tests/test_encoding.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ def test_bom(self):
3838
utf32le = b"\xff\xfe\x00\x00\x34\x6c\x00\x00"
3939
for string in (utf16be, utf16le, utf32be, utf32le):
4040
bom_encoding, bom = read_bom(string)
41+
assert bom_encoding is not None
42+
assert bom is not None
4143
decoded = string[len(bom) :].decode(bom_encoding)
4244
self.assertEqual(water_unicode, decoded)
4345
# Body without BOM
44-
enc, bom = read_bom("foo")
46+
enc, bom = read_bom(b"foo")
4547
self.assertEqual(enc, None)
4648
self.assertEqual(bom, None)
4749
# Empty body
48-
enc, bom = read_bom("")
50+
enc, bom = read_bom(b"")
4951
self.assertEqual(enc, None)
5052
self.assertEqual(bom, None)
5153

tests/test_html.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def test_illegal_entities(self):
6969
def test_browser_hack(self):
7070
# check browser hack for numeric character references in the 80-9F range
7171
self.assertEqual(replace_entities("x™y", encoding="cp1252"), "x\u2122y")
72-
self.assertEqual(replace_entities("x™y", encoding="cp1252"), u"x\u2122y")
72+
self.assertEqual(replace_entities("x™y", encoding="cp1252"), "x\u2122y")
7373

7474
def test_missing_semicolon(self):
7575
for entity, result in (

tests/test_http.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import unittest
22
from collections import OrderedDict
3-
from w3lib.http import basic_auth_header, headers_dict_to_raw, headers_raw_to_dict
3+
from w3lib.http import (
4+
HeadersDictInput,
5+
basic_auth_header,
6+
headers_dict_to_raw,
7+
headers_raw_to_dict,
8+
)
49

510
__doctests__ = ["w3lib.http"] # for trial support
611

@@ -47,7 +52,9 @@ def test_headers_dict_to_raw(self):
4752
)
4853

4954
def test_headers_dict_to_raw_listtuple(self):
50-
dct = OrderedDict([(b"Content-type", [b"text/html"]), (b"Accept", [b"gzip"])])
55+
dct: HeadersDictInput = OrderedDict(
56+
[(b"Content-type", [b"text/html"]), (b"Accept", [b"gzip"])]
57+
)
5158
self.assertEqual(
5259
headers_dict_to_raw(dct), b"Content-type: text/html\r\nAccept: gzip"
5360
)
@@ -70,12 +77,13 @@ def test_headers_dict_to_raw_listtuple(self):
7077
)
7178

7279
def test_headers_dict_to_raw_wrong_values(self):
73-
dct = OrderedDict(
80+
dct: HeadersDictInput = OrderedDict(
7481
[
7582
(b"Content-type", 0),
7683
]
7784
)
7885
self.assertEqual(headers_dict_to_raw(dct), b"")
86+
self.assertEqual(headers_dict_to_raw(dct), b"")
7987

8088
dct = OrderedDict([(b"Content-type", 1), (b"Accept", [b"gzip"])])
8189
self.assertEqual(headers_dict_to_raw(dct), b"Accept: gzip")

tests/test_url.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ def test_add_or_replace_parameters(self):
508508
def test_add_or_replace_parameters_does_not_change_input_param(self):
509509
url = "http://domain/test?arg=original"
510510
input_param = {"arg": "value"}
511-
new_url = add_or_replace_parameters(url, input_param) # noqa
511+
add_or_replace_parameters(url, input_param) # noqa
512512
self.assertEqual(input_param, {"arg": "value"})
513513

514514
def test_url_query_cleaner(self):
@@ -817,15 +817,18 @@ def test_non_ascii_percent_encoding_in_paths(self):
817817
self.assertEqual(
818818
canonicalize_url("http://www.example.com/a do?a=1"),
819819
"http://www.example.com/a%20do?a=1",
820-
),
820+
)
821+
821822
self.assertEqual(
822823
canonicalize_url("http://www.example.com/a %20do?a=1"),
823824
"http://www.example.com/a%20%20do?a=1",
824-
),
825+
)
826+
825827
self.assertEqual(
826828
canonicalize_url("http://www.example.com/a do£.html?a=1"),
827829
"http://www.example.com/a%20do%C2%A3.html?a=1",
828830
)
831+
829832
self.assertEqual(
830833
canonicalize_url(b"http://www.example.com/a do\xc2\xa3.html?a=1"),
831834
"http://www.example.com/a%20do%C2%A3.html?a=1",

tests/test_util.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_deprecation(self):
2020
class ToBytesTestCase(TestCase):
2121
def test_type_error(self):
2222
with raises(TypeError):
23-
to_bytes(True)
23+
to_bytes(True) # type: ignore
2424

2525

2626
class ToNativeStrTestCase(TestCase):
@@ -32,7 +32,7 @@ def test_deprecation(self):
3232
class ToUnicodeTestCase(TestCase):
3333
def test_type_error(self):
3434
with raises(TypeError):
35-
to_unicode(True)
35+
to_unicode(True) # type: ignore
3636

3737

3838
class UnicodeToStrTestCase(TestCase):

tox.ini

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ deps =
2222
commands =
2323
bandit -r -c .bandit.yml {posargs:w3lib}
2424

25+
[testenv:typing]
26+
basepython = python3
27+
deps =
28+
# mypy would error if pytest (or its sub) not found
29+
pytest
30+
mypy==0.910
31+
commands =
32+
mypy --show-error-codes {posargs: w3lib tests}
33+
2534
[testenv:flake8]
2635
basepython = python3
2736
deps =

0 commit comments

Comments
 (0)