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
11
12class HypothesisException(Exception):
13 """Generic parent class for exceptions thrown by Hypothesis."""
14
15
16class _Trimmable(HypothesisException):
17 """Hypothesis can trim these tracebacks even if they're raised internally."""
18
19
20class UnsatisfiedAssumption(HypothesisException):
21 """An internal error raised by assume.
22
23 If you're seeing this error something has gone wrong.
24 """
25
26 def __init__(self, reason=None):
27 self.reason = reason
28
29
30class NoSuchExample(HypothesisException):
31 """The condition we have been asked to satisfy appears to be always false.
32
33 This does not guarantee that no example exists, only that we were
34 unable to find one.
35 """
36
37 def __init__(self, condition_string, extra=""):
38 super().__init__(f"No examples found of condition {condition_string}{extra}")
39
40
41class Unsatisfiable(_Trimmable):
42 """We ran out of time or examples before we could find enough examples
43 which satisfy the assumptions of this hypothesis.
44
45 This could be because the function is too slow. If so, try upping
46 the timeout. It could also be because the function is using assume
47 in a way that is too hard to satisfy. If so, try writing a custom
48 strategy or using a better starting point (e.g if you are requiring
49 a list has unique values you could instead filter out all duplicate
50 values from the list)
51 """
52
53
54class Flaky(_Trimmable):
55 """This function appears to fail non-deterministically: We have seen it
56 fail when passed this example at least once, but a subsequent invocation
57 did not fail.
58
59 Common causes for this problem are:
60 1. The function depends on external state. e.g. it uses an external
61 random number generator. Try to make a version that passes all the
62 relevant state in from Hypothesis.
63 2. The function is suffering from too much recursion and its failure
64 depends sensitively on where it's been called from.
65 3. The function is timing sensitive and can fail or pass depending on
66 how long it takes. Try breaking it up into smaller functions which
67 don't do that and testing those instead.
68 """
69
70
71class InvalidArgument(_Trimmable, TypeError):
72 """Used to indicate that the arguments to a Hypothesis function were in
73 some manner incorrect."""
74
75
76class ResolutionFailed(InvalidArgument):
77 """Hypothesis had to resolve a type to a strategy, but this failed.
78
79 Type inference is best-effort, so this only happens when an
80 annotation exists but could not be resolved for a required argument
81 to the target of ``builds()``, or where the user passed ``...``.
82 """
83
84
85class InvalidState(HypothesisException):
86 """The system is not in a state where you were allowed to do that."""
87
88
89class InvalidDefinition(_Trimmable, TypeError):
90 """Used to indicate that a class definition was not well put together and
91 has something wrong with it."""
92
93
94class HypothesisWarning(HypothesisException, Warning):
95 """A generic warning issued by Hypothesis."""
96
97
98class FailedHealthCheck(_Trimmable):
99 """Raised when a test fails a healthcheck."""
100
101
102class NonInteractiveExampleWarning(HypothesisWarning):
103 """SearchStrategy.example() is designed for interactive use,
104 but should never be used in the body of a test.
105 """
106
107
108class HypothesisDeprecationWarning(HypothesisWarning, FutureWarning):
109 """A deprecation warning issued by Hypothesis.
110
111 Actually inherits from FutureWarning, because DeprecationWarning is
112 hidden by the default warnings filter.
113
114 You can configure the Python :mod:`python:warnings` to handle these
115 warnings differently to others, either turning them into errors or
116 suppressing them entirely. Obviously we would prefer the former!
117 """
118
119
120class HypothesisSideeffectWarning(HypothesisWarning):
121 """A warning issued by Hypothesis when it sees actions that are
122 discouraged at import or initialization time because they are
123 slow or have user-visible side effects.
124 """
125
126
127class Frozen(HypothesisException):
128 """Raised when a mutation method has been called on a ConjectureData object
129 after freeze() has been called."""
130
131
132def __getattr__(name):
133 if name == "MultipleFailures":
134 from hypothesis._settings import note_deprecation
135 from hypothesis.internal.compat import BaseExceptionGroup
136
137 note_deprecation(
138 "MultipleFailures is deprecated; use the builtin `BaseExceptionGroup` type "
139 "instead, or `exceptiongroup.BaseExceptionGroup` before Python 3.11",
140 since="2022-08-02",
141 has_codemod=False, # This would be a great PR though!
142 stacklevel=1,
143 )
144 return BaseExceptionGroup
145
146 raise AttributeError(f"Module 'hypothesis.errors' has no attribute {name}")
147
148
149class DeadlineExceeded(_Trimmable):
150 """Raised when an individual test body has taken too long to run."""
151
152 def __init__(self, runtime, deadline):
153 super().__init__(
154 "Test took %.2fms, which exceeds the deadline of %.2fms"
155 % (runtime.total_seconds() * 1000, deadline.total_seconds() * 1000)
156 )
157 self.runtime = runtime
158 self.deadline = deadline
159
160 def __reduce__(self):
161 return (type(self), (self.runtime, self.deadline))
162
163
164class StopTest(BaseException):
165 """Raised when a test should stop running and return control to
166 the Hypothesis engine, which should then continue normally.
167 """
168
169 def __init__(self, testcounter):
170 super().__init__(repr(testcounter))
171 self.testcounter = testcounter
172
173
174class DidNotReproduce(HypothesisException):
175 pass
176
177
178class Found(Exception):
179 """Signal that the example matches condition. Internal use only."""
180
181 hypothesis_internal_never_escalate = True
182
183
184class RewindRecursive(Exception):
185 """Signal that the type inference should be rewound due to recursive types. Internal use only."""
186
187 def __init__(self, target):
188 self.target = target
189
190
191class SmallSearchSpaceWarning(HypothesisWarning):
192 """Indicates that an inferred strategy does not span the search space
193 in a meaningful way, for example by only creating default instances."""