Skip to content

Commit 8235f70

Browse files
committed
Fix rare BytesWarning
1 parent d1efe56 commit 8235f70

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

hypothesis-python/src/hypothesis/strategies/_internal/strategies.py

+17-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
import sys
1212
import warnings
13-
from collections import defaultdict
13+
from collections import abc, defaultdict
1414
from random import shuffle
1515
from typing import (
1616
Any,
@@ -811,17 +811,22 @@ def pack(self, x):
811811
raise NotImplementedError(f"{self.__class__.__name__}.pack()")
812812

813813
def do_draw(self, data: ConjectureData) -> Ex:
814-
for _ in range(3):
815-
i = data.index
816-
try:
817-
data.start_example(MAPPED_SEARCH_STRATEGY_DO_DRAW_LABEL)
818-
result = self.pack(data.draw(self.mapped_strategy))
819-
data.stop_example()
820-
return result
821-
except UnsatisfiedAssumption:
822-
data.stop_example(discard=True)
823-
if data.index == i:
824-
raise
814+
with warnings.catch_warnings():
815+
if isinstance(self.pack, type) and issubclass(
816+
self.pack, (abc.Mapping, abc.Set)
817+
):
818+
warnings.simplefilter("ignore", BytesWarning)
819+
for _ in range(3):
820+
i = data.index
821+
try:
822+
data.start_example(MAPPED_SEARCH_STRATEGY_DO_DRAW_LABEL)
823+
result = self.pack(data.draw(self.mapped_strategy))
824+
data.stop_example()
825+
return result
826+
except UnsatisfiedAssumption:
827+
data.stop_example(discard=True)
828+
if data.index == i:
829+
raise
825830
raise UnsatisfiedAssumption()
826831

827832
@property

hypothesis-python/tests/cover/test_lookup.py

+5
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,11 @@ def test_generic_collections_only_use_hashable_elements(typ, data):
713713
data.draw(from_type(typ))
714714

715715

716+
@given(st.sets(st.integers() | st.binary(), min_size=2))
717+
def test_no_byteswarning(_):
718+
pass
719+
720+
716721
def test_hashable_type_unhashable_value():
717722
# Decimal("snan") is not hashable; we should be able to generate it.
718723
# See https://github.com/HypothesisWorks/hypothesis/issues/2320

0 commit comments

Comments
 (0)