class documentation

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

This is the base class for all Astroid node classes.

Method __init__ No summary
Method __repr__ Undocumented
Method __str__ Undocumented
Method accept Visit this node using the given visitor.
Method as_string Get the source code that this node represents.
Method block_range Get a range from the given line number to where this node ends.
Method bool_value Determine the boolean value of this node.
Method callable Whether this node defines something that is callable.
Method child_sequence Search for the sequence that contains this child.
Method eq Undocumented
Method frame The first parent frame node.
Method get_children Get the child nodes below this node.
Method has_base Check if this node inherits from the given type.
Method infer Get a generator of the inferred values.
Method inferred Get a list of the inferred values.
Method instantiate_class Instantiate an instance of the defined class.
Method last_child An optimized version of list(get_children())[-1]
Method locate_child Find the field of this node that contains the given child.
Method next_sibling The next sibling statement node.
Method node_ancestors Yield parent, grandparent, etc until there are no more.
Method nodes_of_class Get the nodes (including this one or below) of the given types.
Method op_left_associative Undocumented
Method op_precedence Undocumented
Method parent_of Check if this node is the parent of the given node.
Method previous_sibling The previous sibling statement.
Method repr_tree Get a string representation of the AST from this node.
Method root Return the root node of the syntax tree.
Method scope The first parent node defining a new scope. These can be Module, FunctionDef, ClassDef, Lambda, or GeneratorExp nodes.
Method set_local Define that the given name is declared in the given statement node.
Method statement The first parent node, including self, marked as statement node.
Class Variable is_function Whether this node indicates a function.
Class Variable is_lambda Undocumented
Class Variable is_statement Whether this node indicates a statement.
Class Variable optional_assign Whether this node optionally assigns a variable.
Instance Variable col_offset The column that this node appears on in the source code.
Instance Variable end_col_offset The end column this node appears on in the source code. Note: This is after the last symbol.
Instance Variable end_lineno The last line this node appears on in the source code.
Instance Variable lineno The line that this node appears on in the source code.
Instance Variable parent The parent node in the syntax tree.
Property fromlineno The first line that this node appears on in the source code.
Property tolineno The last line that this node appears on in the source code.
Method _fixed_source_line Attempt to find the line that this node appears on.
Method _get_assign_nodes Undocumented
Method _get_name_nodes Undocumented
Method _get_return_nodes_skip_functions Undocumented
Method _get_yield_nodes_skip_lambdas Undocumented
Method _infer we don't know how to resolve a statement by default
Method _infer_name Undocumented
Method _repr_name Get a name for nice representation.
Class Variable _astroid_fields Node attributes that contain child nodes.
Class Variable _explicit_inference Undocumented
Class Variable _other_fields Node attributes that do not contain child nodes.
Class Variable _other_other_fields Attributes that contain AST-dependent fields.
def __init__(self, lineno: int | None = None, col_offset: int | None = None, parent: NodeNG | None = None, *, end_lineno: int | None = None, end_col_offset: int | None = None): (source)
Parameters
lineno:int | NoneThe line that this node appears on in the source code.
col_offset:int | NoneThe column that this node appears on in the source code.
parent:NodeNG | NoneThe parent node in the syntax tree.
end_lineno:int | NoneThe last line this node appears on in the source code.
end_col_offset:int | NoneThe end column this node appears on in the source code. Note: This is after the last symbol.
def __repr__(self): (source)

Undocumented

def __str__(self): (source)

Undocumented

def accept(self, visitor): (source)

Visit this node using the given visitor.

def as_string(self) -> str: (source)

Get the source code that this node represents.

def block_range(self, lineno): (source)

Get a range from the given line number to where this node ends.

Parameters
lineno:intThe line number to start the range at.
Returns
tuple(int, int or None)The range of line numbers that this node belongs to, starting at the given line number.
def bool_value(self, 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
bool or UninferableThe boolean value of this node.
def callable(self): (source)

Whether this node defines something that is callable.

Returns
boolTrue if this defines something that is callable, False otherwise.
def child_sequence(self, child): (source)

Search for the sequence that contains this child.

Parameters
child:NodeNGThe child node to search sequences for.
Returns
iterable(NodeNG)The sequence containing the given child node.
Raises
AstroidErrorIf no sequence could be found that contains the given child.
def eq(self, value): (source)

Undocumented

def frame(self, *, future: Literal[None, True] = None) -> nodes.FunctionDef | nodes.Module | nodes.ClassDef | nodes.Lambda: (source)

The first parent frame node.

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

Returns
nodes.FunctionDef | nodes.Module | nodes.ClassDef | nodes.LambdaThe first parent frame node.
def has_base(self, node): (source)

Check if this node inherits from the given type.

Parameters
node:NodeNGThe node defining the base to look for. Usually this is a Name node.
def infer(self, context=None, **kwargs): (source)

Get a generator of the inferred values.

This is the main entry point to the inference system.

See Also

:ref:`inference`

If the instance has some explicit inference function set, it will be called instead of the default interface.

Returns
iterableThe inferred values.
def inferred(self): (source)

Get a list of the inferred values.

See Also

:ref:`inference`

Returns
listThe inferred values.
def instantiate_class(self): (source)

Instantiate an instance of the defined class.

Note

On anything other than a ClassDef this will return self.

Returns
objectAn instance of the defined class.
def last_child(self) -> NodeNG | None: (source)

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

def locate_child(self, child): (source)

Find the field of this node that contains the given child.

Parameters
child:NodeNGThe child node to search fields for.
Returns
tuple(str, iterable(NodeNG) or NodeNG)A tuple of the name of the field that contains the child, and the sequence or node that contains the child node.
Raises
AstroidErrorIf no field could be found that contains the given child.
def next_sibling(self): (source)

The next sibling statement node.

Returns
NodeNG or NoneThe next sibling statement node.
def node_ancestors(self) -> Iterator[NodeNG]: (source)

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

@overload
def nodes_of_class(self, klass: type[T_Nodes], skip_klass: SkipKlassT = None) -> Iterator[T_Nodes]:
@overload
def nodes_of_class(self, klass: tuple[type[T_Nodes], type[T_Nodes2]], skip_klass: SkipKlassT = None) -> Iterator[T_Nodes] | Iterator[T_Nodes2]:
@overload
def nodes_of_class(self, klass: tuple[type[T_Nodes], type[T_Nodes2], type[T_Nodes3]], skip_klass: SkipKlassT = None) -> Iterator[T_Nodes] | Iterator[T_Nodes2] | Iterator[T_Nodes3]:
@overload
def nodes_of_class(self, klass: tuple[type[T_Nodes], ...], skip_klass: SkipKlassT = None) -> Iterator[T_Nodes]:
(source)

Get the nodes (including this one or below) of the given types.

Parameters
klass:type[T_Nodes] | tuple[type[T_Nodes], type[T_Nodes2]] | tuple[type[T_Nodes], type[T_Nodes2], type[T_Nodes3]] | tuple[type[T_Nodes], ...]The types of node to search for.
skip_klass:SkipKlassTThe types of node to ignore. This is useful to ignore subclasses of klass.
Returns
Iterator[T_Nodes] | Iterator[T_Nodes2] | Iterator[T_Nodes3]The node of the given types.
def op_left_associative(self): (source)

Undocumented

def op_precedence(self): (source)
def parent_of(self, node): (source)

Check if this node is the parent of the given node.

Parameters
node:NodeNGThe node to check if it is the child.
Returns
boolTrue if this node is the parent of the given node, False otherwise.
def previous_sibling(self): (source)

The previous sibling statement.

Returns
NodeNG or NoneThe previous sibling statement node.
def repr_tree(self, 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:boolIf true, includes the ids with the node type names.
include_linenos:boolIf true, includes the line numbers and column offsets.
ast_state:boolIf true, includes information derived from the whole AST like local and global variables.
indent:strA string to use to indent the output string.
max_depth:intIf set to a positive integer, won't return nodes deeper than max_depth in the string.
max_width:intAttempt 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
strThe string representation of the AST.
def root(self): (source)

Return the root node of the syntax tree.

Returns
ModuleThe root node.
def scope(self) -> nodes.LocalsDictNodeNG: (source)

The first parent node defining a new scope. These can be Module, FunctionDef, ClassDef, Lambda, or GeneratorExp nodes.

Returns
nodes.LocalsDictNodeNGThe first parent scope node.
def set_local(self, 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:strThe name that is being defined.
stmt:NodeNGThe statement that defines the given name.
@overload
def statement(self, *, future: Literal[None] = ...) -> nodes.Statement | nodes.Module:
@overload
def statement(self, *, future: Literal[True]) -> nodes.Statement:
(source)

The first parent node, including self, marked as statement node.

TODO: Deprecate the future parameter and only raise StatementMissing and return nodes.Statement

Raises
AttributeErrorIf self has no parent attribute
StatementMissingIf self has no parent attribute and future is True
is_function: ClassVar[bool] = (source)

Whether this node indicates a function.

is_lambda: ClassVar[bool] = (source)

Undocumented

is_statement: ClassVar[bool] = (source)

Whether this node indicates a statement.

optional_assign: ClassVar[bool] = (source)

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.

col_offset: int | None = (source)

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

end_col_offset: int | None = (source)

The end column this node appears on in the source code. Note: This is after the last symbol.

end_lineno: int | None = (source)

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

lineno: int | None = (source)

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

parent: NodeNG | None = (source)

The parent node in the syntax tree.

@decorators.cachedproperty
fromlineno: int | None = (source)

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

@decorators.cachedproperty
tolineno: int | None = (source)

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

def _fixed_source_line(self) -> int | None: (source)

Attempt to find the line that this node appears on.

We need this method since not all nodes have lineno set.

@decorators.cached
def _get_assign_nodes(self): (source)
def _get_name_nodes(self): (source)
overridden in astroid.nodes.Name

Undocumented

def _get_return_nodes_skip_functions(self): (source)
overridden in astroid.nodes.Return

Undocumented

def _infer(self, context=None): (source)

we don't know how to resolve a statement by default

def _repr_name(self): (source)

Get a name for nice representation.

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

Returns
strThe nice name.
_explicit_inference = (source)

Undocumented

_other_other_fields: ClassVar[tuple[str, ...]] = (source)