Base Nodes

These are abstract node classes that other nodes inherit from.

astroid.nodes._base_nodes.AssignTypeNode(...)

Base node for nodes that can 'assign' such as AnnAssign.

astroid.nodes.BaseContainer(lineno, ...)

Base class for Set, FrozenSet, Tuple and List.

astroid.nodes._base_nodes.MultiLineWithElseBlockNode(...)

Base node for multi-line blocks that can have else statements.

astroid.nodes.ComprehensionScope(lineno, ...)

Scoping for different types of comprehensions.

astroid.nodes._base_nodes.FilterStmtsBaseNode(...)

Base node for statement filtering and assignment type.

astroid.nodes._base_nodes.ImportNode(lineno, ...)

Base node for From and Import Nodes.

astroid.nodes.LocalsDictNodeNG(lineno, ...)

this class provides locals handling common to Module, FunctionDef and ClassDef nodes, including a dict like interface for direct access to locals information

astroid.nodes.node_classes.LookupMixIn(...)

Mixin to look up a name in the right scope.

astroid.nodes.NodeNG(lineno, col_offset, ...)

A node of the new Abstract Syntax Tree (AST).

astroid.nodes._base_nodes.ParentAssignNode(...)

Base node for nodes whose assign_type is determined by the parent node.

astroid.nodes.Statement(lineno, col_offset, ...)

Statement node adding a few attributes.

astroid.nodes.Pattern(lineno, col_offset, ...)

Base class for all Pattern nodes.

class astroid.nodes._base_nodes.AssignTypeNode(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]

Bases: NodeNG

Base node for nodes that can ‘assign’ such as AnnAssign.

Parameters:
  • lineno (int | None) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

assign_type()[source]
class astroid.nodes.BaseContainer(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]

Bases: ParentAssignNode, Instance

Base class for Set, FrozenSet, Tuple and List.

Parameters:
  • lineno (int | None) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

bool_value(context=None)[source]

Determine the boolean value of this node.

Returns:

The boolean value of this node.

Parameters:

context (InferenceContext | None) –

Return type:

bool

classmethod from_elements(elts)[source]

Create a node of this type from the given list of elements.

Parameters:

elts (Iterable[Any]) – The list of elements that the node should contain.

Returns:

A new node containing the given elements.

Return type:

Self

get_children()[source]

Get the child nodes below this node.

itered()[source]

An iterator over the elements this node contains.

Returns:

The contents of this node.

Return type:

iterable(NodeNG)

postinit(elts)[source]
Parameters:

elts (list[SuccessfulInferenceResult]) –

Return type:

None

abstract pytype()[source]

Get the name of the type that this node represents.

Returns:

The name of the type.

Return type:

str

elts: list[SuccessfulInferenceResult]

The elements in the node.

class astroid.nodes._base_nodes.MultiLineWithElseBlockNode(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]

Bases: MultiLineBlockNode

Base node for multi-line blocks that can have else statements.

Parameters:
  • lineno (int | None) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

property blockstart_tolineno
class astroid.nodes.ComprehensionScope(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]

Bases: LocalsDictNodeNG

Scoping for different types of comprehensions.

Parameters:
  • lineno (int | None) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

scope_lookup(node, name, offset=0)

XXX method for interfacing the scope lookup

Parameters:
Return type:

tuple[LocalsDictNodeNG, list[nodes.NodeNG]]

generators: list[nodes.Comprehension]

The generators that are looped through.

class astroid.nodes._base_nodes.FilterStmtsBaseNode(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]

Bases: NodeNG

Base node for statement filtering and assignment type.

Parameters:
  • lineno (int | None) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

assign_type()[source]
class astroid.nodes._base_nodes.ImportNode(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]

Bases: FilterStmtsBaseNode, NoChildrenNode, Statement

Base node for From and Import Nodes.

Parameters:
  • lineno (int | None) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

do_import_module(modname=None)[source]

Return the ast for a module whose name is <modname> imported by <self>.

Parameters:

modname (str | None) –

Return type:

nodes.Module

real_name(asname)[source]

Get name from ‘as’ name.

Parameters:

asname (str) –

Return type:

str

modname: str | None

The module that is being imported from.

This is None for relative imports.

names: list[tuple[str, str | None]]

What is being imported from the module.

Each entry is a tuple of the name being imported, and the alias that the name is assigned to (if any).

class astroid.nodes.LocalsDictNodeNG(lineno, col_offset, parent, *, end_lineno, end_col_offset)[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) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

add_local_node(child_node: nodes.ClassDef, name: str | None = None) None[source]
add_local_node(child_node: nodes.NodeNG, name: str) None

Append a child that should alter the locals of this scope node.

Parameters:
  • child_node – The child node that will alter locals.

  • name – 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 (node_classes.LookupMixIn) – 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[LocalsDictNodeNG, list[nodes.NodeNG]]

set_local(name, stmt)[source]

Define that the given name is declared in the given statement node.

See also

scope()

Parameters:
  • name (str) – The name that is being defined.

  • stmt (nodes.NodeNG) – The statement that defines the given name.

Return type:

None

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[InferenceResult]] = {}

A map of the name of a local variable to the node defining the local.

class astroid.nodes.node_classes.LookupMixIn(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]

Bases: NodeNG

Mixin to look up a name in the right scope.

Parameters:
  • lineno (int | None) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

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, col_offset, parent, *, end_lineno, end_col_offset)[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) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

accept(visitor)[source]

Visit this node using the given visitor.

as_string()[source]

Get the source code that this node represents.

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]

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

Parameters:

context (InferenceContext | None) –

callable()[source]

Whether this node defines something that is callable.

Returns:

Whether this defines something that is callable.

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.

eq(value)[source]
Return type:

bool

frame(*, future=None)[source]

The first parent frame node.

A frame node is a Module, FunctionDef, ClassDef or Lambda.

Returns:

The first parent frame node.

Parameters:

future (Literal[None, True]) –

Return type:

nodes.FunctionDef | nodes.Module | nodes.ClassDef | nodes.Lambda

get_children()[source]

Get the child nodes below this node.

Return type:

Iterator[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.

Return type:

bool

infer(context=None, **kwargs)[source]

Get a generator of the inferred values.

This is the main entry point to the inference system.

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.

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

last_child()[source]

An optimized version of list(get_children())[-1].

Return type:

NodeNG | 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

node_ancestors()[source]

Yield parent, grandparent, etc until there are no more.

Return type:

Iterator[NodeNG]

nodes_of_class(klass: type[_NodesT], skip_klass: None | Type[NodeNG] | Tuple[Type[NodeNG], ...] = None) Iterator[_NodesT][source]
nodes_of_class(klass: tuple[type[_NodesT], type[_NodesT2]], skip_klass: None | Type[NodeNG] | Tuple[Type[NodeNG], ...] = None) Iterator[_NodesT] | Iterator[_NodesT2]
nodes_of_class(klass: tuple[type[_NodesT], type[_NodesT2], type[_NodesT3]], skip_klass: None | Type[NodeNG] | Tuple[Type[NodeNG], ...] = None) Iterator[_NodesT] | Iterator[_NodesT2] | Iterator[_NodesT3]
nodes_of_class(klass: tuple[type[_NodesT], ...], skip_klass: 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.

op_left_associative()[source]
Return type:

bool

op_precedence()[source]
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:

Whether this node is the parent of the given node.

Return type:

bool

previous_sibling()[source]

The previous sibling statement.

Returns:

The previous sibling statement node.

Return type:

NodeNG or None

repr_name()[source]

Get a name for nice representation.

This is either name, attrname, or the empty string.

Return type:

str

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

root()[source]

Return the root node of the syntax tree.

Returns:

The root node.

Return type:

nodes.Module

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:

nodes.LocalsDictNodeNG

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

scope()

Parameters:
  • name (str) – The name that is being defined.

  • stmt (NodeNG) – The statement that defines the given name.

Return type:

None

statement(*, future: None = None) nodes.Statement | nodes.Module[source]
statement(*, future: 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

The column that 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.

end_lineno

The last line this node appears on in the source code.

property fromlineno: int

The first line that this node appears on in the source code.

Can also return 0 if the line can not be determined.

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

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.

parent

The parent node in the syntax tree.

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.

property tolineno: int

The last line that this node appears on in the source code.

Can also return 0 if the line can not be determined.

class astroid.nodes._base_nodes.ParentAssignNode(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]

Bases: AssignTypeNode

Base node for nodes whose assign_type is determined by the parent node.

Parameters:
  • lineno (int | None) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

assign_type()[source]
class astroid.nodes.Statement(lineno, col_offset, parent, *, end_lineno, end_col_offset)[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) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –

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

is_statement: ClassVar[bool] = True

Whether this node indicates a statement.

class astroid.nodes.Pattern(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]

Bases: NodeNG

Base class for all Pattern nodes.

Parameters:
  • lineno (int | None) –

  • col_offset (int | None) –

  • parent (NodeNG | None) –

  • end_lineno (int | None) –

  • end_col_offset (int | None) –