def check_fillable_node(node, path):
    if isinstance(node, (ast.Num, ast.Str)):
        return
    elif isinstance(node, ast.NameConstant) and (node.value in (True, False)):
        return

    raise astcheck.ASTMismatch(path, node, 'number, string or boolean')
Esempio n. 2
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)
Esempio n. 3
0
def check_fillable_node(node, path):
    if isinstance(node, (ast.Num, ast.Str)):
        return
    elif isinstance(node, ast.UnaryOp) and isinstance(node.operand, ast.Num):
        return
    elif (isinstance(node, ast.List) and isinstance(node.ctx, ast.Load)
          and check_list(node)):
        return
    elif isinstance(node, ast.NameConstant) and (node.value in (True, False)):
        return
    elif isinstance(node, ast.List):
        for n in node.elts:
            check_fillable_node(n, path)
        return
    elif isinstance(node, ast.Dict):
        for n in node.keys:
            check_fillable_node(n, path)
        for n in node.values:
            check_fillable_node(n, path)
        return

    raise astcheck.ASTMismatch(path, node,
                               'number, string, boolean, list or dict')
Esempio n. 4
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(dflt, sample_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)
Esempio n. 5
0
def must_not_exist_checker(node, path):
    """Checker function to ensure a field is empty"""
    if (node is not None) and (node != []):
        raise astcheck.ASTMismatch(path, node, "empty")
Esempio n. 6
0
def must_exist_checker(node, path):
    """Checker function to ensure a field is not empty"""
    if (node is None) or (node == []):
        raise astcheck.ASTMismatch(path, node, "non empty")