class documentation

class NodeNG: (source)

Known subclasses: astroid.nodes.Arguments, astroid.nodes.AssignAttr, astroid.nodes.AssignName, astroid.nodes.Attribute, astroid.nodes.Await, astroid.nodes.BinOp, astroid.nodes.BoolOp, astroid.nodes.Call, astroid.nodes.Compare, astroid.nodes.Comprehension, astroid.nodes.Const, astroid.nodes.Decorators, astroid.nodes.DelAttr, astroid.nodes.DelName, astroid.nodes.Dict, astroid.nodes.DictUnpack, astroid.nodes.Ellipsis, astroid.nodes.EmptyNode, astroid.nodes.EvaluatedObject, astroid.nodes.ExtSlice, astroid.nodes.FormattedValue, astroid.nodes.IfExp, astroid.nodes.Index, astroid.nodes.JoinedStr, astroid.nodes.Keyword, astroid.nodes.MatchCase, astroid.nodes.Name, astroid.nodes.NamedExpr, astroid.nodes.node_classes.BaseContainer, astroid.nodes.node_classes.Pattern, astroid.nodes.Slice, astroid.nodes.Starred, astroid.nodes.Statement, astroid.nodes.Subscript, astroid.nodes.UnaryOp, astroid.nodes.Unknown, astroid.nodes.Yield

View In Hierarchy

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

This is the base class for all Astroid node classes.

Method __init__
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=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None): (source)
Parameters
lineno:Optional[int]The line that this node appears on in the source code.
col​_offset:Optional[int]The column that this node appears on in the source code.
parent:Optional[NodeNG]The parent node in the syntax tree.
end​_lineno:Optional[int]The last line this node appears on in the source code.
end​_col​_offset:Optional[int]The 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): (source)
Get the source code that this node represents.
Returns
strUndocumented
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=None): (source)

The first parent frame node.

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

Parameters
future:Literal[None, True]Undocumented
Returns
Union[nodes.FunctionDef, nodes.Module, nodes.ClassDef, nodes.Lambda]The 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): (source)
An optimized version of list(get_children())[-1]
Returns
Optional[NodeNG]Undocumented
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): (source)
Yield parent, grandparent, etc until there are no more.
Returns
Iterator[NodeNG]Undocumented
def nodes_of_class(self, klass, skip_klass=None): (source)
Get the nodes (including this one or below) of the given types.
Parameters
klass:Union[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
Union[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): (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): (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.
def statement(self, *, future=None): (source)

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

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

Parameters
future:Literal[None, True]Undocumented
Returns
Union[nodes.Statement, nodes.Module]Undocumented
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: Optional[int] = (source)
The column that this node appears on in the source code.
end_col_offset: Optional[int] = (source)
The end column this node appears on in the source code. Note: This is after the last symbol.
end_lineno: Optional[int] = (source)
The last line this node appears on in the source code.
lineno: Optional[int] = (source)
The line that this node appears on in the source code.
parent: Optional[NodeNG] = (source)
The parent node in the syntax tree.
@decorators.cachedproperty
fromlineno: Optional[int] = (source)
The first line that this node appears on in the source code.
@decorators.cachedproperty
tolineno: Optional[int] = (source)
The last line that this node appears on in the source code.
def _fixed_source_line(self): (source)

Attempt to find the line that this node appears on.

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

Returns
Optional[int]Undocumented
@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 _infer_name(self, frame, name): (source)
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[typing.Tuple[str, ...]] = (source)