class ClassDef(mixins.FilterStmtsMixin, LocalsDictNodeNG, node_classes.Statement): (source)
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 |
astroid.nodes.NodeNG.__init__
Parameters | |
name:str or None | The name of the class. |
doc:str or None | The function's docstring. |
lineno:int or None | The line that this node appears on in the source code. |
col_offset:int or None | The column that this node appears on in the source code. |
parent:NodeNG or None | The 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. |
Parameters | |
recurs:bool | Whether to recurse or return direct ancestors only. |
context | Undocumented |
Returns | |
iterable(NodeNG) | The base classes |
astroid.nodes.NodeNG.block_range
Parameters | |
lineno:int | Unused. |
Returns | |
tuple(int, int) | The range of line numbers that this node belongs to, |
astroid.nodes.NodeNG.bool_value
Returns | |
bool | The boolean value of this node.
For a ClassDef this is always True. |
astroid.nodes.NodeNG.callable
Returns | |
bool | True if this defines something that is callable,
False otherwise.
For a ClassDef this is always True. |
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 None | The metaclass of this class, or None if one could not be found. |
astroid.nodes.NodeNG.frame
The node's frame node.
A frame node is a Module
, FunctionDef
,
ClassDef
or Lambda
.
Parameters | |
future:Literal[ | Undocumented |
Returns | |
T | The node itself. |
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:str | The attribute to look for. |
context | Undocumented |
class_context:bool | Whether the attribute can be accessed statically. |
Returns | |
list(NodeNG) | The attribute. |
Raises | |
AttributeInferenceError | If the attribute cannot be inferred. |
Return the inference of a subscript.
This is basically looking up the method in the metaclass and calling it.
Returns | |
NodeNG | The inferred value of a subscript to this class. |
Raises | |
AstroidTypeError | If this class does not define a __getitem__ method. |
astroid.nodes.NodeNG.has_base
Parameters | |
node:NodeNG | The node to check for. |
Returns | |
bool | True if this class directly inherits from the given node. |
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 | |
bool | True if the class has a custom __getattr__ or __getattribute__, False otherwise. |
Parameters | |
name:str | The name of the variable to infer. |
context | Undocumented |
class_context | Undocumented |
Returns | |
iterable(NodeNG or Uninferable) | The inferred possible values. |
Returns | |
tuple(tuple(str, node_classes.Const), ...) | the the name and Const pair for each local |
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 None | The metaclass. |
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 | |
AttributeInferenceError | If no attribute with this name can be found in this class or parent classes. |
Parameters | |
name:str | The name to find definitions for. |
context | Undocumented |
Returns | |
iterable(NodeNG) | The parents that define the given name as an instance attribute. |
Parameters | |
type_name:str | The name of the type of check against. |
context | Undocumented |
Returns | |
bool | True if this class is a subtype of the given type, False otherwise. |
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 | |
AttributeInferenceError | If no attribute with this name can be found in this class or parent classes. |
Parameters | |
name:str | The name to find definitions for. |
context | Undocumented |
Returns | |
iterable(NodeNG) | The parents that define the given name. |
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 None | The metaclass of this class. |
Returns | |
iterable(FunctionDef) | The methods defined on the class. |
Returns | |
list(NodeNG) | The list of ancestors, sorted by the mro. |
Raises | |
DuplicateBasesError | Duplicate bases in the same class base |
InconsistentMroError | A class' MRO is inconsistent |
Returns | |
iterable(FunctionDef) | The methods defined on the class. |
Parameters | |
bases:list(NodeNG) | What the class inherits from. |
body:list(NodeNG) | The contents of the class body. |
decorators:Decorators or None | The decorators that are applied to this class. |
newstyle:bool or None | Whether this is a new style class or not. |
metaclass:NodeNG or None | The metaclass of this class. |
keywords:list(Keyword) or None | The keywords given to the class definition. |
Parameters | |
node:NodeNG | The node to look for assignments up to. Any assignments after the given node are ignored. |
name:str | The name to find assignments for. |
offset:int | The 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). |
Returns | |
list(str) or None | 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. |
The keywords given to the class definition.
This is usually for PEP 3115 style metaclass declaration.
astroid.nodes.NodeNG._astroid_fields
Node attributes that contain child nodes.
This is redefined in most concrete classes.