Skip to content

Commit

Permalink
Merge pull request #3873 from tybug/booleans-use-ir
Browse files Browse the repository at this point in the history
Directly use `ConjectureData.draw_boolean()` in `booleans()`
  • Loading branch information
Zac-HD authored Feb 5, 2024
2 parents 5ec646c + dedf8da commit 156a8be
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
RELEASE_TYPE: patch

This patch refactors some internals. There is no user-visible change.
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def all_children(ir_type, kwargs):
max_size = kwargs["max_size"]
intervals = kwargs["intervals"]

# written unidiomatically in order to handle the case of max_size=inf.
size = min_size
while size <= max_size:
for ords in itertools.product(intervals, repeat=size):
Expand Down
6 changes: 3 additions & 3 deletions hypothesis-python/src/hypothesis/strategies/_internal/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
from hypothesis.strategies._internal.deferred import DeferredStrategy
from hypothesis.strategies._internal.functions import FunctionStrategy
from hypothesis.strategies._internal.lazy import LazyStrategy, unwrap_strategies
from hypothesis.strategies._internal.misc import just, none, nothing
from hypothesis.strategies._internal.misc import BooleansStrategy, just, none, nothing
from hypothesis.strategies._internal.numbers import (
IntegersStrategy,
Real,
Expand Down Expand Up @@ -158,14 +158,14 @@


@cacheable
@defines_strategy()
@defines_strategy(force_reusable_values=True)
def booleans() -> SearchStrategy[bool]:
"""Returns a strategy which generates instances of :class:`python:bool`.
Examples from this strategy will shrink towards ``False`` (i.e.
shrinking will replace ``True`` with ``False`` where possible).
"""
return SampledFromStrategy([False, True], repr_="booleans()")
return BooleansStrategy()


@overload
Expand Down
8 changes: 8 additions & 0 deletions hypothesis-python/src/hypothesis/strategies/_internal/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,11 @@ def nothing() -> SearchStrategy:
Examples from this strategy do not shrink (because there are none).
"""
return NOTHING


class BooleansStrategy(SearchStrategy):
def do_draw(self, data):
return data.draw_boolean()

def __repr__(self):
return "booleans()"

0 comments on commit 156a8be

Please sign in to comment.