1# This file is part of Hypothesis, which may be found at
2# https://github.com/HypothesisWorks/hypothesis/
3#
4# Copyright the Hypothesis Authors.
5# Individual contributors are listed in AUTHORS.rst and the git log.
6#
7# This Source Code Form is subject to the terms of the Mozilla Public License,
8# v. 2.0. If a copy of the MPL was not distributed with this file, You can
9# obtain one at https://mozilla.org/MPL/2.0/.
10
11from hypothesis.internal.reflection import get_pretty_function_description
12from hypothesis.strategies._internal.strategies import (
13 SampledFromStrategy,
14 SearchStrategy,
15 T,
16 is_simple_data,
17)
18from hypothesis.strategies._internal.utils import cacheable, defines_strategy
19
20
21class JustStrategy(SampledFromStrategy):
22 """A strategy which always returns a single fixed value.
23
24 It's implemented as a length-one SampledFromStrategy so that all our
25 special-case logic for filtering and sets applies also to just(x).
26
27 The important difference from a SampledFromStrategy with only one
28 element to choose is that JustStrategy *never* touches the underlying
29 choice sequence, i.e. drawing neither reads from nor writes to `data`.
30 This is a reasonably important optimisation (or semantic distinction!)
31 for both JustStrategy and SampledFromStrategy.
32 """
33
34 @property
35 def value(self):
36 return self.elements[0]
37
38 def __repr__(self):
39 suffix = "".join(
40 f".{name}({get_pretty_function_description(f)})"
41 for name, f in self._transformations
42 )
43 if self.value is None:
44 return "none()" + suffix
45 return f"just({get_pretty_function_description(self.value)}){suffix}"
46
47 def calc_is_cacheable(self, recur):
48 return is_simple_data(self.value)
49
50 def do_filtered_draw(self, data):
51 # The parent class's `do_draw` implementation delegates directly to
52 # `do_filtered_draw`, which we can greatly simplify in this case since
53 # we have exactly one value. (This also avoids drawing any data.)
54 return self._transform(self.value)
55
56
57@defines_strategy(never_lazy=True)
58def just(value: T) -> SearchStrategy[T]:
59 """Return a strategy which only generates ``value``.
60
61 Note: ``value`` is not copied. Be wary of using mutable values.
62
63 If ``value`` is the result of a callable, you can use
64 :func:`builds(callable) <hypothesis.strategies.builds>` instead
65 of ``just(callable())`` to get a fresh value each time.
66
67 Examples from this strategy do not shrink (because there is only one).
68 """
69 return JustStrategy([value])
70
71
72@defines_strategy(force_reusable_values=True)
73def none() -> SearchStrategy[None]:
74 """Return a strategy which only generates None.
75
76 Examples from this strategy do not shrink (because there is only
77 one).
78 """
79 return just(None)
80
81
82class Nothing(SearchStrategy):
83 def calc_is_empty(self, recur):
84 return True
85
86 def do_draw(self, data):
87 # This method should never be called because draw() will mark the
88 # data as invalid immediately because is_empty is True.
89 raise NotImplementedError("This should never happen")
90
91 def calc_has_reusable_values(self, recur):
92 return True
93
94 def __repr__(self):
95 return "nothing()"
96
97 def map(self, f):
98 return self
99
100 def filter(self, f):
101 return self
102
103 def flatmap(self, f):
104 return self
105
106
107NOTHING = Nothing()
108
109
110@cacheable
111@defines_strategy(never_lazy=True)
112def nothing() -> SearchStrategy:
113 """This strategy never successfully draws a value and will always reject on
114 an attempt to draw.
115
116 Examples from this strategy do not shrink (because there are none).
117 """
118 return NOTHING
119
120
121class BooleansStrategy(SearchStrategy):
122 def do_draw(self, data):
123 return data.draw_boolean()
124
125 def __repr__(self):
126 return "booleans()"