class documentation

class ClassDef(_base_nodes.FilterStmtsBaseNode, LocalsDictNodeNG, _base_nodes.Statement): (source)

View In Hierarchy

Class representing an :class:`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>

Method __init__ :param name: The name of the class. :type name: str or None
Method ancestors Iterate over the base classes in prefixed depth first order.
Method block_range Get a range from the given line number to where this node ends.
Method bool_value Determine the boolean value of this node.
Method callable Whether this node defines something that is callable.
Method declared_metaclass Return the explicit declared metaclass for the current class.
Method display_type A human readable type of this node.
Method doc.setter Undocumented
Method frame The node's frame node.
Method get_children Get the child nodes below this node.
Method getattr Get an attribute from this class, using Python's attribute semantic.
Method getitem Return the inference of a subscript.
Method has_base Whether this class directly inherits from the given node.
Method has_dynamic_getattr Check if the class has a custom __getattr__ or __getattribute__.
Method has_metaclass_hack Undocumented
Method igetattr Infer the possible values of the given variable.
Method implicit_locals Get implicitly defined class definition locals.
Method implicit_metaclass Get the implicit metaclass of the current class.
Method implicit_parameters Undocumented
Method infer_call_result infer what a class is returning when called
Method instance_attr Get the list of nodes associated to the given attribute name.
Method instance_attr_ancestors Iterate over the parents that define the given name as an attribute.
Method instantiate_class Get an :class:`Instance` of the :class:`ClassDef` node.
Method is_subtype_of Whether this class is a subtype of the given type.
Method local_attr Get the list of assign nodes associated to the given name.
Method local_attr_ancestors Iterate over the parents that define the given name.
Method metaclass Get the metaclass of this class.
Method methods Iterate over all of the method defined in this class and its parents.
Method mro Get the method resolution order, using C3 linearization.
Method mymethods Iterate over all of the method defined in this class only.
Method postinit Do some setup after initialisation.
Method pytype Get the name of the type that this node represents.
Method scope_lookup Lookup where the given name is assigned.
Method slots Get all the slots for this node.
Class Variable hide Undocumented
Class Variable infer_binary_op Undocumented
Class Variable newstyle Undocumented
Class Variable special_attributes The names of special attributes that this class has.
Class Variable type Undocumented
Instance Variable bases What the class inherits from.
Instance Variable body The contents of the class body.
Instance Variable decorators The decorators that are applied to this class.
Instance Variable doc_node The doc node associated with this node.
Instance Variable instance_attrs Undocumented
Instance Variable is_dataclass Whether this class is a dataclass.
Instance Variable keywords The keywords given to the class definition.
Instance Variable locals A map of the name of a local variable to the node defining it.
Instance Variable name The name of the class.
Instance Variable position Position of keyword(s) and name.
Property basenames The names of the parent classes
Property blockstart_tolineno The line on which the beginning of this block ends.
Property doc The class docstring.
Property fromlineno The first line that this node appears on in the source code.
Method _compute_mro Undocumented
Method _find_metaclass Undocumented
Method _get_assign_nodes Undocumented
Method _get_attribute_from_metaclass Undocumented
Method _infer_type_call Undocumented
Method _inferred_bases Undocumented
Method _islots Return an iterator with the inferred slots.
Method _metaclass_lookup_attribute Search the given name in the implicit and the explicit metaclass.
Method _newstyle_impl Undocumented
Method _slots Undocumented
Class Variable _astroid_fields Node attributes that contain child nodes.
Class Variable _other_fields Node attributes that do not contain child nodes.
Class Variable _other_other_fields Attributes that contain AST-dependent fields.
Class Variable _type Undocumented
Instance Variable _doc The class docstring.
Instance Variable _metaclass Undocumented
Instance Variable _metaclass_hack Undocumented
Instance Variable _newstyle Undocumented

Inherited from LocalsDictNodeNG:

Method __contains__ Check if a local is defined in this scope.
Method __getitem__ The first node the defines the given local.
Method __iter__ Iterate over the names of locals defined in this scoped node.
Method add_local_node Append a child that should alter the locals of this scope node.
Method items Get the names of the locals and the node that defines the local.
Method keys The names of locals defined in this scoped node.
Method qname Get the 'qualified' name of the node.
Method scope The first parent node defining a new scope.
Method set_local Define that the given name is declared in the given statement node.
Method values The nodes that define the locals in this scoped node.
Method _append_node append a child, linking it in the tree
Method _scope_lookup XXX method for interfacing the scope lookup

Inherited from LookupMixIn (via LocalsDictNodeNG):

Method ilookup Lookup the inferred values of the given variable.
Method lookup Lookup where the given variable is assigned.

Inherited from NodeNG (via LocalsDictNodeNG, LookupMixIn):

Method __repr__ Undocumented
Method __str__ Undocumented
Method accept Visit this node using the given visitor.
Method as_string Get the source code that this node represents.
Method child_sequence Search for the sequence that contains this child.
Method eq Undocumented
Method infer Get a generator of the inferred values.
Method inferred Get a list of the inferred values.
Method last_child An optimized version of list(get_children())[-1].
Method locate_child Find the field of this node that contains the given child.
Method next_sibling The next sibling statement node.
Method node_ancestors Yield parent, grandparent, etc until there are no more.
Method nodes_of_class Get the nodes (including this one or below) of the given types.
Method op_left_associative Undocumented
Method op_precedence Undocumented
Method parent_of Check if this node is the parent of the given node.
Method previous_sibling The previous sibling statement.
Method repr_tree Get a string representation of the AST from this node.
Method root Return the root node of the syntax tree.
Method statement The first parent node, including self, marked as statement node.
Class Variable is_function Whether this node indicates a function.
Class Variable is_lambda Undocumented
Class Variable is_statement Whether this node indicates a statement.
Class Variable optional_assign Whether this node optionally assigns a variable.
Instance Variable col_offset The column that this node appears on in the source code.
Instance Variable end_col_offset The end column this node appears on in the source code.
Instance Variable end_lineno The last line this node appears on in the source code.
Instance Variable lineno The line that this node appears on in the source code.
Instance Variable parent The parent node in the syntax tree.
Property tolineno The last line that this node appears on in the source code.
Method _fixed_source_line Attempt to find the line that this node appears on.
Method _get_name_nodes Undocumented
Method _get_return_nodes_skip_functions Undocumented
Method _get_yield_nodes_skip_lambdas Undocumented
Method _infer We don't know how to resolve a statement by default.
Method _infer_name Undocumented
Method _repr_name Get a name for nice representation.
Class Variable _explicit_inference Undocumented
@decorators_mod.deprecate_arguments(doc='Use the postinit arg \'doc_node\' instead')
def __init__(self, name=None, doc: str|None = None, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None): (source)

:param name: The name of the class. :type name: str or None :param doc: The class docstring. :param lineno: The line that this node appears on in the source code. :type lineno: int or None :param col_offset: The column that this node appears on in the source code. :type col_offset: int or None :param parent: The parent node in the syntax tree. :type parent: NodeNG or None :param end_lineno: The last line this node appears on in the source code. :type end_lineno: Optional[int] :param end_col_offset: The end column this node appears on in the source code. Note: This is after the last symbol. :type end_col_offset: Optional[int]

def ancestors(self, recurs: bool = True, context: InferenceContext|None = None) -> Generator[ClassDef, None, None]: (source)

Iterate over the base classes in prefixed depth first order. :param recurs: Whether to recurse or return direct ancestors only. :returns: The base classes

def block_range(self, lineno): (source)

Get a range from the given line number to where this node ends. :param lineno: Unused. :type lineno: int :returns: The range of line numbers that this node belongs to, :rtype: tuple(int, int)

def bool_value(self, context: InferenceContext|None = None) -> Literal[True]: (source)

Determine the boolean value of this node. :returns: The boolean value of this node. For a :class:`ClassDef` this is always ``True``.

def callable(self) -> bool: (source)

Whether this node defines something that is callable. :returns: Whether this defines something that is callable. For a :class:`ClassDef` this is always ``True``.

def declared_metaclass(self, context: InferenceContext|None = None) -> NodeNG|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.

def display_type(self) -> str: (source)

A human readable type of this node. :returns: The type of this node. :rtype: str

@doc.setter
def doc(self, value: str|None): (source)

Undocumented

def frame(self: _T, *, future: Literal[None, True] = None) -> _T: (source)

The node's frame node. A frame node is a :class:`Module`, :class:`FunctionDef`, :class:`ClassDef` or :class:`Lambda`. :returns: The node itself.

def get_children(self): (source)

Get the child nodes below this node.

def getattr(self, name: str, context: InferenceContext|None = None, class_context: bool = True) -> list[SuccessfulInferenceResult]: (source)

Get an attribute from this class, using Python's attribute semantic. This method doesn't look in the :attr:`instance_attrs` dictionary since it is done by an :class:`Instance` proxy at inference time. It may return an :class:`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. :param name: The attribute to look for. :param class_context: Whether the attribute can be accessed statically. :returns: The attribute. :raises AttributeInferenceError: If the attribute cannot be inferred.

def getitem(self, 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. :rtype: NodeNG :raises AstroidTypeError: If this class does not define a ``__getitem__`` method.

def has_base(self, node) -> bool: (source)

Whether this class directly inherits from the given node. :param node: The node to check for. :type node: NodeNG :returns: Whether this class directly inherits from the given node.

def has_dynamic_getattr(self, 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__.

def has_metaclass_hack(self): (source)

Undocumented

def igetattr(self, name: str, context: InferenceContext|None = None, class_context: bool = True) -> Iterator[InferenceResult]: (source)

Infer the possible values of the given variable. :param name: The name of the variable to infer. :returns: The inferred possible values.

def implicit_locals(self): (source)

Get implicitly defined class definition locals. :returns: the the name and Const pair for each local :rtype: tuple(tuple(str, node_classes.Const), ...)

def implicit_metaclass(self): (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. :rtype: builtins.type or None

def implicit_parameters(self) -> Literal[1]: (source)

Undocumented

def infer_call_result(self, caller, context: InferenceContext|None = None): (source)

infer what a class is returning when called

def instance_attr(self, 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. :rtype: list(NodeNG) :raises AttributeInferenceError: If no attribute with this name can be found in this class or parent classes.

def instance_attr_ancestors(self, name, context: InferenceContext|None = None): (source)

Iterate over the parents that define the given name as an attribute. :param name: The name to find definitions for. :type name: str :returns: The parents that define the given name as an instance attribute. :rtype: iterable(NodeNG)

def instantiate_class(self) -> bases.Instance: (source)

Get an :class:`Instance` of the :class:`ClassDef` node. :returns: An :class:`Instance` of the :class:`ClassDef` node

def is_subtype_of(self, type_name, context: InferenceContext|None = None) -> bool: (source)

Whether this class is a subtype of the given type. :param type_name: The name of the type of check against. :type type_name: str :returns: Whether this class is a subtype of the given type.

def local_attr(self, 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. :rtype: list(NodeNG) :raises AttributeInferenceError: If no attribute with this name can be found in this class or parent classes.

def local_attr_ancestors(self, name, context: InferenceContext|None = None): (source)

Iterate over the parents that define the given name. :param name: The name to find definitions for. :type name: str :returns: The parents that define the given name. :rtype: iterable(NodeNG)

def metaclass(self, context: InferenceContext|None = None) -> NodeNG|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.

def methods(self): (source)

Iterate over all of the method defined in this class and its parents. :returns: The methods defined on the class. :rtype: iterable(FunctionDef)

def mro(self, context: InferenceContext|None = None) -> list[ClassDef]: (source)

Get the method resolution order, using C3 linearization. :returns: The list of ancestors, sorted by the mro. :rtype: list(NodeNG) :raises DuplicateBasesError: Duplicate bases in the same class base :raises InconsistentMroError: A class' MRO is inconsistent

def mymethods(self): (source)

Iterate over all of the method defined in this class only. :returns: The methods defined on the class. :rtype: iterable(FunctionDef)

def postinit(self, bases, body, decorators, newstyle=None, metaclass: NodeNG|None = None, keywords=None, *, position: Position|None = None, doc_node: Const|None = None): (source)

Do some setup after initialisation. :param bases: What the class inherits from. :type bases: list(NodeNG) :param body: The contents of the class body. :type body: list(NodeNG) :param decorators: The decorators that are applied to this class. :type decorators: Decorators or None :param newstyle: Whether this is a new style class or not. :type newstyle: bool or None :param metaclass: The metaclass of this class. :param keywords: The keywords given to the class definition. :type keywords: list(Keyword) or None :param position: Position of class keyword and name. :param doc_node: The doc node associated with this node.

def pytype(self) -> Literal['builtins.type', 'builtins.classobj']: (source)

Get the name of the type that this node represents. :returns: The name of the type.

def scope_lookup(self, node, name, offset=0): (source)

Lookup where the given name is assigned. :param node: The node to look for assignments up to. Any assignments after the given node are ignored. :type node: NodeNG :param name: The name to find assignments for. :type name: str :param offset: The line offset to filter statements up to. :type offset: int :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). :rtype: tuple(str, list(NodeNG))

@decorators_mod.cached
def slots(self): (source)

Get all the slots for this node. :returns: The names of slots for this class. If the class doesn't define any slot, through the ``__slots__`` variable, then this function will return a None. Also, it will return None in the case the slots were not inferred. :rtype: list(str) or None

Undocumented

infer_binary_op: ClassVar[InferBinaryOp[ClassDef]] = (source)

Undocumented

newstyle = (source)

Undocumented

special_attributes = (source)

The names of special attributes that this class has. :type: objectmodel.ClassModel

Undocumented

What the class inherits from.

The contents of the class body. :type: list(NodeNG)

decorators = (source)

The decorators that are applied to this class. :type: Decorators or None

doc_node = (source)

The doc node associated with this node.

instance_attrs: dict = (source)

Undocumented

is_dataclass: bool = (source)

Whether this class is a dataclass.

keywords = (source)

The keywords given to the class definition. This is usually for :pep:`3115` style metaclass declaration. :type: list(Keyword) or None

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

The name of the class. :type name: str or None

position = (source)

Position of keyword(s) and name. Used as fallback for block nodes which might not provide good enough positional information. E.g. ClassDef, FunctionDef.

The names of the parent classes Names are given in the order they appear in the class definition. :type: list(str)

@cached_property
blockstart_tolineno = (source)

The line on which the beginning of this block ends. :type: int

The class docstring.

@cached_property
fromlineno: int|None = (source)

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

def _compute_mro(self, context: InferenceContext|None = None): (source)

Undocumented

def _find_metaclass(self, seen: set[ClassDef]|None = None, context: InferenceContext|None = None) -> NodeNG|None: (source)

Undocumented

@decorators_mod.cached
def _get_assign_nodes(self): (source)
def _get_attribute_from_metaclass(self, cls, name, context): (source)

Undocumented

def _infer_type_call(self, caller, context): (source)

Undocumented

def _inferred_bases(self, context: InferenceContext|None = None): (source)

Undocumented

def _islots(self): (source)

Return an iterator with the inferred slots.

@lru_cache(maxsize=1024)
def _metaclass_lookup_attribute(self, name, context): (source)

Search the given name in the implicit and the explicit metaclass.

def _newstyle_impl(self, context: InferenceContext|None = None): (source)

Undocumented

def _slots(self): (source)

Undocumented

_astroid_fields: tuple[str, ...] = (source)

Node attributes that contain child nodes. This is redefined in most concrete classes.

_other_fields: tuple[str, ...] = (source)

Node attributes that do not contain child nodes.

_other_other_fields: tuple[str, ...] = (source)

Attributes that contain AST-dependent fields.

Undocumented

The class docstring.

_metaclass = (source)

Undocumented

_metaclass_hack: bool = (source)

Undocumented

_newstyle = (source)

Undocumented