Skip to content

Commit 27e0a1e

Browse files
committed
refactor(test): Reduce usage of assume()
According to recommendation of hypothesis, .filter() is more efficient
1 parent c261837 commit 27e0a1e

11 files changed

Lines changed: 134 additions & 68 deletions

tests/runtime/elem/test_attrib.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
signature_tester,
2828
strategy as _st,
2929
)
30-
from .._testutils.common import attr_name_types, attr_value_types
30+
from .._testutils.common import (
31+
attr_name_types,
32+
attr_value_types,
33+
hashable_elem_if_is_set,
34+
)
3135
from .._testutils.errors import raise_invalid_utf8_type
3236

3337
if sys.version_info >= (3, 11):
@@ -65,11 +69,7 @@ def test_key_type_bad_1(self, disposable_attrib: _Attrib, thing: Any) -> None:
6569
def test_key_type_bad_2(
6670
self, disposable_attrib: _Attrib, iterable_of: Any, k: _AttrName
6771
) -> None:
68-
# unhashable types not addable to set
69-
assume(not (
70-
getattr(iterable_of, "type") in {set, frozenset}
71-
and isinstance(k, bytearray)
72-
)) # fmt: skip
72+
assume(hashable_elem_if_is_set(iterable_of, k))
7373
with raise_invalid_utf8_type:
7474
_ = disposable_attrib[iterable_of(k)]
7575

@@ -94,11 +94,7 @@ def test_value_type_bad_1(self, disposable_attrib: _Attrib, thing: Any) -> None:
9494
def test_value_type_bad_2(
9595
self, disposable_attrib: _Attrib, iterable_of: Any, v: _AttrVal
9696
) -> None:
97-
# unhashable types not addable to set
98-
assume(not (
99-
getattr(iterable_of, "type") in {set, frozenset}
100-
and isinstance(v, bytearray)
101-
)) # fmt: skip
97+
assume(hashable_elem_if_is_set(iterable_of, v))
10298
with raise_invalid_utf8_type:
10399
disposable_attrib["foo"] = iterable_of(v)
104100

@@ -391,10 +387,7 @@ def test_input_iterable_ok(
391387
iterable_of: Any,
392388
) -> None:
393389
# unhashable types not addable to set
394-
assume(not (
395-
getattr(iterable_of, "type") in {set, frozenset}
396-
and isinstance(v, bytearray)
397-
)) # fmt: skip
390+
assume(hashable_elem_if_is_set(iterable_of, v))
398391
self._verify_key_val_present(disposable_attrib, iterable_of((k, v)))
399392

400393
@given(atts=st.iterables(

tests/runtime/elem/test_elem_accessor.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import pytest
1313
from hypothesis import (
1414
HealthCheck,
15-
assume,
1615
given,
1716
settings,
1817
strategies as st,
@@ -76,10 +75,13 @@ def test_child_arg_bad_2(
7675
suppress_health_check=[HealthCheck.function_scoped_fixture],
7776
max_examples=300,
7877
)
79-
@given(thing=_st.all_instances_except_of_type(NoneType, int, Decimal))
78+
@given(
79+
thing=_st.all_instances_except_of_type(NoneType, int, Decimal).filter(
80+
lambda x: x is not NotImplemented and bool(x)
81+
)
82+
)
8083
@pytest.mark.slow
8184
def test_start_arg_bad_1(self, xml2_root: _Element, thing: Any) -> None:
82-
assume(thing is not NotImplemented and bool(thing))
8385
with raise_lxml_non_integer:
8486
_ = xml2_root.index(xml2_root[0], start=thing)
8587

@@ -96,10 +98,13 @@ def test_start_arg_bad_2(self, xml2_root: _Element, iterable_of: Any) -> None:
9698
suppress_health_check=[HealthCheck.function_scoped_fixture],
9799
max_examples=300,
98100
)
99-
@given(thing=_st.all_instances_except_of_type(NoneType, int, Decimal))
101+
@given(
102+
thing=_st.all_instances_except_of_type(NoneType, int, Decimal).filter(
103+
lambda x: x is not NotImplemented and bool(x)
104+
)
105+
)
100106
@pytest.mark.slow
101107
def test_stop_arg_bad_1(self, xml2_root: _Element, thing: Any) -> None:
102-
assume(thing is not NotImplemented and bool(thing))
103108
with raise_lxml_non_integer:
104109
_ = xml2_root.index(xml2_root[0], stop=thing)
105110

tests/runtime/elem/test_find_methods.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
from typing import Any, cast
1616

1717
import pytest
18-
from hypothesis import HealthCheck, assume, example, given, settings
18+
from hypothesis import (
19+
HealthCheck,
20+
example,
21+
given,
22+
settings,
23+
)
1924
from lxml.etree import (
2025
QName,
2126
_Attrib as _Attrib,
@@ -109,12 +114,15 @@ def test_namespaces_arg_ok(self, svg_root: _Element) -> None:
109114

110115
# range objects can cause indefinite hang
111116
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
112-
@given(thing=_st.all_instances_except_of_type(Mapping, NoneType, range))
117+
@given(
118+
thing=_st.all_instances_except_of_type(Mapping, NoneType, range).filter(
119+
lambda x: x is not NotImplemented and bool(x)
120+
)
121+
)
113122
@pytest.mark.slow
114123
def test_namespaces_arg_bad_1(
115124
self, disposable_element: _Element, thing: Any
116125
) -> None:
117-
assume(thing is not NotImplemented and bool(thing))
118126
# pyrefly: ignore[no-matching-overload]
119127
with pytest.raises((TypeError, AttributeError)): # too diversified
120128
_ = disposable_element.iterfind("foo", namespaces=thing)
@@ -175,12 +183,15 @@ def test_namespaces_arg_ok(self, svg_root: _Element) -> None:
175183

176184
# range objects can cause indefinite hang
177185
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
178-
@given(thing=_st.all_instances_except_of_type(Mapping, NoneType, range))
186+
@given(
187+
thing=_st.all_instances_except_of_type(Mapping, NoneType, range).filter(
188+
lambda x: x is not NotImplemented and bool(x)
189+
)
190+
)
179191
@pytest.mark.slow
180192
def test_namespaces_arg_bad_1(
181193
self, disposable_element: _Element, thing: Any
182194
) -> None:
183-
assume(thing is not NotImplemented and bool(thing))
184195
# pyrefly: ignore[no-matching-overload]
185196
with pytest.raises((TypeError, AttributeError)): # too diversified
186197
_ = disposable_element.find("foo", namespaces=thing)
@@ -241,12 +252,15 @@ def test_namespaces_arg_ok(self, svg_root: _Element) -> None:
241252

242253
# range objects can cause indefinite hang
243254
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
244-
@given(thing=_st.all_instances_except_of_type(Mapping, NoneType, range))
255+
@given(
256+
thing=_st.all_instances_except_of_type(Mapping, NoneType, range).filter(
257+
lambda x: x is not NotImplemented and bool(x)
258+
)
259+
)
245260
@pytest.mark.slow
246261
def test_namespaces_arg_bad_1(
247262
self, disposable_element: _Element, thing: Any
248263
) -> None:
249-
assume(thing is not NotImplemented and bool(thing))
250264
# pyrefly: ignore[no-matching-overload]
251265
with pytest.raises((TypeError, AttributeError)): # too diversified
252266
_ = disposable_element.findall("foo", namespaces=thing)
@@ -329,12 +343,15 @@ def test_namespaces_arg_ok(self, svg_root: _Element) -> None:
329343

330344
# range objects can cause indefinite hang
331345
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
332-
@given(thing=_st.all_instances_except_of_type(Mapping, NoneType, range))
346+
@given(
347+
thing=_st.all_instances_except_of_type(Mapping, NoneType, range).filter(
348+
lambda x: x is not NotImplemented and bool(x)
349+
)
350+
)
333351
@pytest.mark.slow
334352
def test_namespaces_arg_bad_1(
335353
self, disposable_element: _Element, thing: Any
336354
) -> None:
337-
assume(thing is not NotImplemented and bool(thing))
338355
# pyrefly: ignore[no-matching-overload]
339356
with pytest.raises((TypeError, AttributeError)): # too diversified
340357
_ = disposable_element.findtext("foo", namespaces=thing)

tests/runtime/elem/test_iter_methods.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,10 +323,13 @@ def test_input_ok(self, svg_root: _Element, arg: Any, kw: Any, empty: bool) -> N
323323
max_examples=300,
324324
)
325325
@given(
326-
thing=_st.all_instances_except_of_type(
326+
thing=_st
327+
.all_instances_except_of_type(
327328
*tag_selector_types.allow,
328329
*tag_selector_types.skip,
329-
).filter(lambda x: x is not NotImplemented and bool(x)).filter(lambda x: not can_practically_iter(x)),
330+
)
331+
.filter(lambda x: x is not NotImplemented and bool(x))
332+
.filter(lambda x: not can_practically_iter(x)),
330333
)
331334
@pytest.mark.slow
332335
def test_input_bad_1(self, xml2_root: _Element, thing: Any) -> None:

tests/runtime/html_/test_beautifulsoup.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010

1111
import pytest
1212
from bs4 import BeautifulSoup
13-
from hypothesis import HealthCheck, assume, given, settings
13+
from hypothesis import (
14+
HealthCheck,
15+
given,
16+
settings,
17+
)
1418
from lxml.etree import (
1519
Element,
1620
_Element as _Element,
@@ -93,10 +97,13 @@ def test_beautifulsoup_arg_ok(self, html2_str: str) -> None:
9397
del result2
9498

9599
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
96-
@given(thing=_st.all_instances_except_of_type(NoneType))
100+
@given(
101+
thing=_st.all_instances_except_of_type(NoneType).filter(
102+
lambda x: x is not NoneType
103+
)
104+
)
97105
@pytest.mark.slow
98106
def test_beautifulsoup_arg_bad_1(self, html2_str: str, thing: Any) -> None:
99-
assume(thing is not NoneType)
100107
if not callable(thing):
101108
with raise_non_callable:
102109
_ = _soup.fromstring(html2_str, thing)
@@ -293,11 +300,14 @@ def test_makeelement_arg_ok(self, html2_str: str) -> None:
293300
del result2
294301

295302
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
296-
@given(thing=_st.all_instances_except_of_type(NoneType))
303+
@given(
304+
thing=_st.all_instances_except_of_type(NoneType).filter(
305+
lambda x: x is not NoneType
306+
)
307+
)
297308
@pytest.mark.slow
298309
def test_makeelement_arg_bad_1(self, html2_str: str, thing: Any) -> None:
299310
soup = BeautifulSoup(html2_str, features="html.parser")
300-
assume(thing is not NoneType)
301311
if not callable(thing):
302312
with raise_non_callable:
303313
_ = _soup.convert_tree(soup, makeelement=thing)

tests/runtime/html_/test_element.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,13 @@ def test_sequence_modify_bad_1(
273273
# items into element tree (e.g. huge ranges)
274274
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
275275
@given(
276-
thing=_st
277-
.all_instances_except_of_type(
276+
thing=_st.all_instances_except_of_type(
278277
_Element,
279278
Iterator,
280279
range,
281280
ipaddress.IPv4Network,
282281
ipaddress.IPv6Network,
283-
)
284-
.filter(lambda x: x is not NotImplemented and bool(x))
282+
).filter(lambda x: x is not NotImplemented and bool(x))
285283
)
286284
@pytest.mark.slow
287285
def test_sequence_modify_bad_2(

tests/runtime/html_/test_html5lib.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
import lxml.etree as _e
1313
import lxml.html.html5parser as h5
1414
import pytest
15-
from hypothesis import HealthCheck, assume, given, settings
15+
from hypothesis import (
16+
HealthCheck,
17+
given,
18+
settings,
19+
)
1620
from lxml.etree import (
1721
_Element as _Element,
1822
_ElementTree as _ElementTree,
@@ -220,10 +224,13 @@ def test_parse_src_ok(
220224
# empty html document (<html><body/></html>)
221225
# - buffer-like objects, in which html content is directly taken from
222226
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
223-
@given(thing=_st.all_instances_except_of_type(str, io.BytesIO, io.StringIO, Buffer))
227+
@given(
228+
thing=_st.all_instances_except_of_type(
229+
str, io.BytesIO, io.StringIO, Buffer
230+
).filter(lambda x: x is not NotImplemented and bool(x))
231+
)
224232
@pytest.mark.slow
225233
def test_parse_src_bad(self, thing: Any) -> None:
226-
assume(thing is not NotImplemented and bool(thing))
227234
# pyrefly: ignore[no-matching-overload]
228235
with pytest.raises((
229236
TypeError,

tests/runtime/html_/test_link_funcs.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import pytest
1919
from hypothesis import (
2020
HealthCheck,
21-
assume,
2221
given,
2322
settings,
2423
)
@@ -304,12 +303,15 @@ def test_handle_failures_arg_ok(
304303
assert old != new
305304

306305
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
307-
@given(thing=_st.all_instances_except_of_type(NoneType))
306+
@given(
307+
thing=_st.all_instances_except_of_type(NoneType).filter(
308+
lambda x: x not in ("ignore", "discard")
309+
)
310+
)
308311
@pytest.mark.slow
309312
def test_handle_failures_arg_bad_1(
310313
self, disposable_html_with_base_href: HtmlElement, thing: Any
311314
) -> None:
312-
assume(thing not in ("ignore", "discard"))
313315
# collection raises TypeError instead
314316
# because of error in constructing exception
315317
# pyrefly: ignore[no-matching-overload]
@@ -352,12 +354,15 @@ def test_handle_failures_valid_type(
352354
assert old != new
353355

354356
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
355-
@given(thing=_st.all_instances_except_of_type(NoneType))
357+
@given(
358+
thing=_st.all_instances_except_of_type(NoneType).filter(
359+
lambda x: x not in ("ignore", "discard")
360+
)
361+
)
356362
@pytest.mark.slow
357363
def test_handle_failures_wrong_type(
358364
self, disposable_html_with_base_href: HtmlElement, thing: Any
359365
) -> None:
360-
assume(thing not in ("ignore", "discard"))
361366
# pyrefly: ignore[no-matching-overload]
362367
with pytest.raises((
363368
ValueError,
@@ -371,13 +376,16 @@ def test_handle_failures_wrong_type(
371376
# that can be anything
372377

373378
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
374-
@given(thing=_st.all_instances_except_of_type(str, NoneType))
379+
# Falsy values short circuited by urljoin() and never raises
380+
@given(
381+
thing=_st.all_instances_except_of_type(str, NoneType).filter(
382+
lambda x: x is not NotImplemented and bool(x)
383+
)
384+
)
375385
@pytest.mark.slow
376386
def test_base_href(
377387
self, disposable_html_with_base_href: HtmlElement, thing: Any
378388
) -> None:
379-
# Falsy values short circuited by urljoin() and never raises
380-
assume(thing is not NotImplemented and bool(thing))
381389
with pytest.raises(TypeError, match="Cannot mix str and non-str arguments"):
382390
_ = make_links_absolute(disposable_html_with_base_href, base_url=thing)
383391

@@ -434,13 +442,16 @@ def test_link_repl_func_bad_input(
434442
# that can be anything
435443

436444
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
437-
@given(thing=_st.all_instances_except_of_type(str, NoneType))
445+
# Falsy values got short circuited by urljoin() and never raises
446+
@given(
447+
thing=_st.all_instances_except_of_type(str, NoneType).filter(
448+
lambda x: x is not NotImplemented and bool(x)
449+
)
450+
)
438451
@pytest.mark.slow
439452
def test_base_href(
440453
self, disposable_html_with_base_href: HtmlElement, thing: Any
441454
) -> None:
442-
# Falsy values got short circuited by urljoin() and never raises
443-
assume(thing is not NotImplemented and bool(thing))
444455
with pytest.raises(TypeError, match="Cannot mix str and non-str arguments"):
445456
_ = rewrite_links(disposable_html_with_base_href, str, base_href=thing)
446457

tests/runtime/test_errorlog.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
from typing import Any, cast
1111

1212
import pytest
13-
from hypothesis import HealthCheck, assume, given, settings
13+
from hypothesis import (
14+
HealthCheck,
15+
given,
16+
settings,
17+
)
1418
from lxml.etree import (
1519
ErrorDomains,
1620
ErrorLevels,
@@ -317,10 +321,13 @@ def test_init_name_arg_ok(self) -> None:
317321
use_global_python_log(pylog)
318322

319323
@settings(suppress_health_check=[HealthCheck.too_slow], max_examples=300)
320-
@given(thing=_st.all_instances_except_of_type(str))
324+
@given(
325+
thing=_st.all_instances_except_of_type(str).filter(
326+
lambda x: x is not NotImplemented and bool(x)
327+
)
328+
)
321329
@pytest.mark.slow
322330
def test_init_name_arg_bad_1(self, thing: Any) -> None:
323-
assume(thing is not NotImplemented and bool(thing))
324331
with pytest.raises(TypeError, match="logger name must be a string"):
325332
_ = PyErrorLog(logger_name=thing)
326333

0 commit comments

Comments
 (0)