impunity.visitor

class impunity.visitor.Visitor(fun, ignore_warnings, ignore_methods)

Bases: NodeTransformer

Impunity AST visitor class checking for Annotations to transform the code if necessary

Attributes:
impunity_funcdict[tuple[str, str], Callable]

Dictionnary of Callables to keep track of functions tracked by impunity

uregpint.UnitRegistry

Unit Registry from Pint to manage UoMs.

classmethod add_func(fun)

Add function to the impunity function dictionnary

Return type:

None

generic_visit(node)

Called if no explicit visitor function exists for a node.

get_annotation_unit(node)

Return a UoM from an AST Node. Return None if the node is not compatible.

Parameters:

node (ast.expr with annotation) – Node with an annotation

Returns:

Optional str of the UoM

Return type:

Optional[str]

get_annotations(name, module=None)

Get annotations of a function found in the impunity_func class dict. Returns None if the function is not annotated.

Return type:

Optional[Dict[str, Any]]

Parameters:
  • namestr

    name of the function for which annotations are required

Returns:
  • Optional dict of annotations

get_node_unit(node)

Method to induce the unit of a node through recursive calls on children if any.

Return type:

QuantityNode

Args:

node (ast.AST): input node

Returns:

QuantityNode: QuantityNode(node, induced_unit)

node_convert(expected_unit, received_unit, received_node)

check if the expected and the received units are coherents with each other by using the Pint library. Modify the received node accordingly and returns it.

Return type:

expr

Parameters:
  • expected_unitstr

    expected Unit of Measure string

  • received_unitstr

    received Unit of Measure string

  • received_nodeQuantityNode

    received Quantity Node

Returns:
  • QuantityNode: Input Quantity Node, eventually modified for unit

coherence.

visit(root)

Initiate the visit of the root AST. Returns a checked ast.AST eventually modified to keep UoM coherence.

Return type:

AST

Args:
rootast.AST

root of the AST to visit

visit_AnnAssign(node)

Method called by the visitor if the visited node is an Annotated Assignement node. Checks the units in the node and returns it eventually modified.

Return type:

AnnAssign

Args:

node (ast.AnnAssign): input node

visit_Assign(node)

Method called by the visitor if the visited node is an Assignement node. Checks the units in the node and returns it eventually modified.

Return type:

Assign

Args:

node (ast.Assign): input node

visit_Call(node)

Method called by the visitor if the visited node is a Call node. Checks the units in the node and returns it eventually modified.

Return type:

Call

Args:

node (ast.Call): input node

visit_ClassDef(node)

Method called by the visitor if the visited node is a class defintion. Is can be the root node in impunity

Return type:

ClassDef

Args:

node (ast.ClassDef): Visited Class Definition

visit_For(node)

Method called by the visitor if the visited node is a for loop node. Checks the units in the node and returns it eventually modified.

Return type:

For

Args:

node (ast.For): input node

visit_FunctionDef(node)

Method called by the visitor if the visited node is function defintion. Is usually the root node in impunity

Return type:

FunctionDef

Args:

node (ast.FunctionDef): Visited Function Definition

visit_ListComp(node)

Method called by the visitor if the visited node is a List Comprehension node. Checks the units in the node and returns it eventually modified.

Return type:

ListComp

Args:

node (ast.ListComp): input node

visit_Return(node)

Method called by the visitor if the visited node is a Return node. Checks the units in the node and returns it eventually modified.

Return type:

Return

Args:

node (ast.Return): input node

visit_comprehension(node)

Method called by the visitor if the visited node is a Comprehension node. Checks the units in the node and returns it eventually modified.

Return type:

comprehension

Args:

node (ast.comprehension): input node