Ejemplo n.º 1
0
            def kwargs_checker(sample_keywords, path):
                sample_kwargs = {k.arg: k.value for k in sample_keywords}

                for k in template_keywords:
                    if k.arg == MULTIWILDCARD_NAME:
                        continue
                    if k.arg in sample_kwargs:
                        astcheck.assert_ast_like(sample_kwargs[k.arg], k.value,
                                                 path + [k.arg])
                    else:
                        raise astcheck.ASTMismatch(path, "(missing)",
                                                   "keyword arg %s" % k.arg)
Ejemplo n.º 2
0
    def __call__(self, sample_node, path):
        # Check positional-or-keyword args
        if self.args:
            if isinstance(self.args, list):
                astcheck._check_node_list(path + ["args"], sample_node.args,
                                          self.args)
            else:
                assert_ast_like(sample_node.args, self.args)

        # Check defaults for positional-or-keyword args
        if self.defaults:
            sample_args_w_defaults = sample_node.args[-len(sample_node.defaults
                                                           ):]
            sample_arg_defaults = {
                a.arg: d
                for a, d in zip(sample_args_w_defaults, sample_node.defaults)
            }
            for argname, dflt in self.defaults:
                try:
                    sample_dflt = sample_arg_defaults[argname]
                except KeyError:
                    raise astcheck.ASTMismatch(path + ["defaults", argname],
                                               "(missing default)", dflt)
                else:
                    assert_ast_like(sample_dflt, dflt,
                                    path + ["defaults", argname])

        # *args
        if self.vararg:
            assert_ast_like(sample_node.vararg, self.vararg)

        # keyword-only arguments
        sample_kwonlyargs = {
            k.arg: (k, d)
            for k, d in zip(sample_node.kwonlyargs, sample_node.kw_defaults)
        }

        for template_arg, template_dflt in self.kwonly_args_dflts:
            argname = template_arg.arg
            try:
                sample_arg, sample_dflt = sample_kwonlyargs[argname]
            except KeyError:
                raise astcheck.ASTMismatch(path + ["kwonlyargs"], "(missing)",
                                           "keyword arg %s" % argname)
            else:
                assert_ast_like(sample_arg, template_arg,
                                path + ["kwonlyargs", argname])
                if template_dflt is not None:
                    assert_ast_like(sample_dflt, template_dflt,
                                    path + ["kw_defaults", argname])

        # If keyword-only-args weren't wildcarded, then there shouldn't
        # be any more args in the sample than the template
        if not self.koa_subset:
            template_kwarg_names = {k.arg for k, d in self.kwonly_args_dflts}
            excess_names = set(sample_kwonlyargs) - template_kwarg_names
            if excess_names:
                raise astcheck.ASTMismatch(path + ["kwonlyargs"], excess_names,
                                           "(not present in template)")

        # **kwargs
        if self.kwarg:
            assert_ast_like(sample_node.kwarg, self.kwarg)