AST Manipulation fun for when I have time
Sunday, November 2nd, 2008Nice asserts
Currently python asserts are of the form
assert EXPRESSION, MESSAGE
It’d be really nice if an assert of the form
assert a < b < c
would display display a message equivalent to this (with a = 2, b = 1, c = 3)
AssertaionError: 2 < 1 < 3
I think I can do this for most Boolean expressions by doing an AST transform. So something like
assert a.foo() < g.bar() != f("fizzle")
Would be transformed into:
_1, _2, _3, = a.foo(), g.bar(), f("fizzle")
assert _1 < _2 != _3, "%s < %s != %s" % (_1, _2, _3)
This would happen right before methods/functions were run. Hopefully, by using AST it will be more portable to other python implementations (This is the primary reason cited by Guido for why adding this type of assertion to the CPython implementation wont be supported).
Mutation Testing
Mutation testing plugin for nose. Mutate Boolean expressions and constants akin to Pester, only using an AST transform instead of text substitution. This could potentially increase the types of mutations possible (shuffle orders of statements executed, change parameters, etc.)
Difficulties:
- Determing what’s under test to change (perhaps use coverage/figleaf to figure out what to change?)
- Keeping track and reporting mutation results to tester (should be relatively straightforward)
- Doing this quickly — mutations usually take a long time. (Only mutate files specified by user?)
Genetic programming
AST tree manipulation can also be used to generate syntactically correct statements automatically. Assuming I find or create an acceptable cross-over algorithm (perhaps steal the one from pygp) AST will make this relatively straightforward.



