errors.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. # coding=utf-8
  2. #
  3. # This file is part of Hypothesis, which may be found at
  4. # https://github.com/HypothesisWorks/hypothesis-python
  5. #
  6. # Most of this work is copyright (C) 2013-2018 David R. MacIver
  7. # (david@drmaciver.com), but it contains contributions by others. See
  8. # CONTRIBUTING.rst for a full list of people who may hold copyright, and
  9. # consult the git log if you need to determine who owns an individual
  10. # contribution.
  11. #
  12. # This Source Code Form is subject to the terms of the Mozilla Public License,
  13. # v. 2.0. If a copy of the MPL was not distributed with this file, You can
  14. # obtain one at http://mozilla.org/MPL/2.0/.
  15. #
  16. # END HEADER
  17. from __future__ import division, print_function, absolute_import
  18. class HypothesisException(Exception):
  19. """Generic parent class for exceptions thrown by Hypothesis."""
  20. class CleanupFailed(HypothesisException):
  21. """At least one cleanup task failed and no other exception was raised."""
  22. class UnsatisfiedAssumption(HypothesisException):
  23. """An internal error raised by assume.
  24. If you're seeing this error something has gone wrong.
  25. """
  26. class BadTemplateDraw(HypothesisException):
  27. """An internal error raised when something unfortunate happened during
  28. template generation and you should restart the draw, preferably with a new
  29. parameter.
  30. This is not an error condition internally, but if you ever see this
  31. in your code it's probably a Hypothesis bug
  32. """
  33. class NoSuchExample(HypothesisException):
  34. """The condition we have been asked to satisfy appears to be always false.
  35. This does not guarantee that no example exists, only that we were
  36. unable to find one.
  37. """
  38. def __init__(self, condition_string, extra=''):
  39. super(NoSuchExample, self).__init__(
  40. 'No examples found of condition %s%s' % (
  41. condition_string, extra)
  42. )
  43. class DefinitelyNoSuchExample(NoSuchExample): # pragma: no cover
  44. """Hypothesis used to be able to detect exhaustive coverage of a search
  45. space and no longer can.
  46. This exception remains for compatibility reasons for now but can
  47. never actually be thrown.
  48. """
  49. class NoExamples(HypothesisException):
  50. """Raised when example() is called on a strategy but we cannot find any
  51. examples after enough tries that we really should have been able to if this
  52. was ever going to work."""
  53. class Unsatisfiable(HypothesisException):
  54. """We ran out of time or examples before we could find enough examples
  55. which satisfy the assumptions of this hypothesis.
  56. This could be because the function is too slow. If so, try upping
  57. the timeout. It could also be because the function is using assume
  58. in a way that is too hard to satisfy. If so, try writing a custom
  59. strategy or using a better starting point (e.g if you are requiring
  60. a list has unique values you could instead filter out all duplicate
  61. values from the list)
  62. """
  63. class Flaky(HypothesisException):
  64. """This function appears to fail non-deterministically: We have seen it
  65. fail when passed this example at least once, but a subsequent invocation
  66. did not fail.
  67. Common causes for this problem are:
  68. 1. The function depends on external state. e.g. it uses an external
  69. random number generator. Try to make a version that passes all the
  70. relevant state in from Hypothesis.
  71. 2. The function is suffering from too much recursion and its failure
  72. depends sensitively on where it's been called from.
  73. 3. The function is timing sensitive and can fail or pass depending on
  74. how long it takes. Try breaking it up into smaller functions which
  75. don't do that and testing those instead.
  76. """
  77. class Timeout(Unsatisfiable):
  78. """We were unable to find enough examples that satisfied the preconditions
  79. of this hypothesis in the amount of time allotted to us."""
  80. class WrongFormat(HypothesisException, ValueError):
  81. """An exception indicating you have attempted to serialize a value that
  82. does not match the type described by this format."""
  83. class BadData(HypothesisException, ValueError):
  84. """The data that we got out of the database does not seem to match the data
  85. we could have put into the database given this schema."""
  86. class InvalidArgument(HypothesisException, TypeError):
  87. """Used to indicate that the arguments to a Hypothesis function were in
  88. some manner incorrect."""
  89. class ResolutionFailed(InvalidArgument):
  90. """Hypothesis had to resolve a type to a strategy, but this failed.
  91. Type inference is best-effort, so this only happens when an
  92. annotation exists but could not be resolved for a required argument
  93. to the target of ``builds()``, or where the user passed ``infer``.
  94. """
  95. class InvalidState(HypothesisException):
  96. """The system is not in a state where you were allowed to do that."""
  97. class InvalidDefinition(HypothesisException, TypeError):
  98. """Used to indicate that a class definition was not well put together and
  99. has something wrong with it."""
  100. class AbnormalExit(HypothesisException):
  101. """Raised when a test running in a child process exits without returning or
  102. raising an exception."""
  103. class FailedHealthCheck(HypothesisException, Warning):
  104. """Raised when a test fails a preliminary healthcheck that occurs before
  105. execution."""
  106. def __init__(self, message, check):
  107. super(FailedHealthCheck, self).__init__(message)
  108. self.health_check = check
  109. class HypothesisDeprecationWarning(HypothesisException, FutureWarning):
  110. pass
  111. class Frozen(HypothesisException):
  112. """Raised when a mutation method has been called on a ConjectureData object
  113. after freeze() has been called."""
  114. class MultipleFailures(HypothesisException):
  115. """Indicates that Hypothesis found more than one distinct bug when testing
  116. your code."""
  117. class DeadlineExceeded(HypothesisException):
  118. """Raised when an individual test body has taken too long to run."""
  119. def __init__(self, runtime, deadline):
  120. super(DeadlineExceeded, self).__init__((
  121. 'Test took %.2fms, which exceeds the deadline of '
  122. '%.2fms') % (runtime, deadline))
  123. self.runtime = runtime
  124. self.deadline = deadline
  125. class StopTest(BaseException):
  126. def __init__(self, testcounter):
  127. super(StopTest, self).__init__(repr(testcounter))
  128. self.testcounter = testcounter
  129. class DidNotReproduce(HypothesisException):
  130. pass