Base Nodes

These are abstract node classes that other nodes inherit from.

astroid.mixins.AssignTypeMixin()

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

Base class for Set, FrozenSet, Tuple and List.

astroid.mixins.BlockRangeMixIn()

override block range

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

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.nodes.ListComp([lineno, col_offset, ...])

Class representing an ast.ListComp node.

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.mixins.ParentAssignTypeMixin()

astroid.nodes.Statement([lineno, ...])

Statement node adding a few attributes

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

Base class for all Pattern nodes.

class astroid.mixins.AssignTypeMixin[source]

Bases: object

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

Bases: astroid.mixins.ParentAssignTypeMixin, astroid.nodes.node_ng.NodeNG, astroid.bases.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.

Return type

None

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.

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

abstract pytype()[source]

Get the name of the type that this node represents.

Returns

The name of the type.

Return type

str

elts: list[NodeNG]

The elements in the node.

class astroid.mixins.BlockRangeMixIn[source]

Bases: object

override block range

blockstart_tolineno

<wrapped by the cachedproperty decorator>

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

Bases: astroid.nodes.scoped_nodes.mixin.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.

Return type

None

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.mixins.FilterStmtsMixin[source]

Bases: object

Mixin for statement filtering and assignment type

assign_type()[source]
class astroid.mixins.ImportFromMixin[source]

Bases: astroid.mixins.FilterStmtsMixin

MixIn for From and Import Nodes

do_import_module(modname=None)[source]

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

real_name(asname)[source]

get name from ‘as’ name

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

Bases: astroid.nodes.scoped_nodes.mixin.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 always Uninferable.

Return type

Uninferable

get_children()[source]

Get the child nodes below this node.

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.

elt = None

The element that forms the output of the expression.

Type

NodeNG or None

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.

parent: NodeNG | None

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.

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

Bases: astroid.nodes.node_classes.LookupMixIn, astroid.nodes.node_ng.NodeNG

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.

Return type

None

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 (astroid.nodes.scoped_nodes.mixin._T) –

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 (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[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(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[str, 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.

Return type

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

eq(value)[source]
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.

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

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: 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.

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

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

root()[source]

Return the root node of the syntax tree.

Returns

The root node.

Return type

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.

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.

parent: NodeNG | None

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.

tolineno

<wrapped by the cachedproperty decorator> The last line that this node appears on in the source code.

class astroid.mixins.ParentAssignTypeMixin[source]

Bases: astroid.mixins.AssignTypeMixin

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

Bases: astroid.nodes.node_ng.NodeNG

Statement node adding a few attributes

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.

Return type

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

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.

parent: NodeNG | None

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.

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

Bases: astroid.nodes.node_ng.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.

Return type

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.

lineno: int | None

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

parent: NodeNG | None

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.