NodeNG#

class astroid.nodes.NodeNG(lineno: int | None, col_offset: int | None, parent: NodeNG | None, *, end_lineno: int | None, end_col_offset: int | None)[source]#

Bases: object

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

This is the base class for all Astroid node classes.

accept(visitor: AsStringVisitor) str[source]#

Visit this node using the given visitor.

as_string() str[source]#

Get the source code that this node represents.

block_range(lineno: int) tuple[int, int][source]#

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

Parameters:

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

bool_value(context: InferenceContext | None = 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() bool[source]#

Whether this node defines something that is callable.

Returns:

Whether this defines something that is callable.

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#

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.

eq(value) bool[source]#
frame(*, 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:

The first parent frame node.

Raises:

ParentMissingError – If self has no parent attribute.

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.

get_children() Iterator[NodeNG][source]#

Get the child nodes below this node.

has_base(node) bool[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: InferenceContext | None = None, **kwargs: Any) Generator[InferenceResult, None, None][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

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.

last_child() NodeNG | None[source]#

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

lineno#

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

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() Iterator[NodeNG][source]#

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

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() bool[source]#
op_precedence()[source]#
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.

parent_of(node) bool[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.

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.

previous_sibling()[source]#

The previous sibling statement.

Returns:

The previous sibling statement node.

Return type:

NodeNG or None

repr_name() str[source]#

Get a name for nice representation.

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

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

root() nodes.Module[source]#

Return the root node of the syntax tree.

Returns:

The root node.

scope() nodes.LocalsDictNodeNG[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.

set_local(name: str, stmt: NodeNG) None[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 – The name that is being defined.

  • stmt – The statement that defines the given name.

statement(*, future: Literal[None, True] = None) _base_nodes.Statement[source]#

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

Raises:

StatementMissing – If self has no parent attribute.

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.