Skip to content

Commit 87c32ac

Browse files
authored
Merge pull request #4141 from tybug/integer-weights-fix
Fix `compute_max_children` for integer weights
2 parents 86c8b8f + 00401e1 commit 87c32ac

File tree

4 files changed

+15
-21
lines changed

4 files changed

+15
-21
lines changed

hypothesis-python/RELEASE.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RELEASE_TYPE: patch
2+
3+
This patch fixes a regression from :ref:`version 6.115.2 <v6.115.2>` where generating values from :func:`~hypothesis.strategies.integers` with certain values for ``min_value`` and ``max_value`` would error.

hypothesis-python/src/hypothesis/internal/conjecture/datatree.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -174,18 +174,13 @@ def compute_max_children(ir_type, kwargs):
174174
if ir_type == "integer":
175175
min_value = kwargs["min_value"]
176176
max_value = kwargs["max_value"]
177-
weights = kwargs["weights"]
178177

179178
if min_value is None and max_value is None:
180179
# full 128 bit range.
181180
return 2**128 - 1
182181
if min_value is not None and max_value is not None:
183182
# count between min/max value.
184-
n = max_value - min_value + 1
185-
# remove any values with a zero probability of being drawn (weight=0).
186-
if weights is not None:
187-
n -= sum(weight == 0 for weight in weights)
188-
return n
183+
return max_value - min_value + 1
189184

190185
# hard case: only one bound was specified. Here we probe either upwards
191186
# or downwards with our full 128 bit generation, but only half of these
@@ -267,21 +262,13 @@ def all_children(ir_type, kwargs):
267262
if ir_type == "integer":
268263
min_value = kwargs["min_value"]
269264
max_value = kwargs["max_value"]
270-
weights = kwargs["weights"]
271265

272266
if min_value is None and max_value is None:
273267
# full 128 bit range.
274268
yield from range(-(2**127) + 1, 2**127 - 1)
275269

276270
elif min_value is not None and max_value is not None:
277-
if weights is None:
278-
yield from range(min_value, max_value + 1)
279-
else:
280-
# skip any values with a corresponding weight of 0 (can never be drawn).
281-
for weight, n in zip(weights, range(min_value, max_value + 1)):
282-
if weight == 0:
283-
continue
284-
yield n
271+
yield from range(min_value, max_value + 1)
285272
else:
286273
assert (min_value is None) ^ (max_value is None)
287274
# hard case: only one bound was specified. Here we probe in 128 bits

hypothesis-python/tests/conjecture/common.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,18 @@ def integer_kwargs(
163163

164164
# Sampler doesn't play well with super small floats, so exclude them
165165
weights = draw(
166-
st.dictionaries(st.integers(), st.floats(0.001, 1), max_size=255)
166+
st.dictionaries(
167+
st.integers(min_value=min_value, max_value=max_value),
168+
st.floats(0.001, 1),
169+
max_size=255,
170+
)
167171
)
168172
# invalid to have a weighting that disallows all possibilities
169173
assume(sum(weights.values()) != 0)
174+
# re-normalize probabilities to sum to some arbitrary target < 1
170175
target = draw(st.floats(0.001, 0.999))
171-
# re-normalize probabilities to sum to some arbitrary value < 1
172-
weights = {k: v / target for k, v in weights.items()}
176+
factor = target / sum(weights.values())
177+
weights = {k: v * factor for k, v in weights.items()}
173178
# float rounding error can cause this to fail.
174179
assume(sum(weights.values()) == target)
175180
else:

hypothesis-python/tests/conjecture/test_ir.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ def test_compute_max_children_is_positive(ir_type_and_kwargs):
5757
@pytest.mark.parametrize(
5858
"ir_type, kwargs, count_children",
5959
[
60-
("integer", {"min_value": 1, "max_value": 2, "weights": [0, 1]}, 1),
61-
("integer", {"min_value": 1, "max_value": 4, "weights": [0, 0.5, 0, 0.5]}, 2),
60+
("integer", {"min_value": 1, "max_value": 2, "weights": {1: 0.1, 2: 0.1}}, 2),
6261
# only possibility is the empty string
6362
(
6463
"string",
@@ -275,7 +274,7 @@ def test_draw_string_single_interval_with_equal_bounds(s, n):
275274
{
276275
"min_value": 1,
277276
"max_value": 2,
278-
"weights": [0, 1],
277+
"weights": {1: 0.2, 2: 0.4},
279278
"shrink_towards": 0,
280279
},
281280
)

0 commit comments

Comments
 (0)