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] [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.
- 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,
- 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 alwaysTrue
.
- callable() bool [source]¶
Whether this node defines something that is callable.
- Returns:
Whether this defines something that is callable. For a
ClassDef
this is alwaysTrue
.
- 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:
- frame(*, future: Literal[None, True] = None) _T [source]¶
The node’s frame node.
A frame node is a
Module
,FunctionDef
,ClassDef
orLambda
.- Returns:
The node itself.
- 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 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 – 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:
- 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__.
- 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.
This will return an instance of builtins.type.
- Returns:
The metaclass.
- Return type:
builtins.type
- infer_binary_op(**kwargs: _P.kwargs) Generator[InferenceResult] ¶
- 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:
- 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.
- instantiate_class() Instance [source]¶
Get an
Instance
of theClassDef
node.- Returns:
An
Instance
of theClassDef
node
- 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:
- 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.
- 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:
- Raises:
DuplicateBasesError – Duplicate bases in the same class base
InconsistentMroError – A class’ MRO is inconsistent
- 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.
- 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'] [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).
- special_attributes¶
The names of special attributes that this class has.
- Type:
objectmodel.ClassModel
- property type: Literal['class', 'exception', 'metaclass']¶
The class type for this node.
Possible values are: class, metaclass, exception.
- Type:
- type_params: list[nodes.TypeVar | nodes.ParamSpec | nodes.TypeVarTuple]¶
…
- Type:
PEP 695 (Python 3.12+) type params, e.g. class MyClass[T]