Ejemplo n.º 1
0
    def __call__(
        self,
        args,
        kwargs,
        fname=None,
        max_fname_arg_count=None,
        method: Optional[str] = None,
    ) -> None:
        if args or kwargs:
            fname = self.fname if fname is None else fname
            max_fname_arg_count = (
                self.max_fname_arg_count
                if max_fname_arg_count is None
                else max_fname_arg_count
            )
            method = self.method if method is None else method

            if method == "args":
                validate_args(fname, args, max_fname_arg_count, self.defaults)
            elif method == "kwargs":
                validate_kwargs(fname, kwargs, self.defaults)
            elif method == "both":
                validate_args_and_kwargs(
                    fname, args, kwargs, max_fname_arg_count, self.defaults
                )
            else:
                raise ValueError(f"invalid validation method '{method}'")
Ejemplo n.º 2
0
def test_validation():
    # No exceptions should be raised.
    compat_args = {"foo": 1, "bar": None, "baz": -2}
    kwargs = {"baz": -2}

    args = (1, None)
    min_fname_arg_count = 2

    validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count,
                             compat_args)
Ejemplo n.º 3
0
def test_validation():
    # No exceptions should be raised.
    compat_args = OrderedDict()
    compat_args["foo"] = 1
    compat_args["bar"] = None
    compat_args["baz"] = -2
    kwargs = {"baz": -2}

    args = (1, None)
    min_fname_arg_count = 2

    validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count, compat_args)
Ejemplo n.º 4
0
def test_duplicate_argument():
    min_fname_arg_count = 2

    compat_args = {"foo": None, "bar": None, "baz": None}
    kwargs = {"foo": None, "bar": None}
    args = (None, )  # duplicate value for "foo"

    msg = fr"{_fname}\(\) got multiple values for keyword argument 'foo'"

    with pytest.raises(TypeError, match=msg):
        validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count,
                                 compat_args)
Ejemplo n.º 5
0
def test_missing_args_or_kwargs(args, kwargs):
    bad_arg = "bar"
    min_fname_arg_count = 2

    compat_args = {"foo": -5, bad_arg: 1}

    msg = (fr"the '{bad_arg}' parameter is not supported "
           fr"in the pandas implementation of {_fname}\(\)")

    with pytest.raises(ValueError, match=msg):
        validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count,
                                 compat_args)
Ejemplo n.º 6
0
    def test_validation(self):
        # No exceptions should be thrown
        compat_args = OrderedDict()
        compat_args['foo'] = 1
        compat_args['bar'] = None
        compat_args['baz'] = -2
        kwargs = {'baz': -2}
        args = (1, None)

        min_fname_arg_count = 2
        validate_args_and_kwargs(self.fname, args, kwargs, min_fname_arg_count,
                                 compat_args)
Ejemplo n.º 7
0
    def test_validation(self):
        # No exceptions should be thrown
        compat_args = OrderedDict()
        compat_args['foo'] = 1
        compat_args['bar'] = None
        compat_args['baz'] = -2
        kwargs = {'baz': -2}
        args = (1, None)

        min_fname_arg_count = 2
        validate_args_and_kwargs(self.fname, args, kwargs,
                                 min_fname_arg_count,
                                 compat_args)
def test_validation():
    # No exceptions should be raised.
    compat_args = OrderedDict()
    compat_args["foo"] = 1
    compat_args["bar"] = None
    compat_args["baz"] = -2
    kwargs = {"baz": -2}

    args = (1, None)
    min_fname_arg_count = 2

    validate_args_and_kwargs(_fname, args, kwargs,
                             min_fname_arg_count,
                             compat_args)
Ejemplo n.º 9
0
def test_invalid_total_length_max_length_one():
    compat_args = ("foo", )
    kwargs = {"foo": "FOO"}
    args = ("FoO", "BaZ")

    min_fname_arg_count = 0
    max_length = len(compat_args) + min_fname_arg_count
    actual_length = len(kwargs) + len(args) + min_fname_arg_count

    msg = (fr"{_fname}\(\) takes at most {max_length} "
           fr"argument \({actual_length} given\)")

    with pytest.raises(TypeError, match=msg):
        validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count,
                                 compat_args)
Ejemplo n.º 10
0
    def test_duplicate_argument(self):
        min_fname_arg_count = 2
        compat_args = OrderedDict()
        compat_args['foo'] = None
        compat_args['bar'] = None
        compat_args['baz'] = None
        kwargs = {'foo': None, 'bar': None}
        args = (None, )  # duplicate value for 'foo'

        msg = (r"{fname}\(\) got multiple values for keyword "
               r"argument '{arg}'".format(fname=self.fname, arg='foo'))

        with tm.assert_raises_regex(TypeError, msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count, compat_args)
Ejemplo n.º 11
0
def test_missing_args_or_kwargs(args, kwargs):
    bad_arg = "bar"
    min_fname_arg_count = 2

    compat_args = OrderedDict()
    compat_args["foo"] = -5
    compat_args[bad_arg] = 1

    msg = (r"the '{arg}' parameter is not supported "
           r"in the pandas implementation of {func}\(\)".format(arg=bad_arg,
                                                                func=_fname))

    with pytest.raises(ValueError, match=msg):
        validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count,
                                 compat_args)
def test_missing_args_or_kwargs(args, kwargs):
    bad_arg = "bar"
    min_fname_arg_count = 2

    compat_args = OrderedDict()
    compat_args["foo"] = -5
    compat_args[bad_arg] = 1

    msg = (r"the '{arg}' parameter is not supported "
           r"in the pandas implementation of {func}\(\)".
           format(arg=bad_arg, func=_fname))

    with pytest.raises(ValueError, match=msg):
        validate_args_and_kwargs(_fname, args, kwargs,
                                 min_fname_arg_count, compat_args)
Ejemplo n.º 13
0
    def test_duplicate_argument(self):
        min_fname_arg_count = 2
        compat_args = OrderedDict()
        compat_args['foo'] = None
        compat_args['bar'] = None
        compat_args['baz'] = None
        kwargs = {'foo': None, 'bar': None}
        args = (None,)  # duplicate value for 'foo'

        msg = (r"{fname}\(\) got multiple values for keyword "
               r"argument '{arg}'".format(fname=self.fname, arg='foo'))

        with tm.assert_raises_regex(TypeError, msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count,
                                     compat_args)
Ejemplo n.º 14
0
    def test_invalid_total_length_max_length_multiple(self):
        compat_args = ('foo', 'bar', 'baz')
        kwargs = {'foo': 'FOO', 'bar': 'BAR'}
        args = ('FoO', 'BaZ')

        min_fname_arg_count = 2
        max_length = len(compat_args) + min_fname_arg_count
        actual_length = len(kwargs) + len(args) + min_fname_arg_count
        msg = (r"{fname}\(\) takes at most {max_length} "
               r"arguments \({actual_length} given\)"
               .format(fname=self.fname, max_length=max_length,
                       actual_length=actual_length))

        with tm.assert_raises_regex(TypeError, msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count,
                                     compat_args)
Ejemplo n.º 15
0
    def test_invalid_total_length_max_length_one(self):
        compat_args = ('foo',)
        kwargs = {'foo': 'FOO'}
        args = ('FoO', 'BaZ')

        min_fname_arg_count = 0
        max_length = len(compat_args) + min_fname_arg_count
        actual_length = len(kwargs) + len(args) + min_fname_arg_count
        msg = (r"{fname}\(\) takes at most {max_length} "
               r"argument \({actual_length} given\)"
               .format(fname=self.fname, max_length=max_length,
                       actual_length=actual_length))

        with pytest.raises(TypeError, match=msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count,
                                     compat_args)
Ejemplo n.º 16
0
def test_duplicate_argument():
    min_fname_arg_count = 2

    compat_args = OrderedDict()
    compat_args["foo"] = None
    compat_args["bar"] = None
    compat_args["baz"] = None

    kwargs = {"foo": None, "bar": None}
    args = (None, )  # duplicate value for "foo"

    msg = r"{fname}\(\) got multiple values for keyword " r"argument '{arg}'".format(
        fname=_fname, arg="foo")

    with pytest.raises(TypeError, match=msg):
        validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count,
                                 compat_args)
Ejemplo n.º 17
0
    def test_invalid_total_length_max_length_one(self):
        compat_args = ('foo', )
        kwargs = {'foo': 'FOO'}
        args = ('FoO', 'BaZ')

        min_fname_arg_count = 0
        max_length = len(compat_args) + min_fname_arg_count
        actual_length = len(kwargs) + len(args) + min_fname_arg_count
        msg = (r"{fname}\(\) takes at most {max_length} "
               r"argument \({actual_length} given\)".format(
                   fname=self.fname,
                   max_length=max_length,
                   actual_length=actual_length))

        with pytest.raises(TypeError, match=msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count, compat_args)
Ejemplo n.º 18
0
    def test_invalid_total_length_max_length_multiple(self):
        compat_args = ('foo', 'bar', 'baz')
        kwargs = {'foo': 'FOO', 'bar': 'BAR'}
        args = ('FoO', 'BaZ')

        min_fname_arg_count = 2
        max_length = len(compat_args) + min_fname_arg_count
        actual_length = len(kwargs) + len(args) + min_fname_arg_count
        msg = (r"{fname}\(\) takes at most {max_length} "
               r"arguments \({actual_length} given\)"
               .format(fname=self.fname, max_length=max_length,
                       actual_length=actual_length))

        with tm.assert_raises_regex(TypeError, msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count,
                                     compat_args)
def test_duplicate_argument():
    min_fname_arg_count = 2

    compat_args = OrderedDict()
    compat_args["foo"] = None
    compat_args["bar"] = None
    compat_args["baz"] = None

    kwargs = {"foo": None, "bar": None}
    args = (None,)  # duplicate value for "foo"

    msg = (r"{fname}\(\) got multiple values for keyword "
           r"argument '{arg}'".format(fname=_fname, arg="foo"))

    with pytest.raises(TypeError, match=msg):
        validate_args_and_kwargs(_fname, args, kwargs,
                                 min_fname_arg_count,
                                 compat_args)
Ejemplo n.º 20
0
def test_invalid_total_length_max_length_multiple():
    compat_args = ("foo", "bar", "baz")
    kwargs = {"foo": "FOO", "bar": "BAR"}
    args = ("FoO", "BaZ")

    min_fname_arg_count = 2
    max_length = len(compat_args) + min_fname_arg_count
    actual_length = len(kwargs) + len(args) + min_fname_arg_count

    msg = (r"{fname}\(\) takes at most {max_length} "
           r"arguments \({actual_length} given\)".format(
               fname=_fname,
               max_length=max_length,
               actual_length=actual_length))

    with pytest.raises(TypeError, match=msg):
        validate_args_and_kwargs(_fname, args, kwargs, min_fname_arg_count,
                                 compat_args)
def test_invalid_total_length_max_length_multiple():
    compat_args = ("foo", "bar", "baz")
    kwargs = {"foo": "FOO", "bar": "BAR"}
    args = ("FoO", "BaZ")

    min_fname_arg_count = 2
    max_length = len(compat_args) + min_fname_arg_count
    actual_length = len(kwargs) + len(args) + min_fname_arg_count

    msg = (r"{fname}\(\) takes at most {max_length} "
           r"arguments \({actual_length} given\)"
           .format(fname=_fname, max_length=max_length,
                   actual_length=actual_length))

    with pytest.raises(TypeError, match=msg):
        validate_args_and_kwargs(_fname, args, kwargs,
                                 min_fname_arg_count,
                                 compat_args)
Ejemplo n.º 22
0
    def __call__(self, args, kwargs, fname=None,
                 max_fname_arg_count=None, method=None):
        if args or kwargs:
            fname = self.fname if fname is None else fname
            max_fname_arg_count = (self.max_fname_arg_count if
                                   max_fname_arg_count is None
                                   else max_fname_arg_count)
            method = self.method if method is None else method

            if method == 'args':
                validate_args(fname, args, max_fname_arg_count, self.defaults)
            elif method == 'kwargs':
                validate_kwargs(fname, kwargs, self.defaults)
            elif method == 'both':
                validate_args_and_kwargs(fname, args, kwargs,
                                         max_fname_arg_count,
                                         self.defaults)
            else:
                raise ValueError("invalid validation method "
                                 "'{method}'".format(method=method))
Ejemplo n.º 23
0
    def __call__(self, args, kwargs, fname=None,
                 max_fname_arg_count=None, method=None):
        if args or kwargs:
            fname = self.fname if fname is None else fname
            max_fname_arg_count = (self.max_fname_arg_count if
                                   max_fname_arg_count is None
                                   else max_fname_arg_count)
            method = self.method if method is None else method

            if method == 'args':
                validate_args(fname, args, max_fname_arg_count, self.defaults)
            elif method == 'kwargs':
                validate_kwargs(fname, kwargs, self.defaults)
            elif method == 'both':
                validate_args_and_kwargs(fname, args, kwargs,
                                         max_fname_arg_count,
                                         self.defaults)
            else:
                raise ValueError("invalid validation method "
                                 "'{method}'".format(method=method))
Ejemplo n.º 24
0
    def test_no_args_with_kwargs(self):
        bad_arg = 'bar'
        min_fname_arg_count = 2

        compat_args = OrderedDict()
        compat_args['foo'] = -5
        compat_args[bad_arg] = 1

        msg = (r"the '{arg}' parameter is not supported "
               r"in the pandas implementation of {func}\(\)".
               format(arg=bad_arg, func=self.fname))

        args = ()
        kwargs = {'foo': -5, bad_arg: 2}
        with pytest.raises(ValueError, match=msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count, compat_args)

        args = (-5, 2)
        kwargs = {}
        with pytest.raises(ValueError, match=msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count, compat_args)
Ejemplo n.º 25
0
    def test_no_args_with_kwargs(self):
        bad_arg = 'bar'
        min_fname_arg_count = 2

        compat_args = OrderedDict()
        compat_args['foo'] = -5
        compat_args[bad_arg] = 1

        msg = (r"the '{arg}' parameter is not supported "
               r"in the pandas implementation of {func}\(\)".format(
                   arg=bad_arg, func=self.fname))

        args = ()
        kwargs = {'foo': -5, bad_arg: 2}
        with pytest.raises(ValueError, match=msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count, compat_args)

        args = (-5, 2)
        kwargs = {}
        with pytest.raises(ValueError, match=msg):
            validate_args_and_kwargs(self.fname, args, kwargs,
                                     min_fname_arg_count, compat_args)