class documentation

class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement): (source)

View In Hierarchy

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>
Method __init__
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 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 Instance of the 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 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 The class' docstring.
Instance Variable instance​_attrs Undocumented
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.
Property basenames The names of the parent classes
Property blockstart​_tolineno The line on which the beginning of this block ends.
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 _metaclass Undocumented
Instance Variable _metaclass​_hack Undocumented
Instance Variable _newstyle Undocumented

Inherited from FilterStmtsMixin:

Method assign​_type Undocumented
Method _get​_filtered​_stmts method used in _filter_stmts to get statements and trigger break

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 Statement:

Method next​_sibling The next sibling statement node.
Method previous​_sibling The previous sibling statement.
Class Variable is​_statement Whether this node indicates a statement.

Inherited from NodeNG (via Statement):

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 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 repr​_tree Get a string representation of the AST from this node.
Method root Return the root node of the syntax tree.
Method scope The first parent node defining a new scope. These can be Module, FunctionDef, ClassDef, Lambda, or GeneratorExp nodes.
Method set​_local Define that the given name is declared in the given statement node.
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 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. Note: This is after the last symbol.
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 fromlineno The first line that this node appears on in the source code.
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
def __init__(self, name=None, doc=None, lineno=None, col_offset=None, parent=None, *, end_lineno=None, end_col_offset=None): (source)
Parameters
name:str or NoneThe name of the class.
doc:str or NoneThe function's docstring.
lineno:int or NoneThe line that this node appears on in the source code.
col​_offset:int or NoneThe column that this node appears on in the source code.
parent:NodeNG or NoneThe parent node in the syntax tree.
end​_lineno:Optional[int]The last line this node appears on in the source code.
end​_col​_offset:Optional[int]The end column this node appears on in the source code. Note: This is after the last symbol.
def ancestors(self, recurs=True, context=None): (source)
Iterate over the base classes in prefixed depth first order.
Parameters
recurs:boolWhether to recurse or return direct ancestors only.
contextUndocumented
Returns
iterable(NodeNG)The base classes
def block_range(self, lineno): (source)
Get a range from the given line number to where this node ends.
Parameters
lineno:intUnused.
Returns
tuple(int, int)The range of line numbers that this node belongs to,
def bool_value(self, context=None): (source)
Determine the boolean value of this node.
Returns
boolThe boolean value of this node. For a ClassDef this is always True.
def callable(self): (source)
Whether this node defines something that is callable.
Returns
boolTrue if this defines something that is callable, False otherwise. For a ClassDef this is always True.
def declared_metaclass(self, 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
NodeNG or NoneThe metaclass of this class, or None if one could not be found.
def display_type(self): (source)
A human readable type of this node.
Returns
strThe type of this node.
def frame(self, *, future=None): (source)

The node's frame node.

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

Parameters
future:Literal[None, True]Undocumented
Returns
TThe node itself.
def get_children(self): (source)
Get the child nodes below this node.
def getattr(self, 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 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:strThe attribute to look for.
contextUndocumented
class​_context:boolWhether the attribute can be accessed statically.
Returns
list(NodeNG)The attribute.
Raises
AttributeInferenceErrorIf the attribute cannot be inferred.
def getitem(self, index, context=None): (source)

Return the inference of a subscript.

This is basically looking up the method in the metaclass and calling it.

Returns
NodeNGThe inferred value of a subscript to this class.
Raises
AstroidTypeErrorIf this class does not define a __getitem__ method.
def has_base(self, node): (source)
Whether this class directly inherits from the given node.
Parameters
node:NodeNGThe node to check for.
Returns
boolTrue if this class directly inherits from the given node.
def has_dynamic_getattr(self, 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
boolTrue if the class has a custom __getattr__ or __getattribute__, False otherwise.
def has_metaclass_hack(self): (source)

Undocumented

def igetattr(self, name, context=None, class_context=True): (source)
Infer the possible values of the given variable.
Parameters
name:strThe name of the variable to infer.
contextUndocumented
class​_contextUndocumented
Returns
iterable(NodeNG or Uninferable)The inferred possible values.
def implicit_locals(self): (source)
Get implicitly defined class definition locals.
Returns
tuple(tuple(str, node_classes.Const), ...)the the name and Const pair for each local
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
builtins.type or NoneThe metaclass.
def implicit_parameters(self): (source)

Undocumented

def infer_call_result(self, caller, context=None): (source)
infer what a class is returning when called
def instance_attr(self, 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
list(NodeNG)The list of assignments to the given name.
Raises
AttributeInferenceErrorIf no attribute with this name can be found in this class or parent classes.
def instance_attr_ancestors(self, name, context=None): (source)
Iterate over the parents that define the given name as an attribute.
Parameters
name:strThe name to find definitions for.
contextUndocumented
Returns
iterable(NodeNG)The parents that define the given name as an instance attribute.
def instantiate_class(self): (source)
Get an Instance of the ClassDef node.
Returns
Instance or ClassDefAn Instance of the ClassDef node, or self if this is not possible.
def is_subtype_of(self, type_name, context=None): (source)
Whether this class is a subtype of the given type.
Parameters
type​_name:strThe name of the type of check against.
contextUndocumented
Returns
boolTrue if this class is a subtype of the given type, False otherwise.
def local_attr(self, 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
list(NodeNG)The list of assignments to the given name.
Raises
AttributeInferenceErrorIf no attribute with this name can be found in this class or parent classes.
def local_attr_ancestors(self, name, context=None): (source)
Iterate over the parents that define the given name.
Parameters
name:strThe name to find definitions for.
contextUndocumented
Returns
iterable(NodeNG)The parents that define the given name.
def metaclass(self, 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
NodeNG or NoneThe metaclass of this class.
def methods(self): (source)
Iterate over all of the method defined in this class and its parents.
Returns
iterable(FunctionDef)The methods defined on the class.
def mro(self, context=None): (source)
Get the method resolution order, using C3 linearization.
Returns
list(NodeNG)The list of ancestors, sorted by the mro.
Raises
DuplicateBasesErrorDuplicate bases in the same class base
InconsistentMroErrorA class' MRO is inconsistent
def mymethods(self): (source)
Iterate over all of the method defined in this class only.
Returns
iterable(FunctionDef)The methods defined on the class.
def postinit(self, bases, body, decorators, newstyle=None, metaclass=None, keywords=None): (source)
Do some setup after initialisation.
Parameters
bases:list(NodeNG)What the class inherits from.
body:list(NodeNG)The contents of the class body.
decorators:Decorators or NoneThe decorators that are applied to this class.
newstyle:bool or NoneWhether this is a new style class or not.
metaclass:NodeNG or NoneThe metaclass of this class.
keywords:list(Keyword) or NoneThe keywords given to the class definition.
def pytype(self): (source)
Get the name of the type that this node represents.
Returns
strThe name of the type.
def scope_lookup(self, node, name, offset=0): (source)
Lookup where the given name is assigned.
Parameters
node:NodeNGThe node to look for assignments up to. Any assignments after the given node are ignored.
name:strThe name to find assignments for.
offset:intThe line offset to filter statements up to.
Returns
tuple(str, list(NodeNG))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).
@decorators_mod.cached
def slots(self): (source)
Get all the slots for this node.
Returns
list(str) or NoneThe 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.
hide: bool = (source)

Undocumented

newstyle = (source)

Undocumented

special_attributes = (source)
The names of special attributes that this class has.
type = (source)

Undocumented

bases = (source)
What the class inherits from.
body = (source)
The contents of the class body.
decorators = (source)
The decorators that are applied to this class.
The class' docstring.
instance_attrs: dict = (source)

Undocumented

keywords = (source)

The keywords given to the class definition.

This is usually for PEP 3115 style metaclass declaration.

locals: dict = (source)
A map of the name of a local variable to the node defining it.
name = (source)
The name of the class.
@property
basenames = (source)

The names of the parent classes

Names are given in the order they appear in the class definition.

@decorators_mod.cachedproperty
blockstart_tolineno = (source)
The line on which the beginning of this block ends.
def _compute_mro(self, context=None): (source)

Undocumented

def _find_metaclass(self, seen=None, context=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=None): (source)

Undocumented

def _islots(self): (source)
Return an iterator with the inferred slots.
def _metaclass_lookup_attribute(self, name, context): (source)
Search the given name in the implicit and the explicit metaclass.
def _newstyle_impl(self, context=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.
_type = (source)

Undocumented

_metaclass = (source)

Undocumented

_metaclass_hack: bool = (source)

Undocumented

_newstyle = (source)

Undocumented