Base Nodes¶
These are abstract node classes that other nodes inherit from.
astroid.mixins.AssignTypeMixin |
|
astroid.node_classes._BaseContainer ([…]) |
Base class for Set, FrozenSet, Tuple and List. |
astroid.mixins.BlockRangeMixIn |
override block range |
astroid.scoped_nodes.ComprehensionScope ([…]) |
Scoping for different types of comprehensions. |
astroid.mixins.FilterStmtsMixin |
Mixin for statement filtering and assignment type |
astroid.mixins.ImportFromMixin |
MixIn for From and Import Nodes |
astroid.scoped_nodes._ListComp ([lineno, …]) |
Class representing an ast.ListComp node. |
astroid.scoped_nodes.LocalsDictNodeNG ([…]) |
this class provides locals handling common to Module, FunctionDef and ClassDef nodes, including a dict like interface for direct access to locals information |
astroid.node_classes.LookupMixIn |
Mixin to look up a name in the right scope. |
astroid.node_classes.NodeNG ([lineno, …]) |
A node of the new Abstract Syntax Tree (AST). |
astroid.mixins.ParentAssignTypeMixin |
|
astroid.node_classes.Statement ([lineno, …]) |
Statement node adding a few attributes |
-
class
astroid.node_classes.
_BaseContainer
(lineno=None, col_offset=None, parent=None)[source]¶ Bases:
astroid.mixins.ParentAssignTypeMixin
,astroid.node_classes.NodeNG
,astroid.bases.Instance
Base class for Set, FrozenSet, Tuple and List.
-
bool_value
(context=None)[source]¶ Determine the boolean value of this node.
Returns: The boolean value of this node. Return type: bool or Uninferable
-
classmethod
from_elements
(elts=None)[source]¶ Create a node of this type from the given list of elements.
Parameters: elts (list(NodeNG)) – The list of elements that the node should contain. Returns: A new node containing the given elements. Return type: NodeNG
-
get_children
()[source]¶ Get the child nodes below this node.
Returns: The children. Return type: iterable(NodeNG)
-
itered
()[source]¶ An iterator over the elements this node contains.
Returns: The contents of this node. Return type: iterable(NodeNG)
-
-
class
astroid.mixins.
BlockRangeMixIn
[source]¶ Bases:
object
override block range
-
blockstart_tolineno
¶ <wrapped by the cachedproperty decorator>
-
-
class
astroid.scoped_nodes.
ComprehensionScope
(lineno=None, col_offset=None, parent=None)[source]¶ Bases:
astroid.scoped_nodes.LocalsDictNodeNG
Scoping for different types of comprehensions.
-
frame
()[source]¶ The first parent frame node.
A frame node is a
Module
,FunctionDef
, orClassDef
.Returns: The first parent frame node. Return type: Module or FunctionDef or ClassDef
-
scope_lookup
(node, name, offset=0)¶ XXX method for interfacing the scope lookup
-
-
class
astroid.mixins.
FilterStmtsMixin
[source]¶ Bases:
object
Mixin for statement filtering and assignment type
-
class
astroid.mixins.
ImportFromMixin
[source]¶ Bases:
astroid.mixins.FilterStmtsMixin
MixIn for From and Import Nodes
-
class
astroid.scoped_nodes.
_ListComp
(lineno=None, col_offset=None, parent=None)[source]¶ Bases:
astroid.node_classes.NodeNG
Class representing an
ast.ListComp
node.>>> node = astroid.extract_node('[thing for thing in things if thing]') >>> node <ListComp l.1 at 0x7f23b2e418d0>
-
bool_value
(context=None)[source]¶ Determine the boolean value of this node.
Returns: The boolean value of this node. For a ListComp
this is alwaysUninferable
.Return type: Uninferable
-
generators
= None¶ The generators that are looped through.
Type: list(Comprehension) or None
-
get_children
()[source]¶ Get the child nodes below this node.
Returns: The children. Return type: iterable(NodeNG)
-
postinit
(elt=None, generators=None)[source]¶ Do some setup after initialisation.
Parameters: - elt (NodeNG or None) – The element that forms the output of the expression.
- generators (list(Comprehension) or None) – The generators that are looped through.
-
-
class
astroid.scoped_nodes.
LocalsDictNodeNG
(lineno=None, col_offset=None, parent=None)[source]¶ Bases:
astroid.node_classes.LookupMixIn
,astroid.node_classes.NodeNG
this class provides locals handling common to Module, FunctionDef and ClassDef nodes, including a dict like interface for direct access to locals information
-
add_local_node
(child_node, name=None)[source]¶ Append a child that should alter the locals of this scope node.
Parameters: - child_node (NodeNG) – The child node that will alter locals.
- name (str or None) – The name of the local that will be altered by the given child node.
-
frame
()[source]¶ The first parent frame node.
A frame node is a
Module
,FunctionDef
, orClassDef
.Returns: The first parent frame node. Return type: Module or FunctionDef or ClassDef
-
items
()[source]¶ Get the names of the locals and the node that defines the local.
Returns: The names of locals and their associated node. Return type: list(tuple(str, NodeNG))
-
keys
()[source]¶ The names of locals defined in this scoped node.
Returns: The names of the defined locals. Return type: list(str)
-
locals
= {}¶ A map of the name of a local variable to the node defining the local.
Type: dict(str, NodeNG)
-
qname
()[source]¶ Get the ‘qualified’ name of the node.
For example: module.name, module.class.name …
Returns: The qualified name. Return type: str
-
scope
()[source]¶ The first parent node defining a new scope.
Returns: The first parent scope node. Return type: Module or FunctionDef or ClassDef or Lambda or GenExpr
-
-
class
astroid.node_classes.
LookupMixIn
[source]¶ Bases:
object
Mixin to look up a name in the right scope.
-
ilookup
(name)[source]¶ Lookup the inferred values of the given variable.
Parameters: name (str) – The variable name to find values for. Returns: The inferred values of the statements returned from lookup()
.Return type: iterable
-
lookup
[source]¶ Lookup where the given variable is assigned.
The lookup starts from self’s scope. If self is not a frame itself and the name is found in the inner frame locals, statements will be filtered to remove ignorable statements according to self’s location.
Parameters: name (str) – The name of the variable to find assignments for. Returns: The scope node and the list of assignments associated to the given name according to the scope where it has been found (locals, globals or builtin). Return type: tuple(str, list(NodeNG))
-
-
class
astroid.node_classes.
NodeNG
(lineno=None, col_offset=None, parent=None)[source]¶ Bases:
object
A node of the new Abstract Syntax Tree (AST).
This is the base class for all Astroid node classes.
-
as_string
()[source]¶ Get the source code that this node represents.
Returns: The source code. Return type: str
-
block_range
(lineno)[source]¶ Get a range from the given line number to where this node ends.
Parameters: lineno (int) – The line number to start the range at. Returns: The range of line numbers that this node belongs to, starting at the given line number. Return type: tuple(int, int or None)
-
bool_value
(context=None)[source]¶ Determine the boolean value of this node.
The boolean value of a node can have three possible values:
- False: For instance, empty data structures, False, empty strings, instances which return explicitly False from the __nonzero__ / __bool__ method.
- True: Most of constructs are True by default: classes, functions, modules etc
- Uninferable: The inference engine is uncertain of the node’s value.
Returns: The boolean value of this node. Return type: bool or Uninferable
-
callable
()[source]¶ Whether this node defines something that is callable.
Returns: True if this defines something that is callable, False otherwise. Return type: bool
-
child_sequence
(child)[source]¶ Search for the sequence that contains this child.
Parameters: child (NodeNG) – The child node to search sequences for. Returns: The sequence containing the given child node. Return type: iterable(NodeNG) Raises: AstroidError – If no sequence could be found that contains the given child.
-
col_offset
= None¶ The column that this node appears on in the source code.
Type: int or None
-
frame
()[source]¶ The first parent frame node.
A frame node is a
Module
,FunctionDef
, orClassDef
.Returns: The first parent frame node. Return type: Module or FunctionDef or ClassDef
-
fromlineno
¶ <wrapped by the cachedproperty decorator> The first line that this node appears on in the source code.
type: int or None
-
get_children
()[source]¶ Get the child nodes below this node.
Returns: The children. Return type: iterable(NodeNG)
-
has_base
(node)[source]¶ Check if this node inherits from the given type.
Parameters: node (NodeNG) – The node defining the base to look for. Usually this is a Name
node.
-
infer
(context=None, **kwargs)[source]¶ Get a generator of the inferred values.
This is the main entry point to the inference system.
See also
If the instance has some explicit inference function set, it will be called instead of the default interface.
Returns: The inferred values. Return type: iterable
-
inferred
()[source]¶ Get a list of the inferred values.
See also
Returns: The inferred values. Return type: list
-
instantiate_class
()[source]¶ Instantiate an instance of the defined class.
Note
On anything other than a
ClassDef
this will return self.Returns: An instance of the defined class. Return type: object
-
is_function
= False¶ Whether this node indicates a function.
Type: bool
-
is_lambda
= False¶
-
is_statement
= False¶ Whether this node indicates a statement.
Type: bool
-
last_child
()[source]¶ An optimized version of list(get_children())[-1]
Returns: The last child, or None if no children exist. Return type: NodeNG or None
-
lineno
= None¶ The line that this node appears on in the source code.
Type: int or None
-
locate_child
(child)[source]¶ Find the field of this node that contains the given child.
Parameters: child (NodeNG) – The child node to search fields for. Returns: A tuple of the name of the field that contains the child, and the sequence or node that contains the child node. Return type: tuple(str, iterable(NodeNG) or NodeNG) Raises: AstroidError – If no field could be found that contains the given child.
-
next_sibling
()[source]¶ The next sibling statement node.
Returns: The next sibling statement node. Return type: NodeNG or None
-
nodes_of_class
(klass, skip_klass=None)[source]¶ Get the nodes (including this one or below) of the given types.
Parameters: - klass (builtins.type or tuple(builtins.type)) – The types of node to search for.
- skip_klass (builtins.type or tuple(builtins.type)) – The types of node to ignore. This is useful to ignore
subclasses of
klass
.
Returns: The node of the given types.
Return type: iterable(NodeNG)
-
optional_assign
= False¶ Whether this node optionally assigns a variable.
This is for loop assignments because loop won’t necessarily perform an assignment if the loop has no iterations. This is also the case from comprehensions in Python 2.
Type: bool
-
parent_of
(node)[source]¶ Check if this node is the parent of the given node.
Parameters: node (NodeNG) – The node to check if it is the child. Returns: True if this node is the parent of the given node, False otherwise. Return type: bool
-
previous_sibling
()[source]¶ The previous sibling statement.
Returns: The previous sibling statement node. Return type: NodeNG or None
-
repr_tree
(ids=False, include_linenos=False, ast_state=False, indent=' ', max_depth=0, max_width=80) → str[source]¶ Get a string representation of the AST from this node.
Parameters: - ids (bool) – If true, includes the ids with the node type names.
- include_linenos (bool) – If true, includes the line numbers and column offsets.
- ast_state (bool) – If true, includes information derived from the whole AST like local and global variables.
- indent (str) – A string to use to indent the output string.
- max_depth (int) – If set to a positive integer, won’t return nodes deeper than max_depth in the string.
- max_width (int) – Attempt to format the output string to stay within this number of characters, but can exceed it under some circumstances. Only positive integer values are valid, the default is 80.
Returns: The string representation of the AST.
Return type: str
-
scope
()[source]¶ The first parent node defining a new scope.
Returns: The first parent scope node. Return type: Module or FunctionDef or ClassDef or Lambda or GenExpr
-
set_local
(name, stmt)[source]¶ Define that the given name is declared in the given statement node.
This definition is stored on the parent scope node.
See also
Parameters: - name (str) – The name that is being defined.
- stmt (NodeNG) – The statement that defines the given name.
-
statement
()[source]¶ The first parent node, including self, marked as statement node.
Returns: The first parent statement. Return type: NodeNG
-
tolineno
¶ <wrapped by the cachedproperty decorator> The last line that this node appears on in the source code.
type: int or None
-
-
class
astroid.node_classes.
Statement
(lineno=None, col_offset=None, parent=None)[source]¶ Bases:
astroid.node_classes.NodeNG
Statement node adding a few attributes
-
is_statement
= True¶ Whether this node indicates a statement.
Type: bool
-