Skip to content

Commit 582f306

Browse files
committed
fix not clamping -0.0 to 0.0
1 parent 8c7c254 commit 582f306

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

hypothesis-python/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+
Fixes a recently-introduced bug where we might have generated ``-0.0`` for ``st.floats(min_value=0.0)``, which is unsound.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,9 @@ def draw_float(
370370
clamped = result # pragma: no cover
371371
else:
372372
clamped = clamper(result)
373-
if clamped != result and not (math.isnan(result) and allow_nan):
373+
if float_to_int(clamped) != float_to_int(result) and not (
374+
math.isnan(result) and allow_nan
375+
):
374376
result = clamped
375377
else:
376378
result = nasty_floats[i - 1]

hypothesis-python/tests/conjecture/test_provider_contract.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@
1919
from hypothesis.internal.conjecture.providers import BytestringProvider
2020
from hypothesis.internal.intervalsets import IntervalSet
2121

22-
from tests.conjecture.common import float_kw, integer_kw, ir_types_and_kwargs, string_kw
22+
from tests.conjecture.common import (
23+
float_kw,
24+
integer_kw,
25+
ir_types_and_kwargs,
26+
nodes,
27+
string_kw,
28+
)
2329

2430

2531
@example(b"\x00" * 100, [("integer", integer_kw())])
@@ -50,20 +56,16 @@ def test_provider_contract_bytestring(bytestring, ir_type_and_kwargs):
5056
except StopTest:
5157
return
5258

53-
# ir_value_permitted is currently restricted to what *could* be generated
54-
# by the buffer. once we're fully on the TCS, we can drop this restriction.
55-
# until then, the BytestringProvider can theoretically generate values
56-
# that aren't forcable to a buffer - but this requires an enormous shrink_towards
57-
# value and is such an edge case that I'm just going to bank on nobody hitting
58-
# it before we're off the bytestring.
59-
integer_edge_case = (
60-
ir_type == "integer"
61-
and kwargs["shrink_towards"] is not None
62-
and kwargs["shrink_towards"].bit_length() > 100
63-
)
64-
assert choice_permitted(value, kwargs) or integer_edge_case
65-
59+
assert choice_permitted(value, kwargs)
6660
kwargs["forced"] = choice_from_index(0, ir_type, kwargs)
6761
assert choice_equal(
6862
kwargs["forced"], getattr(data, f"draw_{ir_type}")(**kwargs)
6963
)
64+
65+
66+
@given(st.lists(nodes()), st.randoms())
67+
def test_provider_contract_hypothesis(nodes, random):
68+
data = ConjectureData(random=random)
69+
for node in nodes:
70+
value = getattr(data, f"draw_{node.ir_type}")(**node.kwargs)
71+
assert choice_permitted(value, node.kwargs)

0 commit comments

Comments
 (0)