cosmic_ray.ast package¶
Submodules¶
cosmic_ray.ast.ast_query module¶
Tools for querying ASTs.
- class cosmic_ray.ast.ast_query.ASTQuery(obj)¶
Bases:
objectAllowing to navigate into any object and test attribute of any object:
Examples: >>> ASTQuery(node).parent.match(Node, type=’node’).ok
Test if node.parent isinstance of Node and node.parent.type == ‘node’ At each step (each ‘.’ (dot)) you receive an ObjTest object, then
Navigation: You can call any properties or functions of the base object >>> ASTQuery(node).parent.children[2].get_next_sibling()
Test: >>> ASTQuery(node).match(attr=’value’).match(Class) All in once: >>> ASTQuery(node).match(Class, attr=’value’)
Conditional navigation: >>> ASTQuery(node).IF.match(attr=’intermediate’).parent.FI
Final result: >>> ASTQuery(node).ok >>> bool(ASTQuery(node))
- property IF¶
Conditional navigation.
- get_definition_name()¶
Get the name of the function or class enclosing the current node.
- property ok¶
Is the query ok.
Module contents¶
Tools for working with parso ASTs.
- class cosmic_ray.ast.Visitor¶
Bases:
ABCAST visitor for parso trees.
This supports both simple traversal as well as editing of the tree.
- abstractmethod visit(node)¶
Called for each node in the walk.
This should return a node that will replace the node argument in the AST. This can be the node argument itself, a new node, or None. If None is returned, then the node is removed from the tree.
- Parameters:
node – The node currently being visited.
- Returns:
A node or None.
- walk(node)¶
Walk a parse tree, calling visit for each node.
- cosmic_ray.ast.ast_nodes(node)¶
Iterable of all nodes in a tree.
- Parameters:
node – The top node in a parso tree to iterate.
- Yields:
All of the nodes in the tree.
- cosmic_ray.ast.dump_node(node)¶
Generate string version of node.
- cosmic_ray.ast.get_ast(source: str)¶
Parse the AST for a code string.
- Parameters:
code (str) – _description_
- cosmic_ray.ast.get_ast_from_path(module_path: Path)¶
Get the AST for the code in a file.
- Parameters:
module_path – pathlib.Path to the file containing the code.
- Returns:
The parso parse tree for the code in module_path.
- cosmic_ray.ast.is_none(node)¶
Determine if a node is the None keyword.
- cosmic_ray.ast.is_number(node)¶
Determine if a node is a number.