Skip to content

Commit a3b8b06

Browse files
committed
Run black
1 parent 1b1e12a commit a3b8b06

3 files changed

Lines changed: 63 additions & 44 deletions

File tree

src/hyperlink/hypothesis.py

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,38 @@
66

77
try:
88
import hypothesis
9+
910
del hypothesis
1011
except ImportError:
1112
from typing import Tuple
13+
1214
__all__ = () # type: Tuple[str, ...]
1315
else:
1416
from csv import reader as csv_reader
1517
from os.path import dirname, join
1618
from string import ascii_letters, digits
1719
from sys import maxunicode
1820
from typing import (
19-
Callable, Iterable, List, Optional, Sequence, Text, TypeVar, cast
21+
Callable,
22+
Iterable,
23+
List,
24+
Optional,
25+
Sequence,
26+
Text,
27+
TypeVar,
28+
cast,
2029
)
2130
from gzip import open as open_gzip
2231

2332
from . import DecodedURL, EncodedURL
2433

2534
from hypothesis import assume
2635
from hypothesis.strategies import (
27-
composite, integers, lists, sampled_from, text
36+
composite,
37+
integers,
38+
lists,
39+
sampled_from,
40+
text,
2841
)
2942

3043
from idna import IDNAError, check_label, encode as idna_encode
@@ -39,7 +52,7 @@
3952
"port_numbers",
4053
)
4154

42-
T = TypeVar('T')
55+
T = TypeVar("T")
4356
DrawCallable = Callable[[Callable[..., T]], T]
4457

4558
try:
@@ -65,8 +78,7 @@ def idna_characters():
6578
)
6679
with open_gzip(dataFileName) as dataFile:
6780
reader = csv_reader(
68-
(line.decode("utf-8") for line in dataFile),
69-
delimiter=",",
81+
(line.decode("utf-8") for line in dataFile), delimiter=",",
7082
)
7183
next(reader) # Skip header row
7284
for row in reader:
@@ -116,9 +128,7 @@ def idna_text(draw, min_size=1, max_size=None):
116128

117129
result = cast(
118130
Text,
119-
draw(text(
120-
min_size=min_size, max_size=max_size, alphabet=alphabet
121-
))
131+
draw(text(min_size=min_size, max_size=max_size, alphabet=alphabet)),
122132
)
123133

124134
# FIXME: There should be a more efficient way to ensure we produce
@@ -143,9 +153,7 @@ def port_numbers(draw, allow_zero=False):
143153
else:
144154
min_value = 1
145155

146-
return cast(
147-
int, draw(integers(min_value=min_value, max_value=65535))
148-
)
156+
return cast(int, draw(integers(min_value=min_value, max_value=65535)))
149157

150158
@composite
151159
def hostname_labels(draw, allow_idn=True):
@@ -165,9 +173,7 @@ def hostname_labels(draw, allow_idn=True):
165173
# If the label doesn't encode to ASCII, then we need to check
166174
# the length of the label after encoding to punycode and adding
167175
# the xn-- prefix.
168-
while (
169-
len(label.encode("punycode")) > 63 - len("xn--")
170-
):
176+
while len(label.encode("punycode")) > 63 - len("xn--"):
171177
# Rather than bombing out, just trim from the end until it
172178
# is short enough, so hypothesis doesn't have to generate
173179
# new data.
@@ -176,10 +182,13 @@ def hostname_labels(draw, allow_idn=True):
176182
else:
177183
label = cast(
178184
Text,
179-
draw(text(
180-
min_size=1, max_size=63,
181-
alphabet=Text(ascii_letters + digits + u"-")
182-
))
185+
draw(
186+
text(
187+
min_size=1,
188+
max_size=63,
189+
alphabet=Text(ascii_letters + digits + u"-"),
190+
)
191+
),
183192
)
184193

185194
# Filter invalid labels.
@@ -208,20 +217,25 @@ def hostnames(draw, allow_leading_digit=True, allow_idn=True):
208217
labels = [
209218
cast(
210219
Text,
211-
draw(hostname_labels(allow_idn=allow_idn).filter(
212-
lambda l: (
213-
True if allow_leading_digit else l[0] not in digits
220+
draw(
221+
hostname_labels(allow_idn=allow_idn).filter(
222+
lambda l: (
223+
True if allow_leading_digit else l[0] not in digits
224+
)
214225
)
215-
))
226+
),
216227
)
217228
]
218229
# Draw remaining labels
219230
labels += cast(
220231
List[Text],
221-
draw(lists(
222-
hostname_labels(allow_idn=allow_idn),
223-
min_size=1, max_size=4,
224-
))
232+
draw(
233+
lists(
234+
hostname_labels(allow_idn=allow_idn),
235+
min_size=1,
236+
max_size=4,
237+
)
238+
),
225239
)
226240

227241
# Trim off labels until the total host name length fits in 252
@@ -239,6 +253,7 @@ def path_characters():
239253
global _path_characters
240254

241255
if _path_characters is None:
256+
242257
def chars():
243258
# type: () -> Iterable[Text]
244259
for i in range(maxunicode):
@@ -268,10 +283,8 @@ def paths(draw):
268283
return cast(
269284
List[Text],
270285
draw(
271-
lists(
272-
text(min_size=1, alphabet=path_characters()), max_size=10
273-
)
274-
)
286+
lists(text(min_size=1, alphabet=path_characters()), max_size=10)
287+
),
275288
)
276289

277290
@composite

src/hyperlink/test/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ def _init_hypothesis():
1717
return
1818

1919
settings.register_profile(
20-
"patience", settings(
20+
"patience",
21+
settings(
2122
suppress_health_check=[
22-
HealthCheck.too_slow, HealthCheck.filter_too_much
23+
HealthCheck.too_slow,
24+
HealthCheck.filter_too_much,
2325
]
24-
)
26+
),
2527
)
2628
settings.load_profile("patience")
2729

src/hyperlink/test/test_hypothesis.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55

66
try:
77
import hypothesis
8+
89
del hypothesis
910
except ImportError:
1011
pass
1112
else:
1213
from string import digits
1314
from typing import Sequence, Text
15+
1416
try:
1517
from unittest.mock import patch
1618
except ImportError:
@@ -24,8 +26,15 @@
2426
from .common import HyperlinkTestCase
2527
from .. import DecodedURL, EncodedURL
2628
from ..hypothesis import (
27-
DrawCallable, composite, decoded_urls, encoded_urls,
28-
hostname_labels, hostnames, idna_text, paths, port_numbers,
29+
DrawCallable,
30+
composite,
31+
decoded_urls,
32+
encoded_urls,
33+
hostname_labels,
34+
hostnames,
35+
idna_text,
36+
paths,
37+
port_numbers,
2938
)
3039

3140
class TestHypothesisStrategies(HyperlinkTestCase):
@@ -42,9 +51,7 @@ def test_idna_text_valid(self, text):
4251
try:
4352
idna_encode(text)
4453
except IDNAError: # pragma: no cover
45-
raise AssertionError(
46-
"Invalid IDNA text: {!r}".format(text)
47-
)
54+
raise AssertionError("Invalid IDNA text: {!r}".format(text))
4855

4956
@given(data())
5057
def test_idna_text_min_max(self, data):
@@ -84,9 +91,7 @@ def test_hostname_labels_valid_idn(self, label):
8491
check_label(label)
8592
idna_encode(label)
8693
except UnicodeError: # pragma: no cover
87-
raise AssertionError(
88-
"Invalid IDN label: {!r}".format(label)
89-
)
94+
raise AssertionError("Invalid IDN label: {!r}".format(label))
9095

9196
@given(data())
9297
@settings(max_examples=10)
@@ -96,6 +101,7 @@ def test_hostname_labels_long_idn_punycode(self, data):
96101
hostname_labels() handles case where idna_text() generates text
97102
that encoded to punycode ends up as longer than allowed.
98103
"""
104+
99105
@composite
100106
def mock_idna_text(draw, min_size, max_size):
101107
# type: (DrawCallable, int, int) -> Text
@@ -126,9 +132,7 @@ def test_hostname_labels_valid_ascii(self, label):
126132
check_label(label)
127133
label.encode("ascii")
128134
except UnicodeError: # pragma: no cover
129-
raise AssertionError(
130-
"Invalid ASCII label: {!r}".format(label)
131-
)
135+
raise AssertionError("Invalid ASCII label: {!r}".format(label))
132136

133137
@given(hostnames())
134138
def test_hostnames_idn(self, hostname):

0 commit comments

Comments
 (0)