Base Nodes¶
These are abstract node classes that other nodes inherit from.
|
Base class for Set, FrozenSet, Tuple and List. |
|
Scoping for different types of comprehensions. |
|
Class representing an |
|
this class provides locals handling common to Module, FunctionDef and ClassDef nodes, including a dict like interface for direct access to locals information |
Mixin to look up a name in the right scope. |
|
|
A node of the new Abstract Syntax Tree (AST). |
|
Statement node adding a few attributes |
|
Base class for all Pattern nodes. |
- class astroid.nodes.BaseContainer(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
ParentAssignNode
,Instance
Base class for Set, FrozenSet, Tuple and List.
- Parameters
lineno (int | None) – The line that this node appears on in the source code.
col_offset (int | None) – The column that this node appears on in the source code.
parent (NodeNG | None) – The parent node in the syntax tree.
end_lineno (int | None) – The last line this node appears on in the source code.
end_col_offset (int | None) – The end column this node appears on in the source code. Note: This is after the last symbol.
- 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.
- itered()[source]¶
An iterator over the elements this node contains.
- Returns
The contents of this node.
- Return type
iterable(NodeNG)
- postinit(elts)[source]¶
Do some setup after initialisation.
- Parameters
elts (list[NodeNG]) – The list of elements the that node contains.
- Return type
None
- class astroid.nodes.ComprehensionScope(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
LocalsDictNodeNG
Scoping for different types of comprehensions.
- Parameters
lineno (int | None) – The line that this node appears on in the source code.
col_offset (int | None) – The column that this node appears on in the source code.
parent (NodeNG | None) – The parent node in the syntax tree.
end_lineno (int | None) – The last line this node appears on in the source code.
end_col_offset (int | None) – The end column this node appears on in the source code. Note: This is after the last symbol.
- scope_lookup(node, name, offset=0)¶
XXX method for interfacing the scope lookup
- generators: list[nodes.Comprehension]¶
The generators that are looped through.
- class astroid.nodes.ListComp(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
ComprehensionScope
Class representing an
ast.ListComp
node.>>> import astroid >>> node = astroid.extract_node('[thing for thing in things if thing]') >>> node <ListComp l.1 at 0x7f23b2e418d0>
- Parameters
lineno – The line that this node appears on in the source code.
col_offset – The column that this node appears on in the source code.
parent – The parent node in the syntax tree.
end_lineno – The last line this node appears on in the source code.
end_col_offset – The end column this node appears on in the source code. Note: This is after the last symbol.
- 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
- 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.
- col_offset: int | None¶
The column that this node appears on in the source code.
- end_col_offset: int | None¶
The end column this node appears on in the source code. Note: This is after the last symbol.
- end_lineno: int | None¶
The last line this node appears on in the source code.
- generators: list[nodes.Comprehension]¶
The generators that are looped through.
- lineno: int | None¶
The line that this node appears on in the source code.
- position: Position | None¶
Position of keyword(s) and name. Used as fallback for block nodes which might not provide good enough positional information. E.g. ClassDef, FunctionDef.
- class astroid.nodes.LocalsDictNodeNG(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
LookupMixIn
this class provides locals handling common to Module, FunctionDef and ClassDef nodes, including a dict like interface for direct access to locals information
- Parameters
lineno (int | None) – The line that this node appears on in the source code.
col_offset (int | None) – The column that this node appears on in the source code.
parent (NodeNG | None) – The parent node in the syntax tree.
end_lineno (int | None) – The last line this node appears on in the source code.
end_col_offset (int | None) – The end column this node appears on in the source code. Note: This is after the last symbol.
- 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.
- 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)
- 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
- Parameters
self (_T) –
- scope_lookup(node, name, offset=0)[source]¶
Lookup where the given variable is assigned.
- Parameters
node (NodeNG) – The node to look for assignments up to. Any assignments after the given node are ignored.
name (str) – The name of the variable to find assignments for.
offset (int) – The line offset to filter statements up to.
- Returns
This 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))
- set_local(name, stmt)[source]¶
Define that the given name is declared in the given statement node.
See also
- Parameters
name (str) – The name that is being defined.
stmt (NodeNG) – The statement that defines the given name.
- values()[source]¶
The nodes that define the locals in this scoped node.
- Returns
The nodes that define locals.
- Return type
list(NodeNG)
- locals: dict[str, list[nodes.NodeNG]] = {}¶
A map of the name of a local variable to the node defining the local.
- class astroid.nodes.node_classes.LookupMixIn(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NodeNG
Mixin to look up a name in the right scope.
- Parameters
lineno (int | None) – The line that this node appears on in the source code.
col_offset (int | None) – The column that this node appears on in the source code.
parent (NodeNG | None) – The parent node in the syntax tree.
end_lineno (int | None) – The last line this node appears on in the source code.
end_col_offset (int | None) – The end column this node appears on in the source code. Note: This is after the last symbol.
- 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(name)[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[LocalsDictNodeNG, list[NodeNG]]
- class astroid.nodes.NodeNG(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
object
A node of the new Abstract Syntax Tree (AST).
This is the base class for all Astroid node classes.
- Parameters
lineno (int | None) – The line that this node appears on in the source code.
col_offset (int | None) – The column that this node appears on in the source code.
parent (NodeNG | None) – The parent node in the syntax tree.
end_lineno (int | None) – The last line this node appears on in the source code.
end_col_offset (int | None) – The end column this node appears on in the source code. Note: This is after the last symbol.
- 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.
- frame(*, future=None)[source]¶
The first parent frame node.
A frame node is a
Module
,FunctionDef
,ClassDef
orLambda
.- Returns
The first parent frame node.
- Parameters
future (Literal[(None, True)]) –
- Return type
nodes.FunctionDef | nodes.Module | nodes.ClassDef | nodes.Lambda
- 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
- Parameters
context (InferenceContext | None) –
kwargs (Any) –
- 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
- 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
- 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
- node_ancestors()[source]¶
Yield parent, grandparent, etc until there are no more.
- Return type
Iterator[NodeNG]
- nodes_of_class(klass: type[_NodesT], skip_klass: Union[None, Type[NodeNG], Tuple[Type[NodeNG], ...]] = None) Iterator[_NodesT] [source]¶
- nodes_of_class(klass: tuple[type[_NodesT], type[_NodesT2]], skip_klass: Union[None, Type[NodeNG], Tuple[Type[NodeNG], ...]] = None) Iterator[_NodesT] | Iterator[_NodesT2]
- nodes_of_class(klass: tuple[type[_NodesT], type[_NodesT2], type[_NodesT3]], skip_klass: Union[None, Type[NodeNG], Tuple[Type[NodeNG], ...]] = None) Iterator[_NodesT] | Iterator[_NodesT2] | Iterator[_NodesT3]
- nodes_of_class(klass: tuple[type[_NodesT], ...], skip_klass: Union[None, Type[NodeNG], Tuple[Type[NodeNG], ...]] = None) Iterator[_NodesT]
Get the nodes (including this one or below) of the given types.
- Parameters
klass – The types of node to search for.
skip_klass – The types of node to ignore. This is useful to ignore subclasses of
klass
.
- Returns
The node of the given types.
- 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)[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. These can be Module, FunctionDef, ClassDef, Lambda, or GeneratorExp nodes.
- Returns
The first parent scope node.
- Return type
- 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(*, future: None = None) nodes.Statement | nodes.Module [source]¶
- statement(*, future: typing_extensions.Literal[True]) nodes.Statement
The first parent node, including self, marked as statement node.
TODO: Deprecate the future parameter and only raise StatementMissing and return nodes.Statement
- Raises
AttributeError – If self has no parent attribute
StatementMissing – If self has no parent attribute and future is True
- col_offset: int | None¶
The column that this node appears on in the source code.
- end_col_offset: int | None¶
The end column this node appears on in the source code. Note: This is after the last symbol.
- end_lineno: int | None¶
The last line this node appears on in the source code.
- fromlineno¶
<wrapped by the cachedproperty decorator> The first line that this node appears on in the source code.
- is_function: ClassVar[bool] = False¶
Whether this node indicates a function.
- is_lambda: ClassVar[bool] = False¶
- is_statement: ClassVar[bool] = False¶
Whether this node indicates a statement.
- lineno: int | None¶
The line that this node appears on in the source code.
- optional_assign: ClassVar[bool] = 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.
- position: Position | None¶
Position of keyword(s) and name. Used as fallback for block nodes which might not provide good enough positional information. E.g. ClassDef, FunctionDef.
- tolineno¶
<wrapped by the cachedproperty decorator> The last line that this node appears on in the source code.
- class astroid.nodes.Statement(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NodeNG
Statement node adding a few attributes
NOTE: This class is part of the public API of ‘astroid.nodes’.
- Parameters
lineno (int | None) – The line that this node appears on in the source code.
col_offset (int | None) – The column that this node appears on in the source code.
parent (NodeNG | None) – The parent node in the syntax tree.
end_lineno (int | None) – The last line this node appears on in the source code.
end_col_offset (int | None) – The end column this node appears on in the source code. Note: This is after the last symbol.
- next_sibling()[source]¶
The next sibling statement node.
- Returns
The next sibling statement node.
- Return type
NodeNG or None
- previous_sibling()[source]¶
The previous sibling statement.
- Returns
The previous sibling statement node.
- Return type
NodeNG or None
- col_offset: int | None¶
The column that this node appears on in the source code.
- end_col_offset: int | None¶
The end column this node appears on in the source code. Note: This is after the last symbol.
- end_lineno: int | None¶
The last line this node appears on in the source code.
- is_statement: ClassVar[bool] = True¶
Whether this node indicates a statement.
- lineno: int | None¶
The line that this node appears on in the source code.
- position: Position | None¶
Position of keyword(s) and name. Used as fallback for block nodes which might not provide good enough positional information. E.g. ClassDef, FunctionDef.
- class astroid.nodes.Pattern(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NodeNG
Base class for all Pattern nodes.
- Parameters
lineno (int | None) – The line that this node appears on in the source code.
col_offset (int | None) – The column that this node appears on in the source code.
parent (NodeNG | None) – The parent node in the syntax tree.
end_lineno (int | None) – The last line this node appears on in the source code.
end_col_offset (int | None) – The end column this node appears on in the source code. Note: This is after the last symbol.
- col_offset: int | None¶
The column that this node appears on in the source code.
- end_col_offset: int | None¶
The end column this node appears on in the source code. Note: This is after the last symbol.
- end_lineno: int | None¶
The last line this node appears on in the source code.
- lineno: int | None¶
The line that this node appears on in the source code.
- position: Position | None¶
Position of keyword(s) and name. Used as fallback for block nodes which might not provide good enough positional information. E.g. ClassDef, FunctionDef.