The AstroidBuilder makes astroid from living object and / or from _ast
The builder is not thread safe and can't be used to parse different sources at the same time.
Class | AstroidBuilder |
Class for building an astroid tree from source code or from a live module. |
Function | build_namespace_package_module |
Undocumented |
Function | extract_node |
Parses some Python code as a module and extracts a designated AST node. |
Function | open_source_file |
Undocumented |
Function | parse |
Parses a source string in order to obtain an astroid AST from it |
Constant | MISPLACED_TYPE_ANNOTATION_ERROR |
Undocumented |
Variable | objects |
Undocumented |
Function | _can_assign_attr |
Undocumented |
Function | _extract_expressions |
Find expressions in a call to _TRANSIENT_FUNCTION and extract them. |
Function | _find_statement_by_line |
Extracts the statement on a specific line from an AST. |
Function | _parse_string |
Undocumented |
Constant | _STATEMENT_SELECTOR |
Undocumented |
Constant | _TRANSIENT_FUNCTION |
Undocumented |
Parses some Python code as a module and extracts a designated AST node.
To extract one or more statement nodes, append #@ to the end of the line
>>> def x(): >>> def y(): >>> return 1 #@The return statement will be extracted.
>>> class X(object): >>> def meth(self): #@ >>> pass
The function object 'meth' will be extracted.
To extract arbitrary expressions, surround them with the fake function call __(...). After parsing, the surrounded expression will be returned and the whole AST (accessible via the returned node's parent attribute) will look like the function call was never there in the first place.
>>> a = __(1)
The const node will be extracted.
>>> def x(d=__(foo.bar)): pass
The node containing the default argument will be extracted.
>>> def foo(a, b): >>> return 0 < __(len(a)) < b
The node containing the function call 'len' will be extracted.
If no statements or expressions are selected, the last toplevel statement will be returned.
If the selected statement is a discard statement, (i.e. an expression turned into a statement), the wrapped expression is returned instead.
For convenience, singleton lists are unpacked.
a module. Will be passed through textwrap.dedent first. :param str module_name: The name of the module. :returns: The designated node from the parse tree, or a list of nodes.
Parameters | |
code:str | Undocumented |
module_name:str | Undocumented |
str code | A piece of Python code that is parsed as |
Returns | |
Union[ | Undocumented |
Parameters | |
code | Undocumented |
module_name | Undocumented |
path | Undocumented |
apply_transforms | Undocumented |
str code | The code for the module. |
str module_name | The name for the module, if any |
str path | The path for the module |
bool apply_transforms | Apply the transforms for the give code. Use it if you don't want the default transforms to be applied. |
Find expressions in a call to _TRANSIENT_FUNCTION and extract them.
The function walks the AST recursively to search for expressions that are wrapped into a call to _TRANSIENT_FUNCTION. If it finds such an expression, it completely removes the function call node from the tree, replacing it by the wrapped expression inside the parent.
expression can be found.
Parameters | |
node:astroid.bases.NodeNG | An astroid node. |
Yields | |
The sequence of wrapped expressions on the modified tree |
Extracts the statement on a specific line from an AST.
If the line number of node matches line, it will be returned; otherwise its children are iterated and the function is called recursively.
Parameters | |
node:astroid.bases.NodeNG | An astroid node. |
line:int | The line number of the statement to extract. |
Returns | |
astroid.bases.NodeNG or None | The statement on the line, or None if no statement for the line can be found. |