ClassDef#

class astroid.nodes.ClassDef(name: str, lineno: int, col_offset: int, parent: NodeNG, *, end_lineno: int | None, end_col_offset: int | None)[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>
ancestors(recurs: bool = True, context: InferenceContext | None = None) Generator[ClassDef, None, None][source]#

Iterate over the base classes in prefixed depth first order.

Parameters:

recurs – Whether to recurse or return direct ancestors only.

Returns:

The base classes

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.

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 class body.

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

Determine the boolean value of this node.

Returns:

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

callable() bool[source]#

Whether this node defines something that is callable.

Returns:

Whether this defines something that is callable. For a ClassDef this is always True.

declared_metaclass(context: InferenceContext | None = None) SuccessfulInferenceResult | 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.

decorators = None#

The decorators that are applied to this class.

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.

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, class_context: bool = True) list[InferenceResult][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 an Instance proxy at inference time. It may return an Uninferable object if the attribute has not been found, but a __getattr__ or __getattribute__ method is defined. If class_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. If class_context is used in that case, then a lookup in the implicit metaclass and the explicit metaclass will be done.

Parameters:
  • name – The attribute to look for.

  • class_context – Whether the attribute can be accessed statically.

Returns:

The attribute.

Raises:

AttributeInferenceError – If the attribute cannot be inferred.

getitem(index, context: InferenceContext | None = 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:

NodeNG

Raises:

AstroidTypeError – If this class does not define a __getitem__ method.

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

has_dynamic_getattr(context: InferenceContext | None = None) bool[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__.

has_metaclass_hack()[source]#
hide = False#
igetattr(name: str, context: InferenceContext | None = None, class_context: bool = True) Iterator[InferenceResult][source]#

Infer the possible values of the given variable.

Parameters:

name – The name of the variable to infer.

Returns:

The inferred possible values.

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

implicit_parameters() Literal[1][source]#
infer_binary_op(**kwargs: _P.kwargs) Generator[InferenceResult, None, None]#
infer_call_result(caller: SuccessfulInferenceResult | None, context: InferenceContext | None = None) Iterator[InferenceResult][source]#

infer what a class is returning when called

instance_attr(name, context: InferenceContext | None = 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.

instance_attr_ancestors(name, context: InferenceContext | None = None)[source]#

Iterate over the parents that define the given name as an attribute.

Parameters:

name (str) – The name to find definitions for.

Returns:

The parents that define the given name as an instance attribute.

Return type:

iterable(NodeNG)

instantiate_class() Instance[source]#

Get an Instance of the ClassDef node.

Returns:

An Instance of the ClassDef node

is_dataclass: bool#

Whether this class is a dataclass.

is_subtype_of(type_name, context: InferenceContext | None = None) bool[source]#

Whether this class is a subtype of the given type.

Parameters:

type_name (str) – The name of the type of check against.

Returns:

Whether this class is a subtype of the given type.

keywords: list[node_classes.Keyword]#

The keywords given to the class definition.

This is usually for PEP 3115 style metaclass declaration.

local_attr(name, context: InferenceContext | None = 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.

local_attr_ancestors(name, context: InferenceContext | None = None)[source]#

Iterate over the parents that define the given name.

Parameters:

name (str) – The name to find definitions for.

Returns:

The parents that define the given name.

Return type:

iterable(NodeNG)

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

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

metaclass(context: InferenceContext | None = None) SuccessfulInferenceResult | 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.

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: InferenceContext | None = None) list[ClassDef][source]#

Get the method resolution order, using C3 linearization.

Returns:

The list of ancestors, sorted by the mro.

Return type:

list(NodeNG)

Raises:
mymethods()[source]#

Iterate over all of the method defined in this class only.

Returns:

The methods defined on the class.

Return type:

iterable(FunctionDef)

name#

The name of the class.

property newstyle#

Whether this is a new style class or not

Type:

bool or None

postinit(bases: list[SuccessfulInferenceResult], body: list[NodeNG], decorators: node_classes.Decorators | None, newstyle: bool | None = None, metaclass: NodeNG | None = None, keywords: list[node_classes.Keyword] | None = None, *, position: Position | None = None, doc_node: Const | None = None, type_params: list[nodes.TypeVar | nodes.ParamSpec | nodes.TypeVarTuple] | None = None) None[source]#
pytype() Literal['builtins.type', 'builtins.classobj'][source]#

Get the name of the type that this node represents.

Returns:

The name of the type.

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

Lookup where the given name is assigned.

Parameters:
  • node – The node to look for assignments up to. Any assignments after the given node are ignored.

  • name – The name to find assignments for.

  • offset – 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).

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

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

Type:

PEP 695 (Python 3.12+) type params, e.g. class MyClass[T]