Skip to content

Commit 49ba6fc

Browse files
committed
Merge branch 'master' into typing/_settings/coverage
2 parents 6df93f9 + 2e26f0b commit 49ba6fc

4 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎hypothesis/src/hypothesis/internal/conjecture/shrinker.py‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,16 @@ def cached_test_function(
399399
if startswith(nodes, self.nodes):
400400
return (True, None)
401401

402-
if sort_key(self.nodes) < sort_key(nodes):
403-
return (False, None)
404-
405402
# sometimes our shrinking passes try obviously invalid things. We handle
406403
# discarding them in one place here.
407404
if any(not choice_permitted(node.value, node.constraints) for node in nodes):
408405
return (False, None)
409406

407+
# note that sort_key can raise on invalid nodes, so we need to check
408+
# choice_permitted first
409+
if sort_key(self.nodes) < sort_key(nodes):
410+
return (False, None)
411+
410412
result = self.engine.cached_test_function(
411413
[n.value for n in nodes], extend=self.__extend
412414
)

‎hypothesis/tests/conjecture/test_shrinker.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from hypothesis.internal.conjecture.shrinking.common import Shrinker as ShrinkerPass
2626
from hypothesis.internal.conjecture.utils import Sampler
2727
from hypothesis.internal.floats import MAX_PRECISE_INTEGER
28+
from hypothesis.internal.intervalsets import IntervalSet
2829
from hypothesis.strategies._internal.lazy import unwrap_strategies
2930

3031
from tests.common.utils import skipif_time_unpatched
@@ -934,6 +935,18 @@ def shrinker(data: ConjectureData):
934935
assert shrinker.choices == ("À",)
935936

936937

938+
def test_discards_string_outside_its_intervals():
939+
intervals = IntervalSet.from_string("ab")
940+
941+
@shrinking_from(["b"])
942+
def shrinker(data: ConjectureData):
943+
data.draw_string(intervals, min_size=1)
944+
data.mark_interesting(interesting_origin())
945+
946+
invalid = shrinker.nodes[0].copy(with_value="z")
947+
assert shrinker.cached_test_function([invalid]) == (False, None)
948+
949+
937950
def test_normalize_unicode_chars_skips_when_no_simplification():
938951
# Plain ascii 'A' has no natural simplification (it is already a base
939952
# uppercase letter), so the pass should leave it untouched.

‎hypothesis/tests/cover/test_one_of.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from hypothesis import given, strategies as st
1717
from hypothesis.errors import InvalidArgument
1818

19-
from tests.common.debug import assert_no_examples
19+
from tests.common.debug import assert_no_examples, minimal
2020

2121

2222
def test_one_of_empty():
@@ -62,3 +62,10 @@ def test_one_of_without_strategies_suggests_sampled_from():
6262
)
6363
def test_one_of_unwrapping(strategy, count):
6464
assert repr(strategy).count("one_of(") == count
65+
66+
67+
def test_shrinks_across_branches_with_different_alphabets():
68+
s = minimal(
69+
st.one_of(st.text("a", max_size=1), st.text("b")), lambda s: len(s) >= 2
70+
)
71+
assert s == "bb"

‎hypothesis/tests/cover/test_regressions.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import pickle
1212
import random
13+
import string
1314
from datetime import timedelta
1415
from unittest.mock import Mock
1516

@@ -161,3 +162,37 @@ def test_no_missed_custom_init_exceptions():
161162
} - {type(exc) for exc in exc_instances}
162163
print(untested_errors_with_custom_init)
163164
assert not untested_errors_with_custom_init
165+
166+
167+
def test_shrinking_mixed_alphabet_one_of_does_not_crash():
168+
# See https://github.com/HypothesisWorks/hypothesis/issues/4905
169+
@st.composite
170+
def lines(draw):
171+
parts = draw(
172+
st.lists(
173+
st.one_of(
174+
st.text("-=_*~ ", min_size=1, max_size=40),
175+
st.text(" \t", max_size=3),
176+
st.text(
177+
string.ascii_letters
178+
+ string.digits
179+
+ string.punctuation
180+
+ " \t\xa0",
181+
max_size=50,
182+
),
183+
),
184+
max_size=6,
185+
)
186+
)
187+
return "\n".join(parts)
188+
189+
@given(lines())
190+
def test(t):
191+
for w in t.split():
192+
letters = "".join(c for c in w if c.isalpha())
193+
if len(letters) >= 4:
194+
assert letters in t
195+
break
196+
197+
with pytest.raises(AssertionError):
198+
test()

0 commit comments

Comments
 (0)