class documentation

class ClassChecker(BaseChecker): (source)

View In Hierarchy

Checker for class nodes. Checks for : * methods without self as first argument * overridden methods signature * access only to existent members via self * attributes not defined in the __init__ method * unreachable code

Method __init__ Checker instances should have the linter as argument.
Method leave_classdef Checker for Class nodes.
Method leave_functiondef On method node, check if this method couldn't be a function.
Method open Called before visiting project (i.e. set of modules).
Method visit_assign Undocumented
Method visit_assignattr Undocumented
Method visit_attribute Check if the getattr is an access to a class member if so, register it.
Method visit_classdef Init visit variable _accessed.
Method visit_functiondef Check method arguments, overriding.
Class Variable name Name of the provider.
Class Variable options Options provided by this provider.
Static Method _is_called_inside_special_method Returns true if the node is located inside a special (aka dunder) method.
Static Method _is_class_or_instance_attribute Check if the given attribute *name* is a class or instance member of the given *klass*.
Static Method _is_classmethod Check if the given *func* node is a class method.
Static Method _is_inferred_instance Check if the inferred value of the given *expr* is an instance of *klass*.
Method _check_accessed_members Check that accessed members are defined.
Method _check_attribute_defined_outside_init Undocumented
Method _check_bases_classes Check that the given class node implements abstract methods from base classes.
Method _check_classmethod_declaration Checks for uses of classmethod() or staticmethod().
Method _check_consistent_mro Detect that a class has a consistent mro or duplicate bases.
Method _check_enum_base Undocumented
Method _check_first_arg_config Undocumented
Method _check_first_arg_for_type Check the name of first argument, expect:.
Method _check_in_slots Check that the given AssignAttr node is defined in the class slots.
Method _check_init Check that the __init__ method call super or ancestors'__init__ method (unless it is used for type hinting with `typing.overload`).
Method _check_invalid_class_object Undocumented
Method _check_invalid_overridden_method Undocumented
Method _check_proper_bases Detect that a class inherits something which is not a class or a type.
Method _check_property_with_parameters Undocumented
Method _check_protected_attribute_access Given an attribute access node (set or get), check if attribute access is legitimate.
Method _check_redefined_slots Check if `node` redefines a slot which is defined in an ancestor class.
Method _check_signature Check that the signature of the two given methods match.
Method _check_slots Undocumented
Method _check_slots_elt Undocumented
Method _check_super_without_brackets Check if there is a function call on a super call without brackets.
Method _check_typing_final Detect that a class does not subclass a class decorated with `typing.final`.
Method _check_unused_private_attributes Undocumented
Method _check_unused_private_functions Undocumented
Method _check_unused_private_variables Check if private variables are never used within a class.
Method _check_useless_super_delegation Check if the given function node is an useless method override.
Method _is_mandatory_method_param Check if nodes.Name corresponds to first attribute variable name.
Method _is_type_self_call Undocumented
Method _uses_mandatory_method_param Check that attribute lookup name use first attribute variable name.
Instance Variable _accessed Undocumented
Instance Variable _first_attrs Undocumented
Instance Variable _mixin_class_rgx Undocumented
Instance Variable _py38_plus Undocumented
Property _dummy_rgx Undocumented

Inherited from BaseChecker:

Method __eq__ Permit to assert Checkers are equal.
Method __gt__ Sorting of checkers.
Method __hash__ Make Checker hashable.
Method __repr__ Undocumented
Method __str__ This might be incomplete because multiple classes inheriting BaseChecker can have the same name.
Method add_message Undocumented
Method check_consistency Check the consistency of msgid.
Method close Called after visiting project (i.e set of modules).
Method create_message_definition_from_tuple Undocumented
Method get_full_documentation Undocumented
Method get_map_data Undocumented
Method get_message_definition Undocumented
Method reduce_map_data Undocumented
Class Variable enabled Undocumented
Class Variable msgs Undocumented
Class Variable reports Undocumented
Instance Variable linter Undocumented
Property messages Undocumented

Inherited from _ArgumentsProvider (via BaseChecker):

Method get_option_def DEPRECATED: Return the dictionary defining an option given its name.
Method level.setter Undocumented
Method load_defaults DEPRECATED: Initialize the provider using default values.
Method option_attrname DEPRECATED: Get the config attribute corresponding to opt.
Method option_value DEPRECATED: Get the current value for the given option.
Method options_and_values DEPRECATED.
Method options_by_section DEPRECATED: Return an iterator on options grouped by section.
Method set_option DEPRECATED: Method called to set an option (registered in the options list).
Class Variable option_groups_descs Option groups of this provider and their descriptions.
Property config Undocumented
Property level Undocumented
Instance Variable _arguments_manager The manager that will parse and register any options provided.
Instance Variable _level Undocumented
def __init__(self, linter: PyLinter): (source)

Checker instances should have the linter as argument.

@only_required_for_messages('unused-private-member', 'attribute-defined-outside-init', 'access-member-before-definition')
def leave_classdef(self, node: nodes.ClassDef): (source)

Checker for Class nodes. check that instance attributes are defined in __init__ and check access to existent members

def leave_functiondef(self, node: nodes.FunctionDef): (source)

On method node, check if this method couldn't be a function. ignore class, static and abstract methods, initializer, methods overridden from a parent class.

def open(self): (source)

Called before visiting project (i.e. set of modules).

@only_required_for_messages('protected-access', 'no-classmethod-decorator', 'no-staticmethod-decorator')
def visit_assign(self, assign_node: nodes.Assign): (source)

Undocumented

@only_required_for_messages('assigning-non-slot', 'invalid-class-object', 'access-member-before-definition')
def visit_assignattr(self, node: nodes.AssignAttr): (source)

Undocumented

def visit_attribute(self, node: nodes.Attribute): (source)

Check if the getattr is an access to a class member if so, register it. Also check for access to protected class member from outside its class (but ignore __special__ methods)

@only_required_for_messages('abstract-method', 'invalid-slots', 'single-string-used-for-slots', 'invalid-slots-object', 'class-variable-slots-conflict', 'inherit-non-class', 'useless-object-inheritance', 'inconsistent-mro', 'duplicate-bases', 'redefined-slots-in-subclass', 'invalid-enum-extension', 'subclassed-final-class', 'implicit-flag-alias')
def visit_classdef(self, node: nodes.ClassDef): (source)

Init visit variable _accessed.

def visit_functiondef(self, node: nodes.FunctionDef): (source)

Check method arguments, overriding.

Name of the provider.

options: tuple[tuple, ...] = (source)

Options provided by this provider.

@staticmethod
def _is_called_inside_special_method(node: nodes.NodeNG) -> bool: (source)

Returns true if the node is located inside a special (aka dunder) method.

@staticmethod
def _is_class_or_instance_attribute(name: str, klass: nodes.ClassDef) -> bool: (source)

Check if the given attribute *name* is a class or instance member of the given *klass*. Returns ``True`` if the name is a property in the given klass, ``False`` otherwise.

@staticmethod
def _is_classmethod(func: LocalsDictNodeNG) -> bool: (source)

Check if the given *func* node is a class method.

@staticmethod
def _is_inferred_instance(expr: nodes.NodeNG, klass: nodes.ClassDef) -> bool: (source)

Check if the inferred value of the given *expr* is an instance of *klass*.

def _check_accessed_members(self, node: nodes.ClassDef, accessed: dict[str, list[_AccessNodes]]): (source)

Check that accessed members are defined.

def _check_attribute_defined_outside_init(self, cnode: nodes.ClassDef): (source)

Undocumented

def _check_bases_classes(self, node: nodes.ClassDef): (source)

Check that the given class node implements abstract methods from base classes.

def _check_classmethod_declaration(self, node: nodes.Assign): (source)

Checks for uses of classmethod() or staticmethod(). When a @classmethod or @staticmethod decorator should be used instead. A message will be emitted only if the assignment is at a class scope and only if the classmethod's argument belongs to the class where it is defined. `node` is an assign node.

def _check_consistent_mro(self, node: nodes.ClassDef): (source)

Detect that a class has a consistent mro or duplicate bases.

def _check_enum_base(self, node: nodes.ClassDef, ancestor: nodes.ClassDef): (source)

Undocumented

def _check_first_arg_config(self, first: str|None, config: Sequence[str], node: nodes.FunctionDef, message: str, method_name: str): (source)

Undocumented

def _check_first_arg_for_type(self, node: nodes.FunctionDef, metaclass: bool): (source)

Check the name of first argument, expect:. * 'self' for a regular method * 'cls' for a class method or a metaclass regular method (actually valid-classmethod-first-arg value) * 'mcs' for a metaclass class method (actually valid-metaclass-classmethod-first-arg) * not one of the above for a static method

def _check_in_slots(self, node: nodes.AssignAttr): (source)

Check that the given AssignAttr node is defined in the class slots.

def _check_init(self, node: nodes.FunctionDef, klass_node: nodes.ClassDef): (source)

Check that the __init__ method call super or ancestors'__init__ method (unless it is used for type hinting with `typing.overload`).

def _check_invalid_class_object(self, node: nodes.AssignAttr): (source)

Undocumented

def _check_invalid_overridden_method(self, function_node: nodes.FunctionDef, parent_function_node: nodes.FunctionDef): (source)

Undocumented

def _check_proper_bases(self, node: nodes.ClassDef): (source)

Detect that a class inherits something which is not a class or a type.

def _check_property_with_parameters(self, node: nodes.FunctionDef): (source)

Undocumented

def _check_protected_attribute_access(self, node: nodes.Attribute|nodes.AssignAttr): (source)

Given an attribute access node (set or get), check if attribute access is legitimate. Call _check_first_attr with node before calling this method. Valid cases are: * self._attr in a method or cls._attr in a classmethod. Checked by _check_first_attr. * Klass._attr inside "Klass" class. * Klass2._attr inside "Klass" class when Klass2 is a base class of Klass.

def _check_redefined_slots(self, node: nodes.ClassDef, slots_node: nodes.NodeNG, slots_list: list[nodes.NodeNG]): (source)

Check if `node` redefines a slot which is defined in an ancestor class.

def _check_signature(self, method1: nodes.FunctionDef, refmethod: nodes.FunctionDef, cls: nodes.ClassDef): (source)

Check that the signature of the two given methods match.

def _check_slots(self, node: nodes.ClassDef): (source)

Undocumented

def _check_slots_elt(self, elt: SuccessfulInferenceResult, node: nodes.ClassDef): (source)

Undocumented

def _check_super_without_brackets(self, node: nodes.Attribute): (source)

Check if there is a function call on a super call without brackets.

def _check_typing_final(self, node: nodes.ClassDef): (source)

Detect that a class does not subclass a class decorated with `typing.final`.

def _check_unused_private_attributes(self, node: nodes.ClassDef): (source)

Undocumented

def _check_unused_private_functions(self, node: nodes.ClassDef): (source)

Undocumented

def _check_unused_private_variables(self, node: nodes.ClassDef): (source)

Check if private variables are never used within a class.

def _check_useless_super_delegation(self, function: nodes.FunctionDef): (source)

Check if the given function node is an useless method override. We consider it *useless* if it uses the super() builtin, but having nothing additional whatsoever than not implementing the method at all. If the method uses super() to delegate an operation to the rest of the MRO, and if the method called is the same as the current one, the arguments passed to super() are the same as the parameters that were passed to this method, then the method could be removed altogether, by letting other implementation to take precedence.

def _is_mandatory_method_param(self, node: nodes.NodeNG) -> bool: (source)

Check if nodes.Name corresponds to first attribute variable name. Name is `self` for method, `cls` for classmethod and `mcs` for metaclass. Static methods return False.

def _is_type_self_call(self, expr: nodes.NodeNG) -> bool: (source)

Undocumented

def _uses_mandatory_method_param(self, node: (nodes.Attribute|nodes.Assign)|nodes.AssignAttr) -> bool: (source)

Check that attribute lookup name use first attribute variable name. Name is `self` for method, `cls` for classmethod and `mcs` for metaclass.

_accessed = (source)

Undocumented

_first_attrs: list[str|None] = (source)

Undocumented

_mixin_class_rgx = (source)

Undocumented

_py38_plus = (source)

Undocumented

@cached_property
_dummy_rgx: Pattern[str] = (source)

Undocumented