Skip to content

Commit d5af8b4

Browse files
authored
Merge pull request #4913 from Liam-DeVoe/fix-integer-weights-key
Fix integer weights key
2 parents 3352fe0 + a4cbaa9 commit d5af8b4

5 files changed

Lines changed: 58 additions & 3 deletions

File tree

‎hypothesis/RELEASE.rst‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes a bug for users of our |PrimitiveProvider| interface, where ``draw_integer`` could incorrectly reuse weights from an earlier draw (:issue:`4908`).

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,11 @@ def choice_constraints_key(
625625
return (
626626
constraints["min_value"],
627627
constraints["max_value"],
628-
None if constraints["weights"] is None else tuple(constraints["weights"]),
628+
(
629+
None
630+
if constraints["weights"] is None
631+
else tuple(sorted(constraints["weights"].items()))
632+
),
629633
constraints["shrink_towards"],
630634
)
631635
return tuple(constraints[key] for key in sorted(constraints)) # type: ignore

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1153,7 +1153,7 @@ def draw_boolean(
11531153
size = 2**bits
11541154
# always leave at least one value that can be true, even for very small
11551155
# p.
1156-
falsey = max(1, math.floor(size * (1 - p)))
1156+
falsey = clamp(1, math.floor(size * (1 - p)), size - 1)
11571157
n = self._draw_bits(bits)
11581158
return n >= falsey
11591159

‎hypothesis/tests/conjecture/test_choice.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from hypothesis.internal.conjecture.choice import (
2828
ChoiceNode,
2929
ChoiceTemplate,
30+
choice_constraints_equal,
3031
choice_equal,
3132
choice_from_index,
3233
choice_permitted,
@@ -303,6 +304,36 @@ def test_choice_node_equality(node):
303304
assert node != 42
304305

305306

307+
def test_choice_constraints_equal_weighted_integers():
308+
# check two things:
309+
# * the same constraints dict, constructed twice, is equal
310+
# * the same constraints dict, reordered, is equal
311+
constraints = [
312+
{
313+
"min_value": 0,
314+
"max_value": 2,
315+
"weights": {0: 0.25, 1: 0.5},
316+
"shrink_towards": 0,
317+
},
318+
{
319+
"min_value": 0,
320+
"max_value": 2,
321+
"weights": {1: 0.5, 0: 0.25},
322+
"shrink_towards": 0,
323+
},
324+
{
325+
"shrink_towards": 0,
326+
"weights": {0: 0.25, 1: 0.5},
327+
"max_value": 2,
328+
"min_value": 0,
329+
},
330+
]
331+
for c1 in constraints:
332+
for c2 in constraints:
333+
assert c1 == c2
334+
assert choice_constraints_equal("integer", c1, c2)
335+
336+
306337
@given(nodes(was_forced=True))
307338
def test_cannot_modify_forced_nodes(node):
308339
with pytest.raises(AssertionError):

‎hypothesis/tests/conjecture/test_provider.py‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
Verbosity,
2727
assume,
2828
errors,
29+
example,
2930
given,
3031
settings,
3132
strategies as st,
@@ -57,7 +58,7 @@
5758
BytestringProvider,
5859
with_register_backend,
5960
)
60-
from hypothesis.internal.floats import SIGNALING_NAN, clamp
61+
from hypothesis.internal.floats import SIGNALING_NAN, clamp, next_down
6162
from hypothesis.internal.intervalsets import IntervalSet
6263
from hypothesis.internal.observability import Observation, _callbacks
6364

@@ -1124,3 +1125,19 @@ def f(n):
11241125
def test_backend_cannot_proceed_raises_on_invalid_scope():
11251126
with pytest.raises(InvalidArgument):
11261127
BackendCannotProceed("not a valid scope")
1128+
1129+
1130+
# even for extremely small probabilities, BytestringProvider always leaves one
1131+
# bit open for True/False
1132+
@example(1e-99)
1133+
@example(next_down(1.0))
1134+
@given(st.floats(0, 1, exclude_min=True, exclude_max=True))
1135+
def test_bytestring_draw_boolean_can_return_either_value(p):
1136+
for bytestring, expected in [(b"\xff", True), (b"\x00", False)]:
1137+
data = ConjectureData(
1138+
random=None,
1139+
observer=None,
1140+
provider=BytestringProvider,
1141+
provider_kw={"bytestring": bytestring},
1142+
)
1143+
assert data.draw_boolean(p) is expected

0 commit comments

Comments
 (0)