Skip to content

Commit d2fda29

Browse files
committed
make max_size required for draw_string and draw_bytes
1 parent 5b0ea4a commit d2fda29

File tree

7 files changed

+51
-46
lines changed

7 files changed

+51
-46
lines changed

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

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ class FloatKWargs(TypedDict):
111111
class StringKWargs(TypedDict):
112112
intervals: IntervalSet
113113
min_size: int
114-
max_size: Optional[int]
114+
max_size: int
115115

116116

117117
class BytesKWargs(TypedDict):
118118
min_size: int
119-
max_size: Optional[int]
119+
max_size: int
120120

121121

122122
class BooleanKWargs(TypedDict):
@@ -1317,7 +1317,7 @@ def draw_string(
13171317
intervals: IntervalSet,
13181318
*,
13191319
min_size: int = 0,
1320-
max_size: Optional[int] = None,
1320+
max_size: int = COLLECTION_DEFAULT_MAX_SIZE,
13211321
forced: Optional[str] = None,
13221322
fake_forced: bool = False,
13231323
) -> str:
@@ -1326,8 +1326,8 @@ def draw_string(
13261326
@abc.abstractmethod
13271327
def draw_bytes(
13281328
self,
1329-
min_size: int,
1330-
max_size: Optional[int],
1329+
min_size: int = 0,
1330+
max_size: int = COLLECTION_DEFAULT_MAX_SIZE,
13311331
*,
13321332
forced: Optional[bytes] = None,
13331333
fake_forced: bool = False,
@@ -1614,14 +1614,10 @@ def draw_string(
16141614
intervals: IntervalSet,
16151615
*,
16161616
min_size: int = 0,
1617-
max_size: Optional[int] = None,
1617+
max_size: int = COLLECTION_DEFAULT_MAX_SIZE,
16181618
forced: Optional[str] = None,
16191619
fake_forced: bool = False,
16201620
) -> str:
1621-
if max_size is None:
1622-
max_size = COLLECTION_DEFAULT_MAX_SIZE
1623-
1624-
assert forced is None or min_size <= len(forced) <= max_size
16251621
assert self._cd is not None
16261622

16271623
average_size = min(
@@ -1672,16 +1668,12 @@ def draw_string(
16721668

16731669
def draw_bytes(
16741670
self,
1675-
min_size: int,
1676-
max_size: Optional[int],
1671+
min_size: int = 0,
1672+
max_size: int = COLLECTION_DEFAULT_MAX_SIZE,
16771673
*,
16781674
forced: Optional[bytes] = None,
16791675
fake_forced: bool = False,
16801676
) -> bytes:
1681-
if max_size is None:
1682-
max_size = COLLECTION_DEFAULT_MAX_SIZE
1683-
1684-
assert forced is None or min_size <= len(forced) <= max_size
16851677
assert self._cd is not None
16861678

16871679
buf = bytearray()
@@ -2251,12 +2243,12 @@ def draw_string(
22512243
intervals: IntervalSet,
22522244
*,
22532245
min_size: int = 0,
2254-
max_size: Optional[int] = None,
2246+
max_size: int = COLLECTION_DEFAULT_MAX_SIZE,
22552247
forced: Optional[str] = None,
22562248
fake_forced: bool = False,
22572249
observe: bool = True,
22582250
) -> str:
2259-
assert forced is None or min_size <= len(forced)
2251+
assert forced is None or min_size <= len(forced) <= max_size
22602252
assert min_size >= 0
22612253

22622254
kwargs: StringKWargs = self._pooled_kwargs(
@@ -2291,14 +2283,14 @@ def draw_string(
22912283

22922284
def draw_bytes(
22932285
self,
2294-
min_size: int,
2295-
max_size: Optional[int],
2286+
min_size: int = 0,
2287+
max_size: int = COLLECTION_DEFAULT_MAX_SIZE,
22962288
*,
22972289
forced: Optional[bytes] = None,
22982290
fake_forced: bool = False,
22992291
observe: bool = True,
23002292
) -> bytes:
2301-
assert forced is None or min_size <= len(forced)
2293+
assert forced is None or min_size <= len(forced) <= max_size
23022294
assert min_size >= 0
23032295

23042296
kwargs: BytesKWargs = self._pooled_kwargs(

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ def _repr_pretty_(self, p, cycle):
147147

148148

149149
def compute_max_children(ir_type, kwargs):
150-
from hypothesis.internal.conjecture.data import COLLECTION_DEFAULT_MAX_SIZE
151-
152150
if ir_type == "integer":
153151
min_value = kwargs["min_value"]
154152
max_value = kwargs["max_value"]
@@ -181,9 +179,6 @@ def compute_max_children(ir_type, kwargs):
181179
min_size = kwargs["min_size"]
182180
max_size = kwargs["max_size"]
183181

184-
if max_size is None:
185-
max_size = COLLECTION_DEFAULT_MAX_SIZE
186-
187182
definitely_too_large = max_size * math.log(2**8) > math.log(
188183
MAX_CHILDREN_EFFECTIVELY_INFINITE
189184
)
@@ -196,9 +191,6 @@ def compute_max_children(ir_type, kwargs):
196191
max_size = kwargs["max_size"]
197192
intervals = kwargs["intervals"]
198193

199-
if max_size is None:
200-
max_size = COLLECTION_DEFAULT_MAX_SIZE
201-
202194
if len(intervals) == 0:
203195
# Special-case the empty alphabet to avoid an error in math.log(0).
204196
# Only possibility is the empty string.

hypothesis-python/src/hypothesis/strategies/_internal/strings.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from hypothesis.errors import HypothesisWarning, InvalidArgument
1818
from hypothesis.internal import charmap
19+
from hypothesis.internal.conjecture.data import COLLECTION_DEFAULT_MAX_SIZE
1920
from hypothesis.internal.filtering import max_len, min_len
2021
from hypothesis.internal.intervalsets import IntervalSet
2122
from hypothesis.internal.reflection import get_pretty_function_description
@@ -158,7 +159,13 @@ def do_draw(self, data):
158159
elems = unwrap_strategies(self.element_strategy)
159160
if isinstance(elems, OneCharStringStrategy):
160161
return data.draw_string(
161-
elems.intervals, min_size=self.min_size, max_size=self.max_size
162+
elems.intervals,
163+
min_size=self.min_size,
164+
max_size=(
165+
COLLECTION_DEFAULT_MAX_SIZE
166+
if self.max_size == float("inf")
167+
else self.max_size
168+
),
162169
)
163170
return "".join(super().do_draw(data))
164171

@@ -338,7 +345,9 @@ def _identifier_characters():
338345
class BytesStrategy(SearchStrategy):
339346
def __init__(self, min_size: int, max_size: Optional[int]):
340347
self.min_size = min_size
341-
self.max_size = max_size if max_size is not None else float("inf")
348+
self.max_size = (
349+
max_size if max_size is not None else COLLECTION_DEFAULT_MAX_SIZE
350+
)
342351

343352
def do_draw(self, data):
344353
return data.draw_bytes(self.min_size, self.max_size)

hypothesis-python/tests/conjecture/common.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
from hypothesis.control import current_build_context
1616
from hypothesis.errors import InvalidArgument
1717
from hypothesis.internal.conjecture import engine as engine_module
18-
from hypothesis.internal.conjecture.data import ConjectureData, IRNode, Status
18+
from hypothesis.internal.conjecture.data import (
19+
COLLECTION_DEFAULT_MAX_SIZE,
20+
ConjectureData,
21+
IRNode,
22+
Status,
23+
)
1924
from hypothesis.internal.conjecture.engine import BUFFER_SIZE, ConjectureRunner
2025
from hypothesis.internal.conjecture.utils import calc_label_from_name
2126
from hypothesis.internal.entropy import deterministic_PRNG
@@ -172,7 +177,7 @@ def draw_integer_kwargs(
172177
@st.composite
173178
def _collection_kwargs(draw, *, forced, use_min_size=True, use_max_size=True):
174179
min_size = 0
175-
max_size = None
180+
max_size = COLLECTION_DEFAULT_MAX_SIZE
176181
# collections are quite expensive in entropy. cap to avoid overruns.
177182
cap = 50
178183

hypothesis-python/tests/conjecture/test_alt_backend.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from hypothesis.internal.compat import int_to_bytes
2525
from hypothesis.internal.conjecture.data import (
2626
AVAILABLE_PROVIDERS,
27+
COLLECTION_DEFAULT_MAX_SIZE,
2728
ConjectureData,
2829
PrimitiveProvider,
2930
)
@@ -132,7 +133,7 @@ def draw_string(
132133
intervals: IntervalSet,
133134
*,
134135
min_size: int = 0,
135-
max_size: Optional[int] = None,
136+
max_size: int = COLLECTION_DEFAULT_MAX_SIZE,
136137
forced: Optional[str] = None,
137138
fake_forced: bool = False,
138139
) -> str:
@@ -145,8 +146,8 @@ def draw_string(
145146

146147
def draw_bytes(
147148
self,
148-
min_size: int,
149-
max_size: Optional[int],
149+
min_size: int = 0,
150+
max_size: int = COLLECTION_DEFAULT_MAX_SIZE,
150151
*,
151152
forced: Optional[bytes] = None,
152153
fake_forced: bool = False,

hypothesis-python/tests/conjecture/test_ir.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from hypothesis import assume, example, given, strategies as st
1818
from hypothesis.errors import StopTest
1919
from hypothesis.internal.conjecture.data import (
20+
COLLECTION_DEFAULT_MAX_SIZE,
2021
ConjectureData,
2122
IRNode,
2223
Status,
@@ -88,7 +89,7 @@ def test_compute_max_children_is_positive(ir_type_and_kwargs):
8889
"string",
8990
{
9091
"min_size": 0,
91-
"max_size": None,
92+
"max_size": COLLECTION_DEFAULT_MAX_SIZE,
9293
"intervals": IntervalSet.from_string("a"),
9394
},
9495
MAX_CHILDREN_EFFECTIVELY_INFINITE,
@@ -114,7 +115,7 @@ def test_compute_max_children_is_positive(ir_type_and_kwargs):
114115
"bytes",
115116
{
116117
"min_size": 0,
117-
"max_size": None,
118+
"max_size": COLLECTION_DEFAULT_MAX_SIZE,
118119
},
119120
MAX_CHILDREN_EFFECTIVELY_INFINITE,
120121
),
@@ -363,7 +364,7 @@ def test_ir_nodes(random):
363364
kwargs={
364365
"intervals": IntervalSet.from_string("abcd"),
365366
"min_size": 0,
366-
"max_size": None,
367+
"max_size": COLLECTION_DEFAULT_MAX_SIZE,
367368
},
368369
was_forced=True,
369370
),
@@ -476,7 +477,7 @@ def test_data_with_changed_forced_value(node):
476477
kwargs={
477478
"intervals": IntervalSet.from_string("bcda"),
478479
"min_size": 4,
479-
"max_size": None,
480+
"max_size": COLLECTION_DEFAULT_MAX_SIZE,
480481
},
481482
was_forced=True,
482483
)
@@ -680,7 +681,7 @@ def test_forced_nodes_are_trivial(node):
680681
kwargs={
681682
"intervals": IntervalSet.from_string("abcd"),
682683
"min_size": 0,
683-
"max_size": None,
684+
"max_size": COLLECTION_DEFAULT_MAX_SIZE,
684685
},
685686
was_forced=False,
686687
),
@@ -690,7 +691,7 @@ def test_forced_nodes_are_trivial(node):
690691
kwargs={
691692
"intervals": IntervalSet.from_string("bcda"),
692693
"min_size": 4,
693-
"max_size": None,
694+
"max_size": COLLECTION_DEFAULT_MAX_SIZE,
694695
},
695696
was_forced=False,
696697
),
@@ -703,7 +704,7 @@ def test_forced_nodes_are_trivial(node):
703704
IRNode(
704705
ir_type="bytes",
705706
value=bytes(2),
706-
kwargs={"min_size": 2, "max_size": None},
707+
kwargs={"min_size": 2, "max_size": COLLECTION_DEFAULT_MAX_SIZE},
707708
was_forced=False,
708709
),
709710
IRNode(
@@ -834,7 +835,7 @@ def values(draw):
834835
kwargs={
835836
"intervals": IntervalSet.from_string("abcd"),
836837
"min_size": 1,
837-
"max_size": None,
838+
"max_size": COLLECTION_DEFAULT_MAX_SIZE,
838839
},
839840
was_forced=False,
840841
),
@@ -847,7 +848,7 @@ def values(draw):
847848
IRNode(
848849
ir_type="bytes",
849850
value=bytes(1),
850-
kwargs={"min_size": 0, "max_size": None},
851+
kwargs={"min_size": 0, "max_size": COLLECTION_DEFAULT_MAX_SIZE},
851852
was_forced=False,
852853
),
853854
IRNode(

hypothesis-python/tests/cover/test_filter_rewriting.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@
2020

2121
from hypothesis import HealthCheck, given, settings, strategies as st
2222
from hypothesis.errors import HypothesisWarning, Unsatisfiable
23+
from hypothesis.internal.conjecture.data import COLLECTION_DEFAULT_MAX_SIZE
2324
from hypothesis.internal.filtering import max_len, min_len
2425
from hypothesis.internal.floats import next_down, next_up
2526
from hypothesis.internal.reflection import get_pretty_function_description
2627
from hypothesis.strategies._internal.core import data
2728
from hypothesis.strategies._internal.lazy import LazyStrategy, unwrap_strategies
2829
from hypothesis.strategies._internal.numbers import FloatStrategy, IntegersStrategy
2930
from hypothesis.strategies._internal.strategies import FilteredStrategy, MappedStrategy
30-
from hypothesis.strategies._internal.strings import TextStrategy
31+
from hypothesis.strategies._internal.strings import BytesStrategy, TextStrategy
3132

3233
from tests.common.debug import check_can_generate_examples
3334
from tests.common.utils import fails_with
@@ -505,6 +506,10 @@ def test_filter_rewriting_text_lambda_len(data, strategy, predicate, start, end)
505506
if isinstance(unwrapped.filtered_strategy, MappedStrategy):
506507
unwrapped = unwrapped.filtered_strategy.mapped_strategy
507508

509+
# binary() has a finite-but-effectively-infinite cap instead.
510+
if isinstance(unwrapped_nofilter, BytesStrategy) and end == math.inf:
511+
end = COLLECTION_DEFAULT_MAX_SIZE
512+
508513
assert unwrapped.filtered_strategy.min_size == start
509514
assert unwrapped.filtered_strategy.max_size == end
510515
value = data.draw(s)

0 commit comments

Comments
 (0)