def composite(f): """Defines a strategy that is built out of potentially arbitrarily many other strategies. This is intended to be used as a decorator. See the full documentation for more details about how to use this function. """ from hypothesis.internal.reflection import copy_argspec argspec = getargspec(f) if ( argspec.defaults is not None and len(argspec.defaults) == len(argspec.args) ): raise InvalidArgument( 'A default value for initial argument will never be used') if len(argspec.args) == 0 and not argspec.varargs: raise InvalidArgument( 'Functions wrapped with composite must take at least one ' 'positional argument.' ) new_argspec = ArgSpec( args=argspec.args[1:], varargs=argspec.varargs, keywords=argspec.keywords, defaults=argspec.defaults ) @defines_strategy @copy_argspec(f.__name__, new_argspec) def accept(*args, **kwargs): class CompositeStrategy(SearchStrategy): def do_draw(self, data): return f(data.draw, *args, **kwargs) return CompositeStrategy() return accept
def test_define_function_signature_validates_function_name(): with raises(ValueError): define_function_signature('hello world', None, ArgSpec( args=['a', 'b'], varargs=None, keywords=None, defaults=None))
def test_copy_argspec_validates_function_name(): with raises(ValueError): copy_argspec('hello world', ArgSpec( args=['a', 'b'], varargs=None, keywords=None, defaults=None))
def test_copy_argspec_validates_arguments(): with raises(ValueError): copy_argspec('hello_world', ArgSpec( args=['a b'], varargs=None, keywords=None, defaults=None))