def test_populates_defaults(): def bar(x=[], y=1): pass assert convert_keyword_arguments(bar, (), {}) == (([], 1), {}) assert convert_keyword_arguments(bar, (), {'y': 42}) == (([], 42), {}) do_conversion_test(bar, (), {}) do_conversion_test(bar, (1,), {})
def test_leaves_unknown_kwargs_in_dict(): def bar(x, **kwargs): pass assert convert_keyword_arguments(bar, (1,), {u"foo": u"hi"}) == ((1,), {u"foo": u"hi"}) assert convert_keyword_arguments(bar, (), {u"x": 1, u"foo": u"hi"}) == ((1,), {u"foo": u"hi"}) do_conversion_test(bar, (1,), {}) do_conversion_test(bar, (), {u"x": 1, u"y": 1})
def test_populates_defaults(): def bar(x=[], y=1): pass assert convert_keyword_arguments(bar, (), {}) == (([], 1), {}) assert convert_keyword_arguments(bar, (), {"y": 42}) == (([], 42), {}) do_conversion_test(bar, (), {}) do_conversion_test(bar, (1, ), {})
def test_errors_on_extra_kwargs(): def foo(a): pass with raises(TypeError, match="keyword"): convert_keyword_arguments(foo, (1, ), {"b": 1}) with raises(TypeError, match="keyword"): convert_keyword_arguments(foo, (1, ), {"b": 1, "c": 2})
def test_simple_conversion(): def foo(a, b, c): return (a, b, c) assert convert_keyword_arguments(foo, (1, 2, 3), {}) == ((1, 2, 3), {}) assert convert_keyword_arguments(foo, (), {u"a": 3, u"b": 2, u"c": 1}) == ((3, 2, 1), {}) do_conversion_test(foo, (1, 0), {u"c": 2}) do_conversion_test(foo, (1,), {u"c": 2, u"b": u"foo"})
def test_errors_on_extra_kwargs(): def foo(a): pass # pragma: no cover with pytest.raises(TypeError) as e: convert_keyword_arguments(foo, (1, ), {u'b': 1}) assert u'keyword' in e.value.args[0] with pytest.raises(TypeError) as e2: convert_keyword_arguments(foo, (1, ), {u'b': 1, u'c': 2}) assert u'keyword' in e2.value.args[0]
def test_simple_conversion(): def foo(a, b, c): return (a, b, c) assert convert_keyword_arguments( foo, (1, 2, 3), {}) == ((1, 2, 3), {}) assert convert_keyword_arguments( foo, (), {'a': 3, 'b': 2, 'c': 1}) == ((3, 2, 1), {}) do_conversion_test(foo, (1, 0), {'c': 2}) do_conversion_test(foo, (1,), {'c': 2, 'b': 'foo'})
def test_errors_on_extra_kwargs(): def foo(a): pass with raises(TypeError) as e: convert_keyword_arguments(foo, (1, ), {"b": 1}) assert "keyword" in e.value.args[0] with raises(TypeError) as e2: convert_keyword_arguments(foo, (1, ), {"b": 1, "c": 2}) assert "keyword" in e2.value.args[0]
def test_errors_on_extra_kwargs(): def foo(a): pass # pragma: no cover with pytest.raises(TypeError) as e: convert_keyword_arguments(foo, (1,), {'b': 1}) assert 'keyword' in e.value.args[0] with pytest.raises(TypeError) as e2: convert_keyword_arguments(foo, (1,), {'b': 1, 'c': 2}) assert 'keyword' in e2.value.args[0]
def test_leaves_unknown_kwargs_in_dict(): def bar(x, **kwargs): pass assert convert_keyword_arguments(bar, (1,), {"foo": "hi"}) == ((1,), {"foo": "hi"}) assert convert_keyword_arguments(bar, (), {"x": 1, "foo": "hi"}) == ( (1,), {"foo": "hi"}, ) do_conversion_test(bar, (1,), {}) do_conversion_test(bar, (), {"x": 1, "y": 1})
def test_leaves_unknown_kwargs_in_dict(): def bar(x, **kwargs): pass assert convert_keyword_arguments(bar, (1,), {'foo': 'hi'}) == ( (1,), {'foo': 'hi'} ) assert convert_keyword_arguments(bar, (), {'x': 1, 'foo': 'hi'}) == ( (1,), {'foo': 'hi'} ) do_conversion_test(bar, (1,), {}) do_conversion_test(bar, (), {'x': 1, 'y': 1})
def test_simple_conversion(): def foo(a, b, c): return (a, b, c) assert convert_keyword_arguments(foo, (1, 2, 3), {}) == ((1, 2, 3), {}) assert convert_keyword_arguments(foo, (), {"a": 3, "b": 2, "c": 1}) == ( (3, 2, 1), {}, ) do_conversion_test(foo, (1, 0), {"c": 2}) do_conversion_test(foo, (1,), {"c": 2, "b": "foo"})
def test_passes_varargs_correctly(): def foo(*args): pass assert convert_keyword_arguments(foo, (1, 2, 3), {}) == ((1, 2, 3), {}) do_conversion_test(foo, (1, 2, 3), {})
def __repr__(self): if self.__representation is None: _args = self.__args _kwargs = self.__kwargs argspec = getargspec(self.__function) defaults = {} if argspec.defaults is not None: for k in hrange(1, len(argspec.defaults) + 1): defaults[argspec.args[-k]] = argspec.defaults[-k] if len(argspec.args) > 1 or argspec.defaults: _args, _kwargs = convert_positional_arguments( self.__function, _args, _kwargs) else: _args, _kwargs = convert_keyword_arguments( self.__function, _args, _kwargs) kwargs_for_repr = dict(_kwargs) for k, v in defaults.items(): if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]: del kwargs_for_repr[k] self.__representation = '%s(%s)' % ( self.__function.__name__, arg_string( self.__function, _args, kwargs_for_repr, reorder=False), ) return self.__representation
def __repr__(self): if self.__representation is None: sig = signature(self.function) pos = [ p for p in sig.parameters.values() if "POSITIONAL" in p.kind.name ] if len(pos) > 1 or any(p.default is not sig.empty for p in pos): _args, _kwargs = convert_positional_arguments( self.function, self.__args, self.__kwargs) else: _args, _kwargs = convert_keyword_arguments( self.function, self.__args, self.__kwargs) kwargs_for_repr = { k: v for k, v in _kwargs.items() if k not in sig.parameters or v is not sig.parameters[k].default } self.__representation = "{}({}){}".format( self.function.__name__, arg_string(self.function, _args, kwargs_for_repr, reorder=False), "".join(map(_repr_filter, self.__filters)), ) return self.__representation
def __repr__(self): if self.__representation is None: _args = self.__args _kwargs = self.__kwargs argspec = getfullargspec(self.__function) defaults = dict(argspec.kwonlydefaults or {}) if argspec.defaults is not None: for name, value in zip(reversed(argspec.args), reversed(argspec.defaults)): defaults[name] = value if len(argspec.args) > 1 or argspec.defaults: _args, _kwargs = convert_positional_arguments( self.__function, _args, _kwargs) else: _args, _kwargs = convert_keyword_arguments( self.__function, _args, _kwargs) kwargs_for_repr = dict(_kwargs) for k, v in defaults.items(): if k in kwargs_for_repr and kwargs_for_repr[k] is defaults[k]: del kwargs_for_repr[k] self.__representation = '%s(%s)' % ( self.__function.__name__, arg_string( self.__function, _args, kwargs_for_repr, reorder=False), ) return self.__representation
def do_conversion_test(f, args, kwargs): result = f(*args, **kwargs) cargs, ckwargs = convert_keyword_arguments(f, args, kwargs) assert result == f(*cargs, **ckwargs) cargs2, ckwargs2 = convert_positional_arguments(f, args, kwargs) assert result == f(*cargs2, **ckwargs2)
def test_errors_on_bad_kwargs(): def bar(): pass # pragma: no cover with pytest.raises(TypeError): convert_keyword_arguments(bar, (), {'foo': 1})
def test_errors_on_bad_kwargs(): def bar(): pass with raises(TypeError): convert_keyword_arguments(bar, (), {'foo': 1})
def test_errors_if_keyword_precedes_positional(): def foo(x, y): pass # pragma: no cover with pytest.raises(TypeError): convert_keyword_arguments(foo, (1, ), {u'x': 2})
def test_errors_if_not_enough_args(): def foo(a, b, c, d=1): pass # pragma: no cover with pytest.raises(TypeError): convert_keyword_arguments(foo, (1, 2), {u'd': 4})
def test_errors_on_bad_kwargs(): def bar(): pass # pragma: no cover with pytest.raises(TypeError): convert_keyword_arguments(bar, (), {u'foo': 1})
def test_errors_if_not_enough_args(): def foo(a, b, c, d=1): pass # pragma: no cover with pytest.raises(TypeError): convert_keyword_arguments(foo, (1, 2), {'d': 4})
def test_errors_if_keyword_precedes_positional(): def foo(x, y): pass with raises(TypeError): convert_keyword_arguments(foo, (1, ), {"x": 2})
def test_errors_if_not_enough_args(): def foo(a, b, c, d=1): pass with raises(TypeError): convert_keyword_arguments(foo, (1, 2), {"d": 4})
def test_errors_if_keyword_precedes_positional(): def foo(x, y): pass # pragma: no cover with pytest.raises(TypeError): convert_keyword_arguments(foo, (1,), {'x': 2})
def test_errors_on_bad_kwargs(): def bar(): pass with raises(TypeError): convert_keyword_arguments(bar, (), {"foo": 1})