Exemple #1
0
def _validate_deadline(x):
    if isinstance(x, bool):
        note_deprecation(
            "The deadline=%r must be a duration in milliseconds, or None to disable."
            "  Boolean deadlines are treated as ints, and deprecated." % (x, ),
            since="2019-03-06",
        )
    if x is None:
        return x
    if isinstance(x, integer_types + (float, )):
        try:
            x = duration(milliseconds=x)
        except OverflowError:
            quiet_raise(
                InvalidArgument(
                    "deadline=%r is invalid, because it is too large to represent "
                    "as a timedelta. Use deadline=None to disable deadlines." %
                    (x, )))
    if isinstance(x, datetime.timedelta):
        if x <= datetime.timedelta(0):
            raise InvalidArgument(
                "deadline=%r is invalid, because it is impossible to meet a "
                "deadline <= 0. Use deadline=None to disable deadlines." %
                (x, ))
        return duration(seconds=x.total_seconds())
    raise InvalidArgument(
        "deadline=%r (type %s) must be a timedelta object, an integer or float number of milliseconds, "
        "or None to disable the per-test-case deadline." %
        (x, type(x).__name__))
def _validate_deadline(x):
    if isinstance(x, bool):
        note_deprecation(
            "The deadline=%r must be a duration in milliseconds, or None to disable."
            "  Boolean deadlines are treated as ints, and deprecated." % (x,),
            since="2019-03-06",
        )
    if x is None:
        return x
    if isinstance(x, integer_types + (float,)):
        try:
            x = duration(milliseconds=x)
        except OverflowError:
            quiet_raise(
                InvalidArgument(
                    "deadline=%r is invalid, because it is too large to represent "
                    "as a timedelta. Use deadline=None to disable deadlines." % (x,)
                )
            )
    if isinstance(x, datetime.timedelta):
        if x <= datetime.timedelta(0):
            raise InvalidArgument(
                "deadline=%r is invalid, because it is impossible to meet a "
                "deadline <= 0. Use deadline=None to disable deadlines." % (x,)
            )
        return duration(seconds=x.total_seconds())
    raise InvalidArgument(
        "deadline=%r (type %s) must be a timedelta object, an integer or float number of milliseconds, "
        "or None to disable the per-test-case deadline." % (x, type(x).__name__)
    )
    def __call__(self, specifier, settings=None):
        from hypothesis.searchstrategy.strategies import SearchStrategy

        if isinstance(specifier, SearchStrategy):
            return specifier

        self.load_deprecated_api()

        if settings is None:
            settings = Settings()

        try:
            result = super(StrategyExtMethod, self).__call__(
                specifier, settings)
        except NotImplementedError:
            quiet_raise(NotImplementedError((
                'Expected a SearchStrategy but got %r of type %s. '
                'Note: This is a NotImplementedError for legacy reasons and '
                'will become an InvalidArgumentError in Hypothesis 2.0.'
            ) % (specifier, type(specifier).__name__)))
        note_deprecation((
            'Conversion of %r to strategy is deprecated '
            'and will be removed in Hypothesis 2.0. Use %r instead.') % (
                specifier, result
        ), settings)

        assert isinstance(result, SearchStrategy)
        return result
Exemple #4
0
 def do_draw(self, data):
     try:
         rule = data.draw(st.sampled_from(self.rules).filter(self.is_valid))
     except HypothesisException:
         # FailedHealthCheck or UnsatisfiedAssumption depending on user settings.
         msg = u"No progress can be made from state %r" % (self.machine, )
         quiet_raise(InvalidDefinition(msg))
     return (rule, data.draw(rule.arguments_strategy))
 def do_draw(self, data):
     try:
         rule = data.draw(st.sampled_from(self.rules).filter(self.is_valid))
     except HypothesisException:
         # FailedHealthCheck or UnsatisfiedAssumption depending on user settings.
         msg = u"No progress can be made from state %r" % (self.machine,)
         quiet_raise(InvalidDefinition(msg))
     return (rule, data.draw(rule.arguments_strategy))
Exemple #6
0
 def reinterpret_bits(x, from_, to):
     if from_ == b"!e":
         arr = numpy.array([x], dtype=">f2")
         if numpy.isfinite(x) and not numpy.isfinite(arr[0]):
             quiet_raise(OverflowError("%r too large for float16" % (x, )))
         buf = arr.tobytes()
     else:
         buf = struct_pack(from_, x)
     if to == b"!e":
         return float(numpy.frombuffer(buf, dtype=">f2")[0])
     return struct_unpack(to, buf)[0]
Exemple #7
0
 def reinterpret_bits(x, from_, to):
     if from_ == b'!e':
         arr = numpy.array([x], dtype='>f2')
         if numpy.isfinite(x) and not numpy.isfinite(arr[0]):
             quiet_raise(OverflowError('%r too large for float16' % (x,)))
         buf = arr.tobytes()
     else:
         buf = struct_pack(from_, x)
     if to == b'!e':
         return float(numpy.frombuffer(buf, dtype='>f2')[0])
     return struct_unpack(to, buf)[0]
Exemple #8
0
    def do_draw(self, data):
        if not any(self.is_valid(rule) for rule in self.rules):
            msg = u"No progress can be made from state %r" % (self.machine, )
            quiet_raise(InvalidDefinition(msg))

        feature_flags = data.draw(self.enabled_rules_strategy)

        # Note: The order of the filters here is actually quite important,
        # because checking is_enabled makes choices, so increases the size of
        # the choice sequence. This means that if we are in a case where many
        # rules are invalid we will make a lot more choices if we ask if they
        # are enabled before we ask if they are valid, so our test cases will
        # be artificially large.
        rule = data.draw(
            st.sampled_from(self.rules).filter(self.is_valid).filter(
                lambda r: feature_flags.is_enabled(r.function.__name__)))

        return (rule, data.draw(rule.arguments_strategy))
Exemple #9
0
 def do_draw(self, data):
     if not any(self.is_valid(rule) for rule in self.rules):
         msg = u"No progress can be made from state %r" % (self.machine,)
         quiet_raise(InvalidDefinition(msg))
     rule = data.draw(st.sampled_from(self.rules).filter(self.is_valid))
     return (rule, data.draw(rule.arguments_strategy))