cosmic_ray.operators

break_continue

binary_operator_replacement

boolean_replacer

Implementation of the no-op operator.

class cosmic_ray.operators.no_op.NoOp

An operator that makes no changes.

This is primarily for baselining and debugging. It behaves like any other operator, but it makes no changes. Obviously this means that, if your test suite passes on unmutated code, it will still pass after applying this operator. Use with care.

classmethod examples()

Examples of the mutations that this operator can make.

This is primarily for testing purposes, but it could also be used for docmentation.

Each example is a tuple of the form (from-code, to-code, index). The index is optional and will be assumed to be 0 if it’s not included. The from-code is a string containing some Python code prior to mutation. The to-code is a string desribing the code after mutation. index indicates the occurrence of the application of the operator to the code (i.e. for when an operator can perform multiple mutation to a piece of code).

Returns: An iterable of example tuples.

mutate(node, index)

Mutate a node in an operator-specific manner.

Return the new, mutated node. Return None if the node has been deleted. Return node if there is no mutation at all for some reason.

mutation_positions(node)

All positions where this operator can mutate node.

An operator might be able to mutate a node in multiple ways, and this function should produce a position description for each of these mutations. Critically, if an operator can make multiple mutations to the same position, this should produce a position for each of these mutations (i.e. multiple identical positions).

Parameters:node – The AST node being mutated.
Returns:An iterable of ((start-line, start-col), (stop-line, stop-col)) tuples describing the locations where this operator will mutate node.

Implementation of operator base class.

class cosmic_ray.operators.operator.Operator

The mutation operator base class.

classmethod examples()

Examples of the mutations that this operator can make.

This is primarily for testing purposes, but it could also be used for docmentation.

Each example is a tuple of the form (from-code, to-code, index). The index is optional and will be assumed to be 0 if it’s not included. The from-code is a string containing some Python code prior to mutation. The to-code is a string desribing the code after mutation. index indicates the occurrence of the application of the operator to the code (i.e. for when an operator can perform multiple mutation to a piece of code).

Returns: An iterable of example tuples.

mutate(node, index)

Mutate a node in an operator-specific manner.

Return the new, mutated node. Return None if the node has been deleted. Return node if there is no mutation at all for some reason.

mutation_positions(node)

All positions where this operator can mutate node.

An operator might be able to mutate a node in multiple ways, and this function should produce a position description for each of these mutations. Critically, if an operator can make multiple mutations to the same position, this should produce a position for each of these mutations (i.e. multiple identical positions).

Parameters:node – The AST node being mutated.
Returns:An iterable of ((start-line, start-col), (stop-line, stop-col)) tuples describing the locations where this operator will mutate node.

Utilities for implementing operators.

cosmic_ray.operators.util.extend_name(suffix)

A factory for class decorators that modify the class name by appending some text to it.

Example:

@extend_name('_Foo')
class Class:
    pass

assert Class.__name__ == 'Class_Foo'