FunctionDef#

class astroid.nodes.FunctionDef(name: str, lineno: int, col_offset: int, parent: NodeNG, *, end_lineno: int | None, end_col_offset: int | None)[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>
argnames() list[str][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)

args: Arguments#

The arguments that the function takes.

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

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

Parameters:

lineno – Unused.

Returns:

The range of line numbers that this node belongs to,

property blockstart_tolineno#

The line on which the beginning of this block ends.

Type:

int

body: list[NodeNG]#

The contents of the function body.

bool_value(context: InferenceContext | None = None) bool[source]#

Determine the boolean value of this node.

Returns:

The boolean value of this node. For a FunctionDef this is always True.

callable() Literal[True][source]#

Whether this node defines something that is callable.

Returns:

Whether this defines something that is callable.

decoratornames(context: InferenceContext | None = None) set[str][source]#

Get the qualified names of each of the decorators on this function.

Parameters:

context – An inference context that can be passed to inference functions

Returns:

The names of the decorators.

decorators: node_classes.Decorators | None#

The decorators that are applied to this method or function.

display_type() str[source]#

A human readable type of this node.

Returns:

The type of this node.

Return type:

str

doc_node: Const | None#

The doc node associated with this node.

property extra_decorators: list[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.

frame(*, future: Literal[None, True] = None) _T[source]#

The node’s frame node.

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

Returns:

The node itself.

property fromlineno: int#

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

Can also return 0 if the line can not be determined.

get_children()[source]#

Get the child nodes below this node.

getattr(name: str, context: InferenceContext | None = None) list[NodeNG][source]#
igetattr(name: str, context: InferenceContext | None = None) Iterator[InferenceResult][source]#

Inferred getattr, which returns an iterator of inferred statements.

implicit_parameters() Literal[0, 1][source]#
infer_call_result(caller: SuccessfulInferenceResult | None, context: InferenceContext | None = None) Iterator[InferenceResult][source]#

Infer what the function returns when called.

infer_yield_result(context: InferenceContext | None = None)[source]#

Infer what the function yields when called

Returns:

What the function yields

Return type:

iterable(NodeNG or Uninferable) or None

is_abstract(pass_is_abstract=True, any_raise_is_abstract=False) bool[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.

is_bound() bool[source]#

Check if the function is bound to an instance or class.

Returns:

Whether the function is bound to an instance or class.

is_function: ClassVar[bool] = True#

Whether this node indicates a function.

For a FunctionDef this is always True.

Type:

bool

is_generator() bool[source]#

Check if this is a generator function.

Returns:

Whether this is a generator function.

is_method() bool[source]#

Check if this function node represents a method.

Returns:

Whether this is a method.

locals: dict[str, list[InferenceResult]]#

A map of the name of a local variable to the node defining it.

name = '<functiondef>'#

The name of the function.

postinit(args: Arguments, body: list[NodeNG], decorators: node_classes.Decorators | None = None, returns=None, type_comment_returns=None, type_comment_args=None, *, position: Position | None = None, doc_node: Const | None = None, type_params: list[nodes.TypeVar | nodes.ParamSpec | nodes.TypeVarTuple] | None = None)[source]#

Do some setup after initialisation.

Parameters:
  • args – The arguments that the function takes.

  • body – The contents of the function body.

  • decorators – The decorators that are applied to this method or function.

  • doc_node – The doc node associated with this node.

  • type_params – The type_params associated with this node.

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() Literal['builtins.instancemethod', 'builtins.function'][source]#

Get the name of the type that this node represents.

Returns:

The name of the type.

returns = None#
scope_lookup(node: LookupMixIn, name: str, offset: int = 0) tuple[LocalsDictNodeNG, list[nodes.NodeNG]][source]#

Lookup where the given name is assigned.

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

type_params: list[nodes.TypeVar | nodes.ParamSpec | nodes.TypeVarTuple]#

Type:

PEP 695 (Python 3.12+) type params, e.g. first ‘T’ in def func[T]() -> T