Start by defining a template, a partial AST to compare against. You can fill in
as many or as few fields as you want. For example, to check for assignment to
a, but ignore the value:
template = ast.Module(body=[ ast.Assign(targets=[ast.Name(id='a')]) ]) sample = ast.parse("a = 7") astcheck.assert_ast_like(sample, template)
astcheck provides some helpers for defining flexible templates; see Template building utilities.
assert_ast_like(sample, template, _path=None)¶
Check that the sample AST matches the template.
Raises a suitable subclass of
ASTMismatchif a difference is detected.
_pathparameter is used for recursion; you shouldn’t normally pass it.
Returns True if the sample AST matches the template.
The parameter order matters! Only fields present in
template will be
checked, so you can leave out bits of the code you don’t care about. Normally,
sample will be the result of the code you want to test, and
will be defined in your test file.
You may want to write more customised checks for part of the AST. To do so, you can attach ‘checker functions’ to any part of the template tree. Checker functions should accept two parameters: the node or value at the corresponding part of the sample tree, and the path to that node—a list of strings and integers representing the attribute and index access used to get there from the root of the sample tree.
If the value passed is not acceptable, the checker function should raise one of the exceptions described below. Otherwise, it should return with no exception. The return value is ignored.
For instance, this will test for a number literal less than 7:
def less_than_seven(node, path): if not isinstance(node, ast.Num): raise astcheck.ASTNodeTypeMismatch(path, node, ast.Num()) if node.n >= 7: raise astcheck.ASTMismatch(path+['n'], node.n, '< 7') template = ast.Expression(body=ast.BinOp(left=less_than_seven)) sample = ast.parse('4+9', mode='eval') astcheck.assert_ast_like(sample, template)
There are a few checker functions available in astcheck—see Template building utilities.
ASTMismatch(path, got, expected)¶
Base exception for differing ASTs.
The following exceptions are raised by
assert_ast_like(). They should
all produce useful error messages explaining which part of the AST differed and
ASTNodeTypeMismatch(path, got, expected)¶
An AST node was of the wrong type.
ASTNodeListMismatch(path, got, expected)¶
A list of AST nodes had the wrong length.
ASTPlainListMismatch(path, got, expected)¶
A list of non-AST objects did not match.
ast.Globalnode has a
nameslist of plain strings