Skip to content

Commit cd800d4

Browse files
committed
use choice_permitted instead of float_permitted
1 parent b6556d0 commit cd800d4

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

hypothesis-python/src/hypothesis/internal/floats.py

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -139,41 +139,30 @@ def next_up_normal(value: float, width: int, *, allow_subnormal: bool) -> float:
139139
mantissa_mask = (1 << 52) - 1
140140

141141

142-
def float_permitted(
143-
f: float,
144-
*,
145-
min_value: float,
146-
max_value: float,
147-
allow_nan: bool,
148-
smallest_nonzero_magnitude: float,
149-
) -> bool:
150-
if math.isnan(f):
151-
return allow_nan
152-
if 0 < abs(f) < smallest_nonzero_magnitude:
153-
return False
154-
return sign_aware_lte(min_value, f) and sign_aware_lte(f, max_value)
155-
156-
157142
def make_float_clamper(
158143
min_value: float,
159144
max_value: float,
160145
*,
161-
smallest_nonzero_magnitude: float,
162146
allow_nan: bool,
147+
smallest_nonzero_magnitude: float,
163148
) -> Callable[[float], float]:
164149
"""
165150
Return a function that clamps positive floats into the given bounds.
166151
"""
152+
from hypothesis.internal.conjecture.choice import choice_permitted
153+
167154
assert sign_aware_lte(min_value, max_value)
168155
range_size = min(max_value - min_value, float_info.max)
169156

170157
def float_clamper(f: float) -> float:
171-
if float_permitted(
158+
if choice_permitted(
172159
f,
173-
min_value=min_value,
174-
max_value=max_value,
175-
allow_nan=allow_nan,
176-
smallest_nonzero_magnitude=smallest_nonzero_magnitude,
160+
{
161+
"min_value": min_value,
162+
"max_value": max_value,
163+
"allow_nan": allow_nan,
164+
"smallest_nonzero_magnitude": smallest_nonzero_magnitude,
165+
},
177166
):
178167
return f
179168
# Outside bounds; pick a new value, sampled from the allowed range,

hypothesis-python/tests/cover/test_float_utils.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
import pytest
1515

1616
from hypothesis import example, given, strategies as st
17-
from hypothesis.internal.conjecture.choice import choice_equal
17+
from hypothesis.internal.conjecture.choice import choice_equal, choice_permitted
1818
from hypothesis.internal.floats import (
1919
count_between_floats,
20-
float_permitted,
2120
make_float_clamper,
2221
next_down,
2322
next_up,
@@ -96,11 +95,13 @@ def test_float_clamper(constraints, input_value):
9695

9796
# if input_value was permitted in the first place, then the clamped value should
9897
# be the same as the input value.
99-
if float_permitted(
98+
if choice_permitted(
10099
input_value,
101-
min_value=min_value,
102-
max_value=max_value,
103-
allow_nan=allow_nan,
104-
smallest_nonzero_magnitude=smallest_nonzero_magnitude,
100+
{
101+
"min_value": min_value,
102+
"max_value": max_value,
103+
"allow_nan": allow_nan,
104+
"smallest_nonzero_magnitude": smallest_nonzero_magnitude,
105+
},
105106
):
106107
assert choice_equal(input_value, clamped)

0 commit comments

Comments
 (0)