module documentation

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
Function _can_assign_attr Undocumented
Function _extract_expressions Find expressions in a call to _TRANSIENT_FUNCTION and extract them.
Function _extract_single_node Call extract_node while making sure that only one value is returned.
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
def build_namespace_package_module(name: str, path: Sequence[str]) -> nodes.Module: (source)

Undocumented

def extract_node(code: str, module_name: str = '') -> nodes.NodeNG|list[nodes.NodeNG]: (source)

Parses some Python code as a module and extracts a designated AST node. Statements: To extract one or more statement nodes, append #@ to the end of the line Examples: >>> 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. Expressions: 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. Examples: >>> 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. :param str code: A piece of Python code that is parsed as 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.

def open_source_file(filename: str) -> tuple[TextIOWrapper, str, str]: (source)

Undocumented

def parse(code: str, module_name: str = '', path: str|None = None, apply_transforms: bool = True) -> nodes.Module: (source)

Parses a source string in order to obtain an astroid AST from it. :param str code: The code for the module. :param str module_name: The name for the module, if any :param str path: The path for the module :param bool apply_transforms: Apply the transforms for the give code. Use it if you don't want the default transforms to be applied.

MISPLACED_TYPE_ANNOTATION_ERROR: str = (source)

Undocumented

Value
'misplaced type annotation'
def _can_assign_attr(node: nodes.ClassDef, attrname: str|None) -> bool: (source)

Undocumented

def _extract_expressions(node: nodes.NodeNG) -> Iterator[nodes.NodeNG]: (source)

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. :param node: An astroid node. :type node: astroid.bases.NodeNG :yields: The sequence of wrapped expressions on the modified tree expression can be found.

def _extract_single_node(code: str, module_name: str = '') -> nodes.NodeNG: (source)

Call extract_node while making sure that only one value is returned.

def _find_statement_by_line(node: nodes.NodeNG, line: int) -> nodes.NodeNG|None: (source)

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. :param node: An astroid node. :type node: astroid.bases.NodeNG :param line: The line number of the statement to extract. :type line: int :returns: The statement on the line, or None if no statement for the line can be found. :rtype: astroid.bases.NodeNG or None

def _parse_string(data: str, type_comments: bool = True) -> tuple[ast.Module, ParserModule]: (source)

Undocumented

_STATEMENT_SELECTOR: str = (source)

Undocumented

Value
'#@'
_TRANSIENT_FUNCTION: str = (source)

Undocumented

Value
'__'