Nodes¶
For a list of available nodes see Nodes.
Nodes¶
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Variation of |
|
Variation of |
|
Class representing an |
|
Class representing an |
|
Asynchronous |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing any constant including num, str, bool, None, bytes. |
|
Class representing an |
|
A node representing a list of decorators. |
|
Variation of |
|
Variation of |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Represents the unpacking of dicts into dicts using PEP 448. |
|
Holds an arbitrary object in the |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Represents a list of string expressions to be joined. |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing a |
|
Class representing a |
|
Class representing a |
|
Class representing a |
|
Class representing a |
|
Class representing a |
|
Class representing a |
|
Class representing a |
|
Class representing a |
|
Class representing a |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
This node represents a node in a constructed AST where introspection is not possible. |
|
Class representing an |
|
Class representing an |
|
Class representing an |
|
Class representing an |
- class astroid.nodes.AnnAssign(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
AssignTypeNode
,Statement
Class representing an
ast.AnnAssign
node.An
AnnAssign
is an assignment with a type annotation.>>> import astroid >>> node = astroid.extract_node('variable: List[int] = range(10)') >>> node <AnnAssign l.1 at 0x7effe1d4c630>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(node=None, context=None, assign_path=None)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
self (nodes.AnnAssign) –
node (node_classes.AssignedStmtsPossibleNode) –
context (InferenceContext | None) –
assign_path (list[int] | None) –
- Return type:
Any
- class astroid.nodes.Arguments(vararg, kwarg, parent)[source]¶
Bases:
AssignTypeNode
Class representing an
ast.arguments
node.An
Arguments
node represents that arguments in a function definition.>>> import astroid >>> node = astroid.extract_node('def foo(bar): pass') >>> node <FunctionDef.foo l.1 at 0x7effe1db8198> >>> node.args <Arguments l.1 at 0x7effe1db82e8>
Almost all attributes can be None for living objects where introspection failed.
- Parameters:
vararg (str | None) –
kwarg (str | None) –
parent (NodeNG) –
- assigned_stmts(node=None, context=None, assign_path=None)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
self (nodes.Arguments) –
node (node_classes.AssignedStmtsPossibleNode) –
context (InferenceContext | None) –
assign_path (list[int] | None) –
- Return type:
Any
- default_value(argname)[source]¶
Get the default value for an argument.
- Parameters:
argname (str) – The name of the argument to get the default value for.
- Raises:
NoDefault – If there is no default value defined for the given argument.
- find_argname(argname, rec=False)[source]¶
Get the index and
AssignName
node for given name.- Parameters:
argname (str) – The name of the argument to search for.
rec (bool) – Whether or not to include arguments in unpacked tuples in the search.
- Returns:
The index and node for the argument.
- Return type:
tuple(str or None, AssignName or None)
- format_args(*, skippable_names=None)[source]¶
Get the arguments formatted as string.
- Returns:
The formatted arguments.
- Return type:
str
- Parameters:
skippable_names (set[str] | None) –
- is_argument(name)[source]¶
Check if the given name is defined in the arguments.
- Parameters:
name (str) – The name to check for.
- Returns:
Whether the given name is defined in the arguments,
- Return type:
bool
- postinit(args, defaults, kwonlyargs, kw_defaults, annotations, posonlyargs, kwonlyargs_annotations, posonlyargs_annotations, varargannotation=None, kwargannotation=None, type_comment_args=None, type_comment_kwonlyargs=None, type_comment_posonlyargs=None)[source]¶
- Parameters:
args (list[astroid.nodes.node_classes.AssignName] | None) –
defaults (list[astroid.nodes.node_ng.NodeNG] | None) –
kwonlyargs (list[astroid.nodes.node_classes.AssignName]) –
kw_defaults (list[astroid.nodes.node_ng.NodeNG | None] | None) –
annotations (list[astroid.nodes.node_ng.NodeNG | None]) –
posonlyargs (list[astroid.nodes.node_classes.AssignName]) –
kwonlyargs_annotations (list[astroid.nodes.node_ng.NodeNG | None]) –
posonlyargs_annotations (list[astroid.nodes.node_ng.NodeNG | None]) –
varargannotation (NodeNG | None) –
kwargannotation (NodeNG | None) –
type_comment_args (list[astroid.nodes.node_ng.NodeNG | None] | None) –
type_comment_kwonlyargs (list[astroid.nodes.node_ng.NodeNG | None] | None) –
type_comment_posonlyargs (list[astroid.nodes.node_ng.NodeNG | None] | None) –
- Return type:
None
- annotations: list[NodeNG | None]¶
The type annotations of arguments that can be passed positionally.
- args: list[AssignName] | None¶
The names of the required arguments.
Can be None if the associated function does not have a retrievable signature and the arguments are therefore unknown. This can happen with (builtin) functions implemented in C that have incomplete signature information.
- property arguments¶
Get all the arguments for this node, including positional only and positional and keyword
- 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.
- kw_defaults: list[NodeNG | None] | None¶
The default values for keyword arguments that cannot be passed positionally.
- kwarg¶
The name of the variable length keyword arguments.
- kwonlyargs: list[AssignName]¶
The keyword arguments that cannot be passed positionally.
- kwonlyargs_annotations: list[NodeNG | None]¶
The type annotations of arguments that cannot be passed positionally.
- posonlyargs: list[AssignName]¶
The arguments that can only be passed positionally.
- posonlyargs_annotations: list[NodeNG | None]¶
The type annotations of arguments that can only be passed positionally.
- type_comment_args: list[NodeNG | None]¶
The type annotation, passed by a type comment, of each argument.
If an argument does not have a type comment, the value for that argument will be None.
- type_comment_kwonlyargs: list[NodeNG | None]¶
The type annotation, passed by a type comment, of each keyword only argument.
If an argument does not have a type comment, the value for that argument will be None.
- type_comment_posonlyargs: list[NodeNG | None]¶
The type annotation, passed by a type comment, of each positional argument.
If an argument does not have a type comment, the value for that argument will be None.
- vararg¶
The name of the variable length arguments.
- class astroid.nodes.Assert(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
Statement
Class representing an
ast.Assert
node.An
Assert
node represents an assert statement.>>> import astroid >>> node = astroid.extract_node('assert len(things) == 10, "Not enough things"') >>> node <Assert l.1 at 0x7effe1d527b8>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.Assign(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
AssignTypeNode
,Statement
Class representing an
ast.Assign
node.An
Assign
is a statement where something is explicitly asssigned to.>>> import astroid >>> node = astroid.extract_node('variable = range(10)') >>> node <Assign l.1 at 0x7effe1db8550>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- postinit(targets, value, type_annotation)[source]¶
- Parameters:
targets (list[astroid.nodes.node_ng.NodeNG]) –
value (NodeNG) –
type_annotation (NodeNG | None) –
- Return type:
None
- class astroid.nodes.AssignAttr(attrname, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
ParentAssignNode
Variation of
ast.Assign
representing assignment to an attribute.>>> import astroid >>> node = astroid.extract_node('self.attribute = range(10)') >>> node <Assign l.1 at 0x7effe1d521d0> >>> list(node.get_children()) [<AssignAttr.attribute l.1 at 0x7effe1d52320>, <Call l.1 at 0x7effe1d522e8>] >>> list(node.get_children())[0].as_string() 'self.attribute'
- Parameters:
attrname (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(node=None, context=None, assign_path=None)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
self (nodes.AssignName | nodes.AssignAttr) –
node (node_classes.AssignedStmtsPossibleNode) –
context (InferenceContext | None) –
assign_path (list[int] | None) –
- Return type:
Any
- infer_lhs(**kwargs)¶
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- attrname¶
The name of the attribute being assigned to.
- class astroid.nodes.AssignName(name, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NoChildrenNode
,LookupMixIn
,ParentAssignNode
Variation of
ast.Assign
representing assignment to a name.An
AssignName
is the name of something that is assigned to. This includes variables defined in a function signature or in a loop.>>> import astroid >>> node = astroid.extract_node('variable = range(10)') >>> node <Assign l.1 at 0x7effe1db8550> >>> list(node.get_children()) [<AssignName.variable l.1 at 0x7effe1db8748>, <Call l.1 at 0x7effe1db8630>] >>> list(node.get_children())[0].as_string() 'variable'
- Parameters:
name (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(node=None, context=None, assign_path=None)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
self (nodes.AssignName | nodes.AssignAttr) –
node (node_classes.AssignedStmtsPossibleNode) –
context (InferenceContext | None) –
assign_path (list[int] | None) –
- Return type:
Any
- infer_lhs(context=None, **kwargs)¶
Infer a Name: use name lookup rules.
- Parameters:
self (Name | AssignName) –
context (InferenceContext | None) –
kwargs (Any) –
- Return type:
Generator[NodeNG | UninferableBase | Proxy, None, None]
- name¶
The name that is assigned to.
- class astroid.nodes.AsyncFor(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
For
Class representing an
ast.AsyncFor
node.An
AsyncFor
is an asynchronousFor
built with theasync
keyword.>>> import astroid >>> node = astroid.extract_node(''' async def func(things): async for thing in things: print(thing) ''') >>> node <AsyncFunctionDef.func l.2 at 0x7f23b2e416d8> >>> node.body[0] <AsyncFor l.3 at 0x7f23b2e417b8>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.AsyncFunctionDef(name, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
FunctionDef
Class representing an
ast.FunctionDef
node.A
AsyncFunctionDef
is an asynchronous function created with the async keyword.>>> import astroid >>> node = astroid.extract_node(''' async def func(things): async for thing in things: print(thing) ''') >>> node <AsyncFunctionDef.func l.2 at 0x7f23b2e416d8> >>> node.body[0] <AsyncFor l.3 at 0x7f23b2e417b8>
- Parameters:
name (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.AsyncWith(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
With
Asynchronous
with
built with theasync
keyword.- 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.
- class astroid.nodes.Attribute(attrname, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.Attribute
node.- Parameters:
attrname (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- attrname¶
The name of the attribute.
- class astroid.nodes.AugAssign(op, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
AssignTypeNode
,Statement
Class representing an
ast.AugAssign
node.An
AugAssign
is an assignment paired with an operator.>>> import astroid >>> node = astroid.extract_node('variable += 1') >>> node <AugAssign l.1 at 0x7effe1db4d68>
- Parameters:
op (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- type_errors(context=None)[source]¶
Get a list of type errors which can occur during inference.
Each TypeError is represented by a
BadBinaryOperationMessage
, which holds the original exception.- Returns:
The list of possible type errors.
- Return type:
list(BadBinaryOperationMessage)
- Parameters:
context (InferenceContext | None) –
- op¶
The operator that is being combined with the assignment.
This includes the equals sign.
- class astroid.nodes.Await(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.Await
node.An
Await
is theawait
keyword.>>> import astroid >>> node = astroid.extract_node(''' async def func(things): await other_func() ''') >>> node <AsyncFunctionDef.func l.2 at 0x7f23b2e41748> >>> node.body[0] <Expr l.3 at 0x7f23b2e419e8> >>> list(node.body[0].get_children())[0] <Await l.3 at 0x7f23b2e41a20>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.BinOp(op, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.BinOp
node.A
BinOp
node is an application of a binary operator.>>> import astroid >>> node = astroid.extract_node('a + b') >>> node <BinOp l.1 at 0x7f23b2e8cfd0>
- Parameters:
op (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- type_errors(context=None)[source]¶
Get a list of type errors which can occur during inference.
Each TypeError is represented by a
BadBinaryOperationMessage
, which holds the original exception.- Returns:
The list of possible type errors.
- Return type:
list(BadBinaryOperationMessage)
- Parameters:
context (InferenceContext | None) –
- op¶
The operator.
- class astroid.nodes.BoolOp(op=None, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NodeNG
Class representing an
ast.BoolOp
node.A
BoolOp
is an application of a boolean operator.>>> import astroid >>> node = astroid.extract_node('a and b') >>> node <BinOp l.1 at 0x7f23b2e71c50>
- Parameters:
op (str | None) – The operator.
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.
- postinit(values=None)[source]¶
Do some setup after initialisation.
- Parameters:
values (list[astroid.nodes.node_ng.NodeNG] | None) – The values being applied to the operator.
- Return type:
None
- op: str | None¶
The operator.
- class astroid.nodes.Break(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NoChildrenNode
,Statement
Class representing an
ast.Break
node.>>> import astroid >>> node = astroid.extract_node('break') >>> node <Break l.1 at 0x7f23b2e9e5c0>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.Call(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.Call
node.A
Call
node is a call to a function, method, etc.>>> import astroid >>> node = astroid.extract_node('function()') >>> node <Call l.1 at 0x7f23b2e71eb8>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- postinit(func, args, keywords)[source]¶
- Parameters:
func (NodeNG) –
args (list[astroid.nodes.node_ng.NodeNG]) –
keywords (list[astroid.nodes.node_classes.Keyword]) –
- Return type:
None
- property kwargs: list[astroid.nodes.node_classes.Keyword]¶
The keyword arguments that unpack something.
- property starargs: list[astroid.nodes.node_classes.Starred]¶
The positional arguments that unpack something.
- class astroid.nodes.ClassDef(name, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
FilterStmtsBaseNode
,LocalsDictNodeNG
,Statement
Class representing an
ast.ClassDef
node.>>> import astroid >>> node = astroid.extract_node(''' class Thing: def my_meth(self, arg): return arg + self.offset ''') >>> node <ClassDef.Thing l.2 at 0x7f23b2e9e748>
- Parameters:
name (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- ancestors(recurs=True, context=None)[source]¶
Iterate over the base classes in prefixed depth first order.
- Parameters:
recurs (bool) – Whether to recurse or return direct ancestors only.
context (InferenceContext | None) –
- Returns:
The base classes
- Return type:
Generator[ClassDef, None, None]
- block_range(lineno)[source]¶
Get a range from the given line number to where this node ends.
- Parameters:
lineno (int) – Unused.
- Returns:
The range of line numbers that this node belongs to,
- Return type:
tuple[int, int]
- bool_value(context=None)[source]¶
Determine the boolean value of this node.
- Returns:
The boolean value of this node. For a
ClassDef
this is alwaysTrue
.- Parameters:
context (InferenceContext | None) –
- Return type:
Literal[True]
- callable()[source]¶
Whether this node defines something that is callable.
- Returns:
Whether this defines something that is callable. For a
ClassDef
this is alwaysTrue
.- Return type:
bool
- declared_metaclass(context=None)[source]¶
Return the explicit declared metaclass for the current class.
An explicit declared metaclass is defined either by passing the
metaclass
keyword argument in the class definition line (Python 3) or (Python 2) by having a__metaclass__
class attribute, or if there are no explicit bases but there is a global__metaclass__
variable.- Returns:
The metaclass of this class, or None if one could not be found.
- Parameters:
context (InferenceContext | None) –
- Return type:
SuccessfulInferenceResult | None
- display_type()[source]¶
A human readable type of this node.
- Returns:
The type of this node.
- Return type:
str
- frame(*, future=None)[source]¶
The node’s frame node.
A frame node is a
Module
,FunctionDef
,ClassDef
orLambda
.- Returns:
The node itself.
- Parameters:
self (_T) –
future (Literal[None, True]) –
- Return type:
_T
- getattr(name, context=None, class_context=True)[source]¶
Get an attribute from this class, using Python’s attribute semantic.
This method doesn’t look in the
instance_attrs
dictionary since it is done by anInstance
proxy at inference time. It may return anUninferable
object if the attribute has not been found, but a__getattr__
or__getattribute__
method is defined. Ifclass_context
is given, then it is considered that the attribute is accessed from a class context, e.g. ClassDef.attribute, otherwise it might have been accessed from an instance as well. Ifclass_context
is used in that case, then a lookup in the implicit metaclass and the explicit metaclass will be done.- Parameters:
name (str) – The attribute to look for.
class_context (bool) – Whether the attribute can be accessed statically.
context (InferenceContext | None) –
- Returns:
The attribute.
- Raises:
AttributeInferenceError – If the attribute cannot be inferred.
- Return type:
list[SuccessfulInferenceResult]
- getitem(index, context=None)[source]¶
Return the inference of a subscript.
This is basically looking up the method in the metaclass and calling it.
- Returns:
The inferred value of a subscript to this class.
- Return type:
- Raises:
AstroidTypeError – If this class does not define a
__getitem__
method.- Parameters:
context (InferenceContext | None) –
- has_base(node)[source]¶
Whether this class directly inherits from the given node.
- Parameters:
node (NodeNG) – The node to check for.
- Returns:
Whether this class directly inherits from the given node.
- Return type:
bool
- has_dynamic_getattr(context=None)[source]¶
Check if the class has a custom __getattr__ or __getattribute__.
If any such method is found and it is not from builtins, nor from an extension module, then the function will return True.
- Returns:
Whether the class has a custom __getattr__ or __getattribute__.
- Parameters:
context (InferenceContext | None) –
- Return type:
bool
- igetattr(name, context=None, class_context=True)[source]¶
Infer the possible values of the given variable.
- Parameters:
name (str) – The name of the variable to infer.
context (InferenceContext | None) –
class_context (bool) –
- Returns:
The inferred possible values.
- Return type:
Iterator[InferenceResult]
- implicit_locals()[source]¶
Get implicitly defined class definition locals.
- Returns:
the the name and Const pair for each local
- Return type:
tuple(tuple(str, node_classes.Const), …)
- implicit_metaclass()[source]¶
Get the implicit metaclass of the current class.
For newstyle classes, this will return an instance of builtins.type. For oldstyle classes, it will simply return None, since there’s no implicit metaclass there.
- Returns:
The metaclass.
- Return type:
builtins.type or None
- infer_binary_op(**kwargs)¶
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- infer_call_result(caller, context=None)[source]¶
infer what a class is returning when called
- Parameters:
caller (SuccessfulInferenceResult | None) –
context (InferenceContext | None) –
- Return type:
Iterator[InferenceResult]
- instance_attr(name, context=None)[source]¶
Get the list of nodes associated to the given attribute name.
Assignments are looked for in both this class and in parents.
- Returns:
The list of assignments to the given name.
- Return type:
list(NodeNG)
- Raises:
AttributeInferenceError – If no attribute with this name can be found in this class or parent classes.
- Parameters:
context (InferenceContext | None) –
- instance_attr_ancestors(name, context=None)[source]¶
Iterate over the parents that define the given name as an attribute.
- Parameters:
name (str) – The name to find definitions for.
context (InferenceContext | None) –
- Returns:
The parents that define the given name as an instance attribute.
- Return type:
iterable(NodeNG)
- instantiate_class()[source]¶
Get an
Instance
of theClassDef
node.- Returns:
An
Instance
of theClassDef
node- Return type:
Instance
- is_subtype_of(type_name, context=None)[source]¶
Whether this class is a subtype of the given type.
- Parameters:
type_name (str) – The name of the type of check against.
context (InferenceContext | None) –
- Returns:
Whether this class is a subtype of the given type.
- Return type:
bool
- local_attr(name, context=None)[source]¶
Get the list of assign nodes associated to the given name.
Assignments are looked for in both this class and in parents.
- Returns:
The list of assignments to the given name.
- Return type:
list(NodeNG)
- Raises:
AttributeInferenceError – If no attribute with this name can be found in this class or parent classes.
- Parameters:
context (InferenceContext | None) –
- local_attr_ancestors(name, context=None)[source]¶
Iterate over the parents that define the given name.
- Parameters:
name (str) – The name to find definitions for.
context (InferenceContext | None) –
- Returns:
The parents that define the given name.
- Return type:
iterable(NodeNG)
- metaclass(context=None)[source]¶
Get the metaclass of this class.
If this class does not define explicitly a metaclass, then the first defined metaclass in ancestors will be used instead.
- Returns:
The metaclass of this class.
- Parameters:
context (InferenceContext | None) –
- Return type:
SuccessfulInferenceResult | None
- methods()[source]¶
Iterate over all of the method defined in this class and its parents.
- Returns:
The methods defined on the class.
- Return type:
iterable(FunctionDef)
- mro(context=None)[source]¶
Get the method resolution order, using C3 linearization.
- Returns:
The list of ancestors, sorted by the mro.
- Return type:
list(NodeNG)
- Raises:
DuplicateBasesError – Duplicate bases in the same class base
InconsistentMroError – A class’ MRO is inconsistent
- Parameters:
context (InferenceContext | None) –
- mymethods()[source]¶
Iterate over all of the method defined in this class only.
- Returns:
The methods defined on the class.
- Return type:
iterable(FunctionDef)
- postinit(bases, body, decorators, newstyle=None, metaclass=None, keywords=None, *, position=None, doc_node=None)[source]¶
- Parameters:
bases (list[SuccessfulInferenceResult]) –
body (list[NodeNG]) –
decorators (node_classes.Decorators | None) –
newstyle (bool | None) –
metaclass (NodeNG | None) –
keywords (list[node_classes.Keyword] | None) –
position (Position | None) –
doc_node (Const | None) –
- Return type:
None
- pytype()[source]¶
Get the name of the type that this node represents.
- Returns:
The name of the type.
- Return type:
Literal[‘builtins.type’, ‘builtins.classobj’]
- scope_lookup(node, name, offset=0)[source]¶
Lookup where the given name 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 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]]
- property basenames¶
The names of the parent classes
Names are given in the order they appear in the class definition.
- Type:
list(str)
- bases: list[SuccessfulInferenceResult]¶
What the class inherits from.
- property blockstart_tolineno¶
The line on which the beginning of this block ends.
- Type:
int
- decorators = None¶
The decorators that are applied to this class.
- 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.
- hide = False¶
- is_dataclass: bool¶
Whether this class is a dataclass.
- keywords: list[node_classes.Keyword]¶
The keywords given to the class definition.
This is usually for PEP 3115 style metaclass declaration.
- name¶
The name of the class.
- property newstyle¶
Whether this is a new style class or not
- Type:
bool or None
- special_attributes¶
The names of special attributes that this class has.
- Type:
objectmodel.ClassModel
- property type¶
The class type for this node.
Possible values are: class, metaclass, exception.
- Type:
str
- class astroid.nodes.Compare(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.Compare
node.A
Compare
node indicates a comparison.>>> import astroid >>> node = astroid.extract_node('a <= b <= c') >>> node <Compare l.1 at 0x7f23b2e9e6d8> >>> node.ops [('<=', <Name.b l.1 at 0x7f23b2e9e2b0>), ('<=', <Name.c l.1 at 0x7f23b2e9e390>)]
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- get_children()[source]¶
Get the child nodes below this node.
Overridden to handle the tuple fields and skip returning the operator strings.
- Returns:
The children.
- Return type:
iterable(NodeNG)
- last_child()[source]¶
An optimized version of list(get_children())[-1]
- Returns:
The last child.
- Return type:
- postinit(left, ops)[source]¶
- Parameters:
left (NodeNG) –
ops (list[tuple[str, astroid.nodes.node_ng.NodeNG]]) –
- Return type:
None
- class astroid.nodes.Comprehension(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.comprehension
node.A
Comprehension
indicates the loop inside any type of comprehension including generator expressions.>>> import astroid >>> node = astroid.extract_node('[x for x in some_values]') >>> list(node.get_children()) [<Name.x l.1 at 0x7f23b2e352b0>, <Comprehension l.1 at 0x7f23b2e35320>] >>> list(node.get_children())[1].as_string() 'for x in some_values'
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assign_type()[source]¶
The type of assignment that this node performs.
- Returns:
The assignment type.
- Return type:
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- postinit(target, iter, ifs, is_async)[source]¶
- Parameters:
target (NodeNG) –
iter (NodeNG) –
ifs (list[astroid.nodes.node_ng.NodeNG]) –
is_async (bool) –
- Return type:
None
- is_async: bool¶
Whether this is an asynchronous comprehension or not.
- optional_assign: ClassVar[bool] = True¶
Whether this node optionally assigns a variable.
- class astroid.nodes.Const(value, lineno=None, col_offset=None, parent=None, kind=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NoChildrenNode
,Instance
Class representing any constant including num, str, bool, None, bytes.
>>> import astroid >>> node = astroid.extract_node('(5, "This is a string.", True, None, b"bytes")') >>> node <Tuple.tuple l.1 at 0x7f23b2e358d0> >>> list(node.get_children()) [<Const.int l.1 at 0x7f23b2e35940>, <Const.str l.1 at 0x7f23b2e35978>, <Const.bool l.1 at 0x7f23b2e359b0>, <Const.NoneType l.1 at 0x7f23b2e359e8>, <Const.bytes l.1 at 0x7f23b2e35a20>]
- Parameters:
value (Any) – The value that the constant represents.
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.
kind (str | None) – The string prefix. “u” for u-prefixed strings and
None
otherwise. Python 3.8+ only.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.
- bool_value(context=None)[source]¶
Determine the boolean value of this node.
- Returns:
The boolean value of this node.
- Return type:
bool
- Parameters:
context (InferenceContext | None) –
- getitem(index, context=None)[source]¶
Get an item from this node if subscriptable.
- Parameters:
- Raises:
AstroidTypeError – When the given index cannot be used as a subscript index, or if this node is not subscriptable.
- has_dynamic_getattr()[source]¶
Check if the node has a custom __getattr__ or __getattribute__.
- Returns:
Whether the class has a custom __getattr__ or __getattribute__. For a
Const
this is alwaysFalse
.- Return type:
bool
- infer_binary_op(**kwargs)¶
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- infer_unary_op(op)¶
- itered()[source]¶
An iterator over the elements this node contains.
- Returns:
The contents of this node.
- Return type:
iterable(Const)
- Raises:
TypeError – If this node does not represent something that is iterable.
- pytype()[source]¶
Get the name of the type that this node represents.
- Returns:
The name of the type.
- Return type:
str
- kind: str | None¶
“The string prefix. “u” for u-prefixed strings and
None
otherwise. Python 3.8+ only.
- value: Any¶
The value that the constant represents.
- class astroid.nodes.Continue(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NoChildrenNode
,Statement
Class representing an
ast.Continue
node.>>> import astroid >>> node = astroid.extract_node('continue') >>> node <Continue l.1 at 0x7f23b2e35588>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.Decorators(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
A node representing a list of decorators.
A
Decorators
is the decorators that are applied to a method or function.>>> import astroid >>> node = astroid.extract_node(''' @property def my_property(self): return 3 ''') >>> node <FunctionDef.my_property l.2 at 0x7f23b2e35d30> >>> list(node.get_children())[0] <Decorators l.1 at 0x7f23b2e35d68>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- postinit(nodes)[source]¶
- Parameters:
nodes (list[astroid.nodes.node_ng.NodeNG]) –
- Return type:
None
- class astroid.nodes.DelAttr(attrname, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
ParentAssignNode
Variation of
ast.Delete
representing deletion of an attribute.>>> import astroid >>> node = astroid.extract_node('del self.attr') >>> node <Delete l.1 at 0x7f23b2e35f60> >>> list(node.get_children())[0] <DelAttr.attr l.1 at 0x7f23b2e411d0>
- Parameters:
attrname (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- attrname¶
The name of the attribute that is being deleted.
- class astroid.nodes.DelName(name, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NoChildrenNode
,LookupMixIn
,ParentAssignNode
Variation of
ast.Delete
representing deletion of a name.A
DelName
is the name of something that is deleted.>>> import astroid >>> node = astroid.extract_node("del variable #@") >>> list(node.get_children()) [<DelName.variable l.1 at 0x7effe1da4d30>] >>> list(node.get_children())[0].as_string() 'variable'
- Parameters:
name (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- name¶
The name that is being deleted.
- class astroid.nodes.Delete(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
AssignTypeNode
,Statement
Class representing an
ast.Delete
node.A
Delete
is adel
statement this is deleting something.>>> import astroid >>> node = astroid.extract_node('del self.attr') >>> node <Delete l.1 at 0x7f23b2e35f60>
- Parameters:
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- postinit(targets)[source]¶
- Parameters:
targets (list[astroid.nodes.node_ng.NodeNG]) –
- Return type:
None
- class astroid.nodes.Dict(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
,DictInstance
Class representing an
ast.Dict
node.A
Dict
is a dictionary that is created with{}
syntax.>>> import astroid >>> node = astroid.extract_node('{1: "1"}') >>> node <Dict.dict l.1 at 0x7f23b2e35cc0>
- 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.
- Return type:
bool
- Parameters:
context (InferenceContext | None) –
- get_children()[source]¶
Get the key and value nodes below this node.
Children are returned in the order that they are defined in the source code, key first then the value.
- Returns:
The children.
- Return type:
iterable(NodeNG)
- getitem(index, context=None)[source]¶
Get an item from this node.
- Parameters:
- Raises:
AstroidTypeError – When the given index cannot be used as a subscript index, or if this node is not subscriptable.
AstroidIndexError – If the given index does not exist in the dictionary.
- Return type:
- infer_unary_op(op)¶
- itered()[source]¶
An iterator over the keys this node contains.
- Returns:
The keys of this node.
- Return type:
iterable(NodeNG)
- last_child()[source]¶
An optimized version of list(get_children())[-1]
- Returns:
The last child, or None if no children exist.
- Return type:
NodeNG or None
- postinit(items)[source]¶
Do some setup after initialisation.
- Parameters:
items (list[tuple[SuccessfulInferenceResult, SuccessfulInferenceResult]]) – The key-value pairs contained in the dictionary.
- Return type:
None
- pytype()[source]¶
Get the name of the type that this node represents.
- Returns:
The name of the type.
- Return type:
Literal[‘builtins.dict’]
- items: list[tuple[InferenceResult, InferenceResult]]¶
The key-value pairs contained in the dictionary.
- class astroid.nodes.DictComp(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
ComprehensionScope
Class representing an
ast.DictComp
node.>>> import astroid >>> node = astroid.extract_node('{k:v for k, v in things if k > v}') >>> node <DictComp l.1 at 0x7f23b2e41d68>
- Parameters:
lineno (int) –
col_offset (int) –
parent (NodeNG) –
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. For a
DictComp
this is alwaysUninferable
.- Return type:
Uninferable
- Parameters:
context (InferenceContext | None) –
- postinit(key, value, generators)[source]¶
- Parameters:
key (NodeNG) –
value (NodeNG) –
generators (list[nodes.Comprehension]) –
- Return type:
None
- class astroid.nodes.DictUnpack(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NoChildrenNode
Represents the unpacking of dicts into dicts using PEP 448.
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.EmptyNode(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NoChildrenNode
Holds an arbitrary object in the
LocalsDictNodeNG.locals
.- Parameters:
lineno (None) –
col_offset (None) –
parent (None) –
end_lineno (None) –
end_col_offset (None) –
- object = None¶
- class astroid.nodes.ExceptHandler(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
MultiLineBlockNode
,AssignTypeNode
,Statement
Class representing an
ast.ExceptHandler
. node.An
ExceptHandler
is anexcept
block on a try-except.>>> import astroid >>> node = astroid.extract_node(''' try: do_something() except Exception as error: print("Error!") ''') >>> node <TryExcept l.2 at 0x7f23b2e9d908> >>> node.handlers [<ExceptHandler l.4 at 0x7f23b2e9e860>]
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- catch(exceptions)[source]¶
Check if this node handles any of the given
- Parameters:
exceptions (list[str] | None) – The names of the exceptions to check for.
- Return type:
bool
- postinit(type, name, body)[source]¶
- Parameters:
type (NodeNG | None) –
name (AssignName | None) –
body (list[astroid.nodes.node_ng.NodeNG]) –
- Return type:
None
- property blockstart_tolineno¶
The line on which the beginning of this block ends.
- Type:
int
- name: AssignName | None¶
The name that the caught exception is assigned to.
- class astroid.nodes.Expr(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
Statement
Class representing an
ast.Expr
node.An
Expr
is any expression that does not have its value used or stored.>>> import astroid >>> node = astroid.extract_node('method()') >>> node <Call l.1 at 0x7f23b2e352b0> >>> node.parent <Expr l.1 at 0x7f23b2e35278>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.For(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
MultiLineWithElseBlockNode
,AssignTypeNode
,Statement
Class representing an
ast.For
node.>>> import astroid >>> node = astroid.extract_node('for thing in things: print(thing)') >>> node <For l.1 at 0x7f23b2e8cf28>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- postinit(target, iter, body, orelse, type_annotation)[source]¶
- Parameters:
target (NodeNG) –
iter (NodeNG) –
body (list[astroid.nodes.node_ng.NodeNG]) –
orelse (list[astroid.nodes.node_ng.NodeNG]) –
type_annotation (NodeNG | None) –
- Return type:
None
- property blockstart_tolineno¶
The line on which the beginning of this block ends.
- Type:
int
- class astroid.nodes.FormattedValue(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NodeNG
Class representing an
ast.FormattedValue
node.Represents a PEP 498 format string.
>>> import astroid >>> node = astroid.extract_node('f"Format {type_}"') >>> node <JoinedStr l.1 at 0x7f23b2e4ed30> >>> node.values [<Const.str l.1 at 0x7f23b2e4eda0>, <FormattedValue l.1 at 0x7f23b2e4edd8>]
- 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.
- conversion: int¶
The type of formatting to be applied to the value.
See also
- class astroid.nodes.FunctionDef(name, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
MultiLineBlockNode
,FilterStmtsBaseNode
,Statement
,LocalsDictNodeNG
Class representing an
ast.FunctionDef
.>>> import astroid >>> node = astroid.extract_node(''' ... def my_func(arg): ... return arg + 1 ... ''') >>> node <FunctionDef.my_func l.2 at 0x7f23b2e71e10>
- Parameters:
name (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- argnames()[source]¶
Get the names of each of the arguments, including that of the collections of variable-length arguments (“args”, “kwargs”, etc.), as well as positional-only and keyword-only arguments.
- Returns:
The names of the arguments.
- Return type:
list(str)
- block_range(lineno)[source]¶
Get a range from the given line number to where this node ends.
- Parameters:
lineno (int) – Unused.
- Returns:
The range of line numbers that this node belongs to,
- Return type:
tuple[int, int]
- bool_value(context=None)[source]¶
Determine the boolean value of this node.
- Returns:
The boolean value of this node. For a
FunctionDef
this is alwaysTrue
.- Parameters:
context (InferenceContext | None) –
- Return type:
bool
- callable()[source]¶
Whether this node defines something that is callable.
- Returns:
Whether this defines something that is callable.
- Return type:
Literal[True]
- decoratornames(context=None)[source]¶
Get the qualified names of each of the decorators on this function.
- Parameters:
context (InferenceContext | None) – An inference context that can be passed to inference functions
- Returns:
The names of the decorators.
- Return type:
set[str]
- display_type()[source]¶
A human readable type of this node.
- Returns:
The type of this node.
- Return type:
str
- frame(*, future=None)[source]¶
The node’s frame node.
A frame node is a
Module
,FunctionDef
,ClassDef
orLambda
.- Returns:
The node itself.
- Parameters:
self (_T) –
future (Literal[None, True]) –
- Return type:
_T
- getattr(name, context=None)[source]¶
- Parameters:
name (str) –
context (InferenceContext | None) –
- Return type:
- igetattr(name, context=None)[source]¶
Inferred getattr, which returns an iterator of inferred statements.
- Parameters:
name (str) –
context (InferenceContext | None) –
- Return type:
Iterator[InferenceResult]
- infer_call_result(caller, context=None)[source]¶
Infer what the function returns when called.
- Parameters:
caller (SuccessfulInferenceResult | None) –
context (InferenceContext | None) –
- Return type:
Iterator[InferenceResult]
- infer_yield_result(context=None)[source]¶
Infer what the function yields when called
- Returns:
What the function yields
- Return type:
iterable(NodeNG or Uninferable) or None
- Parameters:
context (InferenceContext | None) –
- is_abstract(pass_is_abstract=True, any_raise_is_abstract=False)[source]¶
Check if the method is abstract.
A method is considered abstract if any of the following is true: * The only statement is ‘raise NotImplementedError’ * The only statement is ‘raise <SomeException>’ and any_raise_is_abstract is True * The only statement is ‘pass’ and pass_is_abstract is True * The method is annotated with abc.astractproperty/abc.abstractmethod
- Returns:
Whether the method is abstract.
- Return type:
bool
- is_bound()[source]¶
Check if the function is bound to an instance or class.
- Returns:
Whether the function is bound to an instance or class.
- Return type:
bool
- is_generator()[source]¶
Check if this is a generator function.
- Returns:
Whether this is a generator function.
- Return type:
bool
- is_method()[source]¶
Check if this function node represents a method.
- Returns:
Whether this is a method.
- Return type:
bool
- postinit(args, body, decorators=None, returns=None, type_comment_returns=None, type_comment_args=None, *, position=None, doc_node=None)[source]¶
Do some setup after initialisation.
- Parameters:
args (Arguments) – The arguments that the function takes.
body (list[astroid.nodes.node_ng.NodeNG]) – The contents of the function body.
decorators (Decorators | None) – The decorators that are applied to this method or function.
doc_node (Const | None) – The doc node associated with this node.
position (Position | None) –
- Params type_comment_returns:
The return type annotation passed via a type comment.
- Params type_comment_args:
The args type annotation passed via a type comment.
- Params position:
Position of function keyword(s) and name.
- pytype()[source]¶
Get the name of the type that this node represents.
- Returns:
The name of the type.
- Return type:
Literal[‘builtins.instancemethod’, ‘builtins.function’]
- scope_lookup(node, name, offset=0)[source]¶
Lookup where the given name is assigned.
- Parameters:
node (node_classes.LookupMixIn) –
name (str) –
offset (int) –
- Return type:
tuple[LocalsDictNodeNG, list[nodes.NodeNG]]
- property blockstart_tolineno¶
The line on which the beginning of this block ends.
- Type:
int
- decorators: node_classes.Decorators | None¶
The decorators that are applied to this method or function.
- property extra_decorators: list[astroid.nodes.node_classes.Call]¶
The extra decorators that this function can have.
Additional decorators are considered when they are used as assignments, as in
method = staticmethod(method)
. The property will return all the callables that are used for decoration.
- 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] = True¶
Whether this node indicates a function.
For a
FunctionDef
this is alwaysTrue
.- Type:
bool
- is_lambda: ClassVar[bool] = True¶
- name = '<functiondef>'¶
The name of the function.
- returns = None¶
- special_attributes¶
The names of special attributes that this function has.
- property type: str¶
The function type for this node.
Possible values are: method, function, staticmethod, classmethod.
- type_annotation = None¶
If present, this will contain the type annotation passed by a type comment
- Type:
NodeNG or None
- type_comment_args = None¶
If present, this will contain the type annotation for arguments passed by a type comment
- type_comment_returns = None¶
If present, this will contain the return type annotation, passed by a type comment
- class astroid.nodes.GeneratorExp(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
ComprehensionScope
Class representing an
ast.GeneratorExp
node.>>> import astroid >>> node = astroid.extract_node('(thing for thing in things if thing)') >>> node <GeneratorExp l.1 at 0x7f23b2e4e400>
- Parameters:
lineno (int) –
col_offset (int) –
parent (NodeNG) –
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. For a
GeneratorExp
this is alwaysTrue
.- Parameters:
context (InferenceContext | None) –
- Return type:
Literal[True]
- postinit(elt, generators)[source]¶
- Parameters:
elt (NodeNG) –
generators (list[nodes.Comprehension]) –
- Return type:
None
- generators: list[nodes.Comprehension]¶
The generators that are looped through.
- class astroid.nodes.Global(names, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NoChildrenNode
,Statement
Class representing an
ast.Global
node.>>> import astroid >>> node = astroid.extract_node('global a_global') >>> node <Global l.1 at 0x7f23b2e9de10>
- Parameters:
names (list[str]) – The names being declared as global.
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.
- names: list[str]¶
The names being declared as global.
- class astroid.nodes.If(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
MultiLineWithElseBlockNode
,Statement
Class representing an
ast.If
node.>>> import astroid >>> node = astroid.extract_node('if condition: print(True)') >>> node <If l.1 at 0x7f23b2e9dd30>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- 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]
- postinit(test, body, orelse)[source]¶
- Parameters:
test (NodeNG) –
body (list[astroid.nodes.node_ng.NodeNG]) –
orelse (list[astroid.nodes.node_ng.NodeNG]) –
- Return type:
None
- property blockstart_tolineno¶
The line on which the beginning of this block ends.
- Type:
int
- class astroid.nodes.IfExp(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.IfExp
node. >>> import astroid >>> node = astroid.extract_node(‘value if condition else other’) >>> node <IfExp l.1 at 0x7f23b2e9dbe0>- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.Import(names=None, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
ImportNode
Class representing an
ast.Import
node. >>> import astroid >>> node = astroid.extract_node(‘import astroid’) >>> node <Import l.1 at 0x7f23b2e4e5c0>- Parameters:
names (list[tuple[str, str | None]]) – The names being imported.
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.
- names: list[tuple[str, str | None]]¶
The names being imported.
Each entry is a
tuple
of the name being imported, and the alias that the name is assigned to (if any).
- class astroid.nodes.ImportFrom(fromname, names, level=0, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
ImportNode
Class representing an
ast.ImportFrom
node.>>> import astroid >>> node = astroid.extract_node('from my_package import my_module') >>> node <ImportFrom l.1 at 0x7f23b2e415c0>
- Parameters:
fromname (str | None) – The module that is being imported from.
names (list[tuple[str, str | None]]) – What is being imported from the module.
level (int | None) – The level of relative import.
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.
- level: int | None¶
The level of relative import.
Essentially this is the number of dots in the import. This is always 0 for absolute imports.
- 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.JoinedStr(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NodeNG
Represents a list of string expressions to be joined.
>>> import astroid >>> node = astroid.extract_node('f"Format {type_}"') >>> node <JoinedStr l.1 at 0x7f23b2e4ed30>
- 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.
- postinit(values=None)[source]¶
Do some setup after initialisation.
- Parameters:
value – The string expressions to be joined.
values (list[astroid.nodes.node_ng.NodeNG] | None) –
- Type:
list(FormattedValue or Const)
- Return type:
None
- values: list[NodeNG]¶
The string expressions to be joined.
- Type:
list(FormattedValue or Const)
- class astroid.nodes.Keyword(arg, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.keyword
node.>>> import astroid >>> node = astroid.extract_node('function(a_kwarg=True)') >>> node <Call l.1 at 0x7f23b2e9e320> >>> node.keywords [<Keyword l.1 at 0x7f23b2e9e9b0>]
- Parameters:
arg (str | None) –
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- arg¶
The argument being assigned to.
- class astroid.nodes.Lambda(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
FilterStmtsBaseNode
,LocalsDictNodeNG
Class representing an
ast.Lambda
node.>>> import astroid >>> node = astroid.extract_node('lambda arg: arg + 1') >>> node <Lambda.<lambda> l.1 at 0x7f23b2e41518>
- Parameters:
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- argnames()[source]¶
Get the names of each of the arguments, including that of the collections of variable-length arguments (“args”, “kwargs”, etc.), as well as positional-only and keyword-only arguments.
- Returns:
The names of the arguments.
- Return type:
list(str)
- bool_value(context=None)[source]¶
Determine the boolean value of this node.
- Returns:
The boolean value of this node. For a
Lambda
this is alwaysTrue
.- Parameters:
context (InferenceContext | None) –
- Return type:
Literal[True]
- callable()[source]¶
Whether this node defines something that is callable.
- Returns:
Whether this defines something that is callable For a
Lambda
this is alwaysTrue
.- Return type:
Literal[True]
- display_type()[source]¶
A human readable type of this node.
- Returns:
The type of this node.
- Return type:
str
- frame(*, future=None)[source]¶
The node’s frame node.
A frame node is a
Module
,FunctionDef
,ClassDef
orLambda
.- Returns:
The node itself.
- Parameters:
self (_T) –
future (Literal[None, True]) –
- Return type:
_T
- getattr(name, context=None)[source]¶
- Parameters:
name (str) –
context (InferenceContext | None) –
- Return type:
- infer_call_result(caller, context=None)[source]¶
Infer what the function returns when called.
- Parameters:
caller (SuccessfulInferenceResult | None) –
context (InferenceContext | None) –
- Return type:
Iterator[InferenceResult]
- pytype()[source]¶
Get the name of the type that this node represents.
- Returns:
The name of the type.
- Return type:
Literal[‘builtins.instancemethod’, ‘builtins.function’]
- scope_lookup(node, name, offset=0)[source]¶
Lookup where the given names is assigned.
- Parameters:
node (LookupMixIn) – The node to look for assignments up to. Any assignments after the given node are ignored.
name (str) – The name 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[astroid.nodes.scoped_nodes.mixin.LocalsDictNodeNG, list[astroid.nodes.node_ng.NodeNG]]
- is_lambda: ClassVar[bool] = True¶
- name = '<lambda>'¶
- special_attributes¶
The names of special attributes that this function has.
- property type: Literal['method', 'function']¶
Whether this is a method or function.
- Returns:
‘method’ if this is a method, ‘function’ otherwise.
- class astroid.nodes.List(ctx=None, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
BaseContainer
Class representing an
ast.List
node.>>> import astroid >>> node = astroid.extract_node('[1, 2, 3]') >>> node <List.list l.1 at 0x7f23b2e9e128>
- Parameters:
ctx (Context | None) – Whether the list is assigned to or loaded from.
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.
- assigned_stmts(node=None, context=None, assign_path=None)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
self (nodes.Tuple | nodes.List) –
node (node_classes.AssignedStmtsPossibleNode) –
context (InferenceContext | None) –
assign_path (list[int] | None) –
- Return type:
Any
- infer_binary_op(**kwargs)¶
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- infer_unary_op(op)¶
- pytype()[source]¶
Get the name of the type that this node represents.
- Returns:
The name of the type.
- Return type:
Literal[‘builtins.list’]
- ctx: Context | None¶
Whether the list is assigned to or loaded from.
- class astroid.nodes.ListComp(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
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 (int) –
col_offset (int) –
parent (NodeNG) –
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. For a
ListComp
this is alwaysUninferable
.- Return type:
Uninferable
- Parameters:
context (InferenceContext | None) –
- postinit(elt, generators)[source]¶
- Parameters:
elt (NodeNG) –
generators (list[nodes.Comprehension]) –
- generators: list[nodes.Comprehension]¶
The generators that are looped through.
- class astroid.nodes.Match(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
Statement
,MultiLineBlockNode
Class representing a
ast.Match
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case 200: ... case _: ... ''') >>> node <Match l.2 at 0x10c24e170>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- postinit(*, subject, cases)[source]¶
- Parameters:
subject (NodeNG) –
cases (list[astroid.nodes.node_classes.MatchCase]) –
- Return type:
None
- class astroid.nodes.MatchAs(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
AssignTypeNode
,Pattern
Class representing a
ast.MatchAs
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case [1, a]: ... case {'key': b}: ... case Point2D(0, 0) as c: ... case d: ... ''') >>> node.cases[0].pattern.patterns[1] <MatchAs l.3 at 0x10d0b2da0> >>> node.cases[1].pattern.patterns[0] <MatchAs l.5 at 0x10d0b2920> >>> node.cases[2].pattern <MatchAs l.7 at 0x10d0b06a0> >>> node.cases[3].pattern <MatchAs l.9 at 0x10d09b880>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- postinit(*, pattern, name)[source]¶
- Parameters:
pattern (Pattern | None) –
name (AssignName | None) –
- Return type:
None
- class astroid.nodes.MatchCase(*, parent=None)[source]¶
Bases:
MultiLineBlockNode
Class representing a
ast.match_case
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case 200: ... ''') >>> node.cases[0] <MatchCase l.3 at 0x10c24e590>
- Parameters:
parent (NodeNG | None) –
- postinit(*, pattern, guard, body)[source]¶
- Parameters:
pattern (Pattern) –
guard (NodeNG | None) –
body (list[astroid.nodes.node_ng.NodeNG]) –
- Return type:
None
- col_offset: None¶
The column that this node appears on in the source code.
- end_col_offset: None¶
The end column this node appears on in the source code.
Note: This is after the last symbol.
- end_lineno: None¶
The last line this node appears on in the source code.
- lineno: None¶
The line that this node appears on in the source code.
- class astroid.nodes.MatchClass(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
Pattern
Class representing a
ast.MatchClass
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case Point2D(0, 0): ... case Point3D(x=0, y=0, z=0): ... ''') >>> node.cases[0].pattern <MatchClass l.3 at 0x10ca83940> >>> node.cases[1].pattern <MatchClass l.5 at 0x10ca80880>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- postinit(*, cls, patterns, kwd_attrs, kwd_patterns)[source]¶
- Parameters:
cls (NodeNG) –
patterns (list[astroid.nodes.node_classes.Pattern]) –
kwd_attrs (list[str]) –
kwd_patterns (list[astroid.nodes.node_classes.Pattern]) –
- Return type:
None
- class astroid.nodes.MatchMapping(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
AssignTypeNode
,Pattern
Class representing a
ast.MatchMapping
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case {1: "Hello", 2: "World", 3: _, **rest}: ... ''') >>> node.cases[0].pattern <MatchMapping l.3 at 0x10c8a8850>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- postinit(*, keys, patterns, rest)[source]¶
- Parameters:
keys (list[astroid.nodes.node_ng.NodeNG]) –
patterns (list[astroid.nodes.node_classes.Pattern]) –
rest (AssignName | None) –
- Return type:
None
- class astroid.nodes.MatchOr(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
Pattern
Class representing a
ast.MatchOr
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case 400 | 401 | 402: ... ''') >>> node.cases[0].pattern <MatchOr l.3 at 0x10d0b0b50>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- postinit(*, patterns)[source]¶
- Parameters:
patterns (list[astroid.nodes.node_classes.Pattern]) –
- Return type:
None
- class astroid.nodes.MatchSequence(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
Pattern
Class representing a
ast.MatchSequence
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case [1, 2]: ... case (1, 2, *_): ... ''') >>> node.cases[0].pattern <MatchSequence l.3 at 0x10ca80d00> >>> node.cases[1].pattern <MatchSequence l.5 at 0x10ca80b20>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- postinit(*, patterns)[source]¶
- Parameters:
patterns (list[astroid.nodes.node_classes.Pattern]) –
- Return type:
None
- class astroid.nodes.MatchSingleton(*, value, lineno=None, col_offset=None, end_lineno=None, end_col_offset=None, parent=None)[source]¶
Bases:
Pattern
Class representing a
ast.MatchSingleton
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case True: ... case False: ... case None: ... ''') >>> node.cases[0].pattern <MatchSingleton l.3 at 0x10c2282e0> >>> node.cases[1].pattern <MatchSingleton l.5 at 0x10c228af0> >>> node.cases[2].pattern <MatchSingleton l.7 at 0x10c229f90>
- Parameters:
value (Literal[True, False, None]) –
lineno (int | None) –
col_offset (int | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
parent (NodeNG | None) –
- class astroid.nodes.MatchStar(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
AssignTypeNode
,Pattern
Class representing a
ast.MatchStar
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case [1, *_]: ... ''') >>> node.cases[0].pattern.patterns[1] <MatchStar l.3 at 0x10ca809a0>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- postinit(*, name)[source]¶
- Parameters:
name (AssignName | None) –
- Return type:
None
- class astroid.nodes.MatchValue(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
Pattern
Class representing a
ast.MatchValue
node.>>> import astroid >>> node = astroid.extract_node(''' match x: case 200: ... ''') >>> node.cases[0].pattern <MatchValue l.3 at 0x10c24e200>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.Module(name, file=None, path=None, package=False, pure_python=True)[source]¶
Bases:
LocalsDictNodeNG
Class representing an
ast.Module
node.>>> import astroid >>> node = astroid.extract_node('import astroid') >>> node <Import l.1 at 0x7f23b2e4e5c0> >>> node.parent <Module l.0 at 0x7f23b2e4eda0>
- Parameters:
name (str) –
file (str | None) –
path (Sequence[str] | None) –
package (bool) –
pure_python (bool) –
- absolute_import_activated()[source]¶
Whether PEP 328 absolute import behaviour has been enabled.
- Returns:
Whether PEP 328 has been enabled.
- Return type:
bool
- block_range(lineno)[source]¶
Get a range from where this node starts to where this node ends.
- Parameters:
lineno (int) – Unused.
- Returns:
The range of line numbers that this node belongs to.
- Return type:
tuple[int, int]
- bool_value(context=None)[source]¶
Determine the boolean value of this node.
- Returns:
The boolean value of this node. For a
Module
this is alwaysTrue
.- Parameters:
context (InferenceContext | None) –
- Return type:
bool
- display_type()[source]¶
A human readable type of this node.
- Returns:
The type of this node.
- Return type:
str
- frame(*, future=None)[source]¶
The node’s frame node.
A frame node is a
Module
,FunctionDef
,ClassDef
orLambda
.- Returns:
The node itself.
- Parameters:
self (_T) –
future (Literal[None, True]) –
- Return type:
_T
- fully_defined()[source]¶
Check if this module has been build from a .py file.
If so, the module contains a complete representation, including the code.
- Returns:
Whether the module has been built from a .py file.
- Return type:
bool
- getattr(name, context=None, ignore_locals=False)[source]¶
- Parameters:
context (InferenceContext | None) –
- igetattr(name, context=None)[source]¶
Infer the possible values of the given variable.
- Parameters:
name (str) – The name of the variable to infer.
context (InferenceContext | None) –
- Returns:
The inferred possible values.
- Return type:
Iterator[InferenceResult]
- import_module(modname, relative_only=False, level=None, use_cache=True)[source]¶
Get the ast for a given module as if imported from this module.
- Parameters:
modname (str) – The name of the module to “import”.
relative_only (bool) – Whether to only consider relative imports.
level (int | None) – The level of relative import.
use_cache (bool) – Whether to use the astroid_cache of modules.
- Returns:
The imported module ast.
- Return type:
- next_sibling()[source]¶
The next sibling statement node.
- Returns:
The next sibling statement node.
- Return type:
NodeNG or None
- postinit(body, *, doc_node=None)[source]¶
- Parameters:
body (list[astroid.nodes.node_ng.NodeNG]) –
doc_node (Const | None) –
- previous_sibling()[source]¶
The previous sibling statement.
- Returns:
The previous sibling statement node.
- Return type:
NodeNG or None
- public_names()[source]¶
The list of the names that are publicly available in this module.
- Returns:
The list of public names.
- Return type:
list(str)
- pytype()[source]¶
Get the name of the type that this node represents.
- Returns:
The name of the type.
- Return type:
Literal[‘builtins.module’]
- relative_to_absolute_name(modname, level)[source]¶
Get the absolute module name for a relative import.
The relative import can be implicit or explicit.
- Parameters:
modname (str) – The module name to convert.
level (int | None) – The level of relative import.
- Returns:
The absolute module name.
- Raises:
TooManyLevelsError – When the relative import refers to a module too far above this one.
- Return type:
str
- scope_lookup(node, name, offset=0)[source]¶
Lookup where the given variable is assigned.
- Parameters:
node (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[astroid.nodes.scoped_nodes.mixin.LocalsDictNodeNG, list[astroid.nodes.node_ng.NodeNG]]
- statement(*, future: None = None) Module [source]¶
- statement(*, future: Literal[True]) NoReturn
The first parent node, including self, marked as statement node.
When called on a
Module
with the future parameter this raises an error.TODO: Deprecate the future parameter and only raise StatementMissing
- Raises:
StatementMissing – If no self has no parent attribute and future is True
- wildcard_import_names()[source]¶
The list of imported names when this module is ‘wildcard imported’.
It doesn’t include the ‘__builtins__’ name which is added by the current CPython implementation of wildcard imports.
- Returns:
The list of imported names.
- Return type:
list(str)
- body: list[node_classes.NodeNG]¶
The contents of the module.
- file¶
The path to the file that this ast has been extracted from.
This will be
None
when the representation has been built from a built-in module.
- file_bytes: str | bytes | None = None¶
The string/bytes that this ast was built from.
- file_encoding: str | None = None¶
The encoding of the source file.
This is used to get unicode out of a source file. Python 2 only.
- future_imports: set[str]¶
The imports from
__future__
.
- globals: dict[str, list[SuccessfulInferenceResult]]¶
A map of the name of a global variable to the node defining the global.
- name¶
The name of the module.
- package¶
Whether the node represents a package or a module.
- pure_python¶
Whether the ast was built from source.
- scope_attrs = {'__doc__', '__file__', '__name__', '__package__', '__path__'}¶
The names of module attributes available through the global scope.
- special_attributes¶
The names of special attributes that this module has.
- class astroid.nodes.Name(name, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NoChildrenNode
,LookupMixIn
Class representing an
ast.Name
node.A
Name
node is something that is named, but not covered byAssignName
orDelName
.>>> import astroid >>> node = astroid.extract_node('range(10)') >>> node <Call l.1 at 0x7effe1db8710> >>> list(node.get_children()) [<Name.range l.1 at 0x7effe1db86a0>, <Const.int l.1 at 0x7effe1db8518>] >>> list(node.get_children())[0].as_string() 'range'
- Parameters:
name (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- name¶
The name that this node refers to.
- class astroid.nodes.Nonlocal(names, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
NoChildrenNode
,Statement
Class representing an
ast.Nonlocal
node.>>> import astroid >>> node = astroid.extract_node(''' def function(): nonlocal var ''') >>> node <FunctionDef.function l.2 at 0x7f23b2e9e208> >>> node.body[0] <Nonlocal l.3 at 0x7f23b2e9e908>
- Parameters:
names (list[str]) – The names being declared as not local.
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.
- names: list[str]¶
The names being declared as not local.
- class astroid.nodes.Pass(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NoChildrenNode
,Statement
Class representing an
ast.Pass
node.>>> import astroid >>> node = astroid.extract_node('pass') >>> node <Pass l.1 at 0x7f23b2e9e748>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.Raise(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
Statement
Class representing an
ast.Raise
node.>>> import astroid >>> node = astroid.extract_node('raise RuntimeError("Something bad happened!")') >>> node <Raise l.1 at 0x7f23b2e9e828>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.Return(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
Statement
Class representing an
ast.Return
node.>>> import astroid >>> node = astroid.extract_node('return True') >>> node <Return l.1 at 0x7f23b8211908>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.Set(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
BaseContainer
Class representing an
ast.Set
node.>>> import astroid >>> node = astroid.extract_node('{1, 2, 3}') >>> node <Set.set l.1 at 0x7f23b2e71d68>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- infer_unary_op(op)¶
- class astroid.nodes.SetComp(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
ComprehensionScope
Class representing an
ast.SetComp
node.>>> import astroid >>> node = astroid.extract_node('{thing for thing in things if thing}') >>> node <SetComp l.1 at 0x7f23b2e41898>
- Parameters:
lineno (int) –
col_offset (int) –
parent (NodeNG) –
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. For a
SetComp
this is alwaysUninferable
.- Return type:
Uninferable
- Parameters:
context (InferenceContext | None) –
- postinit(elt, generators)[source]¶
- Parameters:
elt (NodeNG) –
generators (list[nodes.Comprehension]) –
- Return type:
None
- generators: list[nodes.Comprehension]¶
The generators that are looped through.
- class astroid.nodes.Slice(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.Slice
node.>>> import astroid >>> node = astroid.extract_node('things[1:3]') >>> node <Subscript l.1 at 0x7f23b2e71f60> >>> node.slice <Slice l.1 at 0x7f23b2e71e80>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- igetattr(attrname, context=None)[source]¶
Infer the possible values of the given attribute on the slice.
- Parameters:
attrname (str) – The name of the attribute to infer.
context (InferenceContext | None) –
- Returns:
The inferred possible values.
- Return type:
Iterator[SuccessfulInferenceResult]
- class astroid.nodes.Starred(ctx, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
ParentAssignNode
Class representing an
ast.Starred
node.>>> import astroid >>> node = astroid.extract_node('*args') >>> node <Starred l.1 at 0x7f23b2e41978>
- Parameters:
ctx (Context) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- ctx¶
Whether the starred item is assigned to or loaded from.
- class astroid.nodes.Subscript(ctx, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.Subscript
node.>>> import astroid >>> node = astroid.extract_node('things[1:3]') >>> node <Subscript l.1 at 0x7f23b2e71f60>
- Parameters:
ctx (Context) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- infer_lhs(**kwargs)¶
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- ctx¶
Whether the subscripted item is assigned to or loaded from.
- class astroid.nodes.TryExcept(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
MultiLineWithElseBlockNode
,Statement
Class representing an
ast.TryExcept
node.>>> import astroid >>> node = astroid.extract_node(''' try: do_something() except Exception as error: print("Error!") ''') >>> node <TryExcept l.2 at 0x7f23b2e9d908>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- 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]
- postinit(body, handlers, orelse)[source]¶
- Parameters:
body (list[astroid.nodes.node_ng.NodeNG]) –
handlers (list[astroid.nodes.node_classes.ExceptHandler]) –
orelse (list[astroid.nodes.node_ng.NodeNG]) –
- Return type:
None
- handlers: list[ExceptHandler]¶
The exception handlers.
- class astroid.nodes.TryFinally(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
MultiLineWithElseBlockNode
,Statement
Class representing an
ast.TryFinally
node.>>> import astroid >>> node = astroid.extract_node(''' try: do_something() except Exception as error: print("Error!") finally: print("Cleanup!") ''') >>> node <TryFinally l.2 at 0x7f23b2e41d68>
- 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.
- 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]
- postinit(body=None, finalbody=None)[source]¶
Do some setup after initialisation.
- Parameters:
body (list[astroid.nodes.node_ng.NodeNG | astroid.nodes.node_classes.TryExcept] | None) – The try-except that the finally is attached to.
finalbody (list[astroid.nodes.node_ng.NodeNG] | None) – The contents of the
finally
block.
- Return type:
None
- class astroid.nodes.TryStar(*, lineno=None, col_offset=None, end_lineno=None, end_col_offset=None, parent=None)[source]¶
Bases:
MultiLineWithElseBlockNode
,Statement
Class representing an
ast.TryStar
node.- 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.
- block_range(lineno)[source]¶
Get a range from a given line number to where this node ends.
- Parameters:
lineno (int) –
- Return type:
tuple[int, int]
- postinit(*, body=None, handlers=None, orelse=None, finalbody=None)[source]¶
Do some setup after initialisation. :param body: The contents of the block to catch exceptions from. :param handlers: The exception handlers. :param orelse: The contents of the
else
block. :param finalbody: The contents of thefinally
block.- Parameters:
body (list[astroid.nodes.node_ng.NodeNG] | None) –
handlers (list[astroid.nodes.node_classes.ExceptHandler] | None) –
orelse (list[astroid.nodes.node_ng.NodeNG] | None) –
finalbody (list[astroid.nodes.node_ng.NodeNG] | None) –
- Return type:
None
- handlers: list[ExceptHandler]¶
The exception handlers.
- class astroid.nodes.Tuple(ctx=None, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
BaseContainer
Class representing an
ast.Tuple
node.>>> import astroid >>> node = astroid.extract_node('(1, 2, 3)') >>> node <Tuple.tuple l.1 at 0x7f23b2e41780>
- Parameters:
ctx (Context | None) – Whether the tuple is assigned to or loaded from.
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.
- assigned_stmts(node=None, context=None, assign_path=None)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
self (nodes.Tuple | nodes.List) –
node (node_classes.AssignedStmtsPossibleNode) –
context (InferenceContext | None) –
assign_path (list[int] | None) –
- Return type:
Any
- infer_binary_op(**kwargs)¶
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- infer_unary_op(op)¶
- pytype()[source]¶
Get the name of the type that this node represents.
- Returns:
The name of the type.
- Return type:
Literal[‘builtins.tuple’]
- ctx: Context | None¶
Whether the tuple is assigned to or loaded from.
- class astroid.nodes.UnaryOp(op, lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.UnaryOp
node.>>> import astroid >>> node = astroid.extract_node('-5') >>> node <UnaryOp l.1 at 0x7f23b2e4e198>
- Parameters:
op (str) –
lineno (int) –
col_offset (int) –
parent (NodeNG) –
end_lineno (int | None) –
end_col_offset (int | None) –
- type_errors(context=None)[source]¶
Get a list of type errors which can occur during inference.
Each TypeError is represented by a
BadBinaryOperationMessage
, which holds the original exception.- Returns:
The list of possible type errors.
- Return type:
list(BadBinaryOperationMessage)
- Parameters:
context (InferenceContext | None) –
- op¶
The operator.
- class astroid.nodes.Unknown(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
AssignTypeNode
This node represents a node in a constructed AST where introspection is not possible. At the moment, it’s only used in the args attribute of FunctionDef nodes where function signature introspection failed.
- Parameters:
lineno (None) –
col_offset (None) –
parent (None) –
end_lineno (None) –
end_col_offset (None) –
- name = 'Unknown'¶
- class astroid.nodes.While(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
MultiLineWithElseBlockNode
,Statement
Class representing an
ast.While
node.>>> import astroid >>> node = astroid.extract_node(''' while condition(): print("True") ''') >>> node <While l.2 at 0x7f23b2e4e390>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- 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]
- postinit(test, body, orelse)[source]¶
- Parameters:
test (NodeNG) –
body (list[astroid.nodes.node_ng.NodeNG]) –
orelse (list[astroid.nodes.node_ng.NodeNG]) –
- Return type:
None
- property blockstart_tolineno¶
The line on which the beginning of this block ends.
- Type:
int
- class astroid.nodes.With(lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None)[source]¶
Bases:
MultiLineWithElseBlockNode
,AssignTypeNode
,Statement
Class representing an
ast.With
node.>>> import astroid >>> node = astroid.extract_node(''' with open(file_path) as file_: print(file_.read()) ''') >>> node <With l.2 at 0x7f23b2e4e710>
- 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.
- assigned_stmts(**kwargs)¶
Returns the assigned statement (non inferred) according to the assignment type. See astroid/protocols.py for actual implementation.
- Parameters:
args (_P.args) –
kwargs (_P.kwargs) –
- Return type:
Generator[InferenceResult, None, None]
- get_children()[source]¶
Get the child nodes below this node.
- Returns:
The children.
- Return type:
iterable(NodeNG)
- postinit(items=None, body=None, type_annotation=None)[source]¶
Do some setup after initialisation.
- Parameters:
items (list[tuple[astroid.nodes.node_ng.NodeNG, astroid.nodes.node_ng.NodeNG | None]] | None) – The pairs of context managers and the names they are assigned to.
body (list[astroid.nodes.node_ng.NodeNG] | None) – The contents of the
with
block.type_annotation (NodeNG | None) –
- Return type:
None
- property blockstart_tolineno¶
The line on which the beginning of this block ends.
- Type:
int
- class astroid.nodes.Yield(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
NodeNG
Class representing an
ast.Yield
node.>>> import astroid >>> node = astroid.extract_node('yield True') >>> node <Yield l.1 at 0x7f23b2e4e5f8>
- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –
- class astroid.nodes.YieldFrom(lineno, col_offset, parent, *, end_lineno, end_col_offset)[source]¶
Bases:
Yield
Class representing an
ast.YieldFrom
node.- Parameters:
lineno (int | None) –
col_offset (int | None) –
parent (NodeNG | None) –
end_lineno (int | None) –
end_col_offset (int | None) –