class documentation

class VariablesChecker(BaseChecker): (source)

View In Hierarchy

BaseChecker for variables. Checks for * unused variables / imports * undefined variables * redefinition of variable from builtins or from an outer scope or except handler * use of variable before assignment * __all__ consistency * self/cls assignment

Method __init__ Checker instances should have the linter as argument.
Method leave_assign Undocumented
Method leave_classdef Leave class: update consumption analysis variable.
Method leave_dictcomp Leave dictcomp: update consumption analysis variable.
Method leave_excepthandler Undocumented
Method leave_for Undocumented
Method leave_functiondef Leave function: check function's locals are consumed.
Method leave_generatorexp Leave genexpr: update consumption analysis variable.
Method leave_lambda Leave lambda: update consumption analysis variable.
Method leave_listcomp Leave listcomp: update consumption analysis variable.
Method leave_module Leave module: check globals.
Method leave_setcomp Leave setcomp: update consumption analysis variable.
Method leave_with Undocumented
Method visit_arguments Undocumented
Method visit_assign Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned.
Method visit_assignname Undocumented
Method visit_classdef Visit class: update consumption analysis variable.
Method visit_const Take note of names that appear inside string literal type annotations unless the string is a parameter to `typing.Literal` or `typing.Annotation`.
Method visit_delname Undocumented
Method visit_dictcomp Visit dictcomp: update consumption analysis variable.
Method visit_excepthandler Undocumented
Method visit_for Undocumented
Method visit_functiondef Visit function: update consumption analysis variable and check locals.
Method visit_generatorexp Visit genexpr: update consumption analysis variable.
Method visit_global Check names imported exists in the global scope.
Method visit_import Check modules attribute accesses.
Method visit_importfrom Check modules attribute accesses.
Method visit_lambda Visit lambda: update consumption analysis variable.
Method visit_listcomp Visit listcomp: update consumption analysis variable.
Method visit_module Visit module : update consumption analysis variable checks globals doesn't overrides builtins.
Method visit_name Don't add the 'utils.only_required_for_messages' decorator here!
Method visit_setcomp Visit setcomp: update consumption analysis variable.
Method visit_subscript Undocumented
Class Variable name Name of the provider.
Class Variable options Options provided by this provider.
Static Method _comprehension_between_frame_and_node Return True if a ComprehensionScope intervenes between `node` and its frame.
Static Method _defined_in_function_definition Undocumented
Static Method _in_lambda_or_comprehension_body Return True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.
Static Method _is_first_level_self_reference Check if a first level method's annotation or default values refers to its own class, and return a consumer action.
Static Method _is_never_evaluated Check if a NamedExpr is inside a side of if ... else that never gets evaluated.
Static Method _is_variable_violation Undocumented
Static Method _maybe_used_and_assigned_at_once Check if `defstmt` has the potential to use and assign a name in the same statement.
Static Method _nodes_to_unpack Return the list of values of the `Assign` node.
Method _allowed_redefined_builtin Undocumented
Method _check_all Undocumented
Method _check_classdef_metaclasses Undocumented
Method _check_consumer Checks a consumer for conditions that should trigger messages.
Method _check_globals Undocumented
Method _check_imports Undocumented
Method _check_is_unused Undocumented
Method _check_late_binding_closure Check whether node is a cell var that is assigned within a containing loop.
Method _check_metaclasses Update consumption analysis for metaclasses.
Method _check_module_attrs Check that module_names (list of string) are accessible through the given module, if the latest access name corresponds to a module, return it.
Method _check_potential_index_error Check for the potential-index-error message.
Method _check_self_cls_assign Check that self/cls don't get assigned.
Method _check_unpacking Check for unbalanced tuple unpacking and unpacking non sequences.
Method _check_unused_arguments Undocumented
Method _ignore_class_scope Return True if the node is in a local class scope, as an assignment.
Method _is_name_ignored Undocumented
Method _is_only_type_assignment Check if variable only gets assigned a type and never a value.
Method _loopvar_name Undocumented
Method _report_unbalanced_unpacking Undocumented
Method _report_unpacking_non_sequence Undocumented
Method _should_ignore_redefined_builtin Undocumented
Method _should_node_be_skipped Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks.
Method _store_type_annotation_names Undocumented
Method _store_type_annotation_node Given a type annotation, store all the name nodes it refers to.
Method _undefined_and_used_before_checker Undocumented
Instance Variable _except_handler_names_queue This is a queue, last in first out.
Instance Variable _postponed_evaluation_enabled Undocumented
Instance Variable _to_consume Undocumented
Instance Variable _type_annotation_names Undocumented
Property _allow_global_unused_variables Undocumented
Property _analyse_fallback_blocks Undocumented
Property _ignored_modules 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 open Called before visiting project (i.e. set of modules).
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.

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

Undocumented

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

Leave class: update consumption analysis variable.

def leave_dictcomp(self, _: nodes.DictComp): (source)

Leave dictcomp: update consumption analysis variable.

@utils.only_required_for_messages('redefined-outer-name')
def leave_excepthandler(self, node: nodes.ExceptHandler): (source)

Undocumented

def leave_for(self, node: nodes.For): (source)

Undocumented

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

Leave function: check function's locals are consumed.

def leave_generatorexp(self, _: nodes.GeneratorExp): (source)

Leave genexpr: update consumption analysis variable.

def leave_lambda(self, _: nodes.Lambda): (source)

Leave lambda: update consumption analysis variable.

def leave_listcomp(self, _: nodes.ListComp): (source)

Leave listcomp: update consumption analysis variable.

@utils.only_required_for_messages('unused-import', 'unused-wildcard-import', 'redefined-builtin', 'undefined-all-variable', 'invalid-all-object', 'invalid-all-format', 'unused-variable', 'undefined-variable')
def leave_module(self, node: nodes.Module): (source)

Leave module: check globals.

def leave_setcomp(self, _: nodes.SetComp): (source)

Leave setcomp: update consumption analysis variable.

def leave_with(self, node: nodes.With): (source)

Undocumented

def visit_arguments(self, node: nodes.Arguments): (source)

Undocumented

@utils.only_required_for_messages('unbalanced-tuple-unpacking', 'unpacking-non-sequence', 'self-cls-assignment', 'unbalanced_dict_unpacking')
def visit_assign(self, node: nodes.Assign): (source)

Check unbalanced tuple unpacking for assignments and unpacking non-sequences as well as in case self/cls get assigned.

def visit_assignname(self, node: nodes.AssignName): (source)

Undocumented

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

Visit class: update consumption analysis variable.

@utils.only_required_for_messages('unused-import', 'unused-variable')
def visit_const(self, node: nodes.Const): (source)

Take note of names that appear inside string literal type annotations unless the string is a parameter to `typing.Literal` or `typing.Annotation`.

def visit_delname(self, node: nodes.DelName): (source)

Undocumented

def visit_dictcomp(self, node: nodes.DictComp): (source)

Visit dictcomp: update consumption analysis variable.

@utils.only_required_for_messages('redefined-outer-name')
def visit_excepthandler(self, node: nodes.ExceptHandler): (source)

Undocumented

@utils.only_required_for_messages('unbalanced-dict-unpacking')
def visit_for(self, node: nodes.For): (source)

Undocumented

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

Visit function: update consumption analysis variable and check locals.

def visit_generatorexp(self, node: nodes.GeneratorExp): (source)

Visit genexpr: update consumption analysis variable.

@utils.only_required_for_messages('global-variable-undefined', 'global-variable-not-assigned', 'global-statement', 'global-at-module-level', 'redefined-builtin')
def visit_global(self, node: nodes.Global): (source)

Check names imported exists in the global scope.

@utils.only_required_for_messages('no-name-in-module')
def visit_import(self, node: nodes.Import): (source)

Check modules attribute accesses.

@utils.only_required_for_messages('no-name-in-module')
def visit_importfrom(self, node: nodes.ImportFrom): (source)

Check modules attribute accesses.

def visit_lambda(self, node: nodes.Lambda): (source)

Visit lambda: update consumption analysis variable.

def visit_listcomp(self, node: nodes.ListComp): (source)

Visit listcomp: update consumption analysis variable.

def visit_module(self, node: nodes.Module): (source)

Visit module : update consumption analysis variable checks globals doesn't overrides builtins.

Don't add the 'utils.only_required_for_messages' decorator here! It's important that all 'Name' nodes are visited, otherwise the 'NamesConsumers' won't be correct.

def visit_setcomp(self, node: nodes.SetComp): (source)

Visit setcomp: update consumption analysis variable.

def visit_subscript(self, node: nodes.Subscript): (source)

Undocumented

Name of the provider.

Options provided by this provider.

@staticmethod
def _comprehension_between_frame_and_node(node: nodes.Name) -> bool: (source)

Return True if a ComprehensionScope intervenes between `node` and its frame.

@staticmethod
def _defined_in_function_definition(node: nodes.NodeNG, frame: nodes.NodeNG) -> bool: (source)

Undocumented

@staticmethod
def _in_lambda_or_comprehension_body(node: nodes.NodeNG, frame: nodes.NodeNG) -> bool: (source)

Return True if node within a lambda/comprehension body (or similar) and thus should not have access to class attributes in frame.

@staticmethod
def _is_first_level_self_reference(node: nodes.Name, defstmt: nodes.ClassDef, found_nodes: list[nodes.NodeNG]) -> tuple[VariableVisitConsumerAction, list[nodes.NodeNG]|None]: (source)

Check if a first level method's annotation or default values refers to its own class, and return a consumer action.

@staticmethod
def _is_never_evaluated(defnode: nodes.NamedExpr, defnode_parent: nodes.IfExp) -> bool: (source)

Check if a NamedExpr is inside a side of if ... else that never gets evaluated.

@staticmethod
def _is_variable_violation(node: nodes.Name, defnode: nodes.NodeNG, stmt: nodes.Statement, defstmt: nodes.Statement, frame: nodes.LocalsDictNodeNG, defframe: nodes.LocalsDictNodeNG, base_scope_type: str, is_recursive_klass: bool) -> tuple[bool, bool, bool]: (source)

Undocumented

@staticmethod
def _maybe_used_and_assigned_at_once(defstmt: nodes.Statement) -> bool: (source)

Check if `defstmt` has the potential to use and assign a name in the same statement.

Return the list of values of the `Assign` node.

def _allowed_redefined_builtin(self, name: str) -> bool: (source)

Undocumented

def _check_all(self, node: nodes.Module, not_consumed: dict[str, list[nodes.NodeNG]]): (source)

Undocumented

def _check_classdef_metaclasses(self, klass: nodes.ClassDef, parent_node: nodes.Module|nodes.FunctionDef) -> list[tuple[dict[str, list[nodes.NodeNG]], str]]: (source)

Undocumented

def _check_consumer(self, node: nodes.Name, stmt: nodes.NodeNG, frame: nodes.LocalsDictNodeNG, current_consumer: NamesConsumer, base_scope_type: str) -> tuple[VariableVisitConsumerAction, list[nodes.NodeNG]|None]: (source)

Checks a consumer for conditions that should trigger messages.

def _check_globals(self, not_consumed: dict[str, nodes.NodeNG]): (source)

Undocumented

def _check_imports(self, not_consumed: dict[str, list[nodes.NodeNG]]): (source)

Undocumented

def _check_is_unused(self, name: str, node: nodes.FunctionDef, stmt: nodes.NodeNG, global_names: set[str], nonlocal_names: Iterable[str], comprehension_target_names: Iterable[str]): (source)

Undocumented

def _check_late_binding_closure(self, node: nodes.Name): (source)

Check whether node is a cell var that is assigned within a containing loop. Special cases where we don't care about the error: 1. When the node's function is immediately called, e.g. (lambda: i)() 2. When the node's function is returned from within the loop, e.g. return lambda: i

def _check_metaclasses(self, node: nodes.Module|nodes.FunctionDef): (source)

Update consumption analysis for metaclasses.

def _check_module_attrs(self, node: _base_nodes.ImportNode, module: nodes.Module, module_names: list[str]) -> nodes.Module|None: (source)

Check that module_names (list of string) are accessible through the given module, if the latest access name corresponds to a module, return it.

def _check_potential_index_error(self, node: nodes.Subscript, inferred_slice: nodes.NodeNG|None): (source)

Check for the potential-index-error message.

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

Check that self/cls don't get assigned.

def _check_unpacking(self, inferred: InferenceResult, node: nodes.Assign, targets: list[nodes.NodeNG]): (source)

Check for unbalanced tuple unpacking and unpacking non sequences.

def _check_unused_arguments(self, name: str, node: nodes.FunctionDef, stmt: nodes.NodeNG, argnames: list[str], nonlocal_names: Iterable[str]): (source)

Undocumented

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

Return True if the node is in a local class scope, as an assignment. Detect if we are in a local class scope, as an assignment. For example, the following is fair game. class A: b = 1 c = lambda b=b: b * b class B: tp = 1 def func(self, arg: tp): ... class C: tp = 2 def func(self, arg=tp): ... class C: class Tp: pass class D(Tp): ...

def _is_name_ignored(self, stmt: nodes.NodeNG, name: str) -> (re.Pattern[str]|re.Match[str])|None: (source)

Undocumented

def _is_only_type_assignment(self, node: nodes.Name, defstmt: nodes.Statement) -> bool: (source)

Check if variable only gets assigned a type and never a value.

def _loopvar_name(self, node: astroid.Name): (source)

Undocumented

def _report_unbalanced_unpacking(self, node: nodes.NodeNG, inferred: InferenceResult, targets: list[nodes.NodeNG], values: list[nodes.NodeNG], details: str): (source)

Undocumented

def _report_unpacking_non_sequence(self, node: nodes.NodeNG, details: str): (source)

Undocumented

def _should_ignore_redefined_builtin(self, stmt: nodes.NodeNG) -> bool: (source)

Undocumented

def _should_node_be_skipped(self, node: nodes.Name, consumer: NamesConsumer, is_start_index: bool) -> bool: (source)

Tests a consumer and node for various conditions in which the node shouldn't be checked for the undefined-variable and used-before-assignment checks.

def _store_type_annotation_names(self, node: (nodes.For|nodes.Assign)|nodes.With): (source)

Undocumented

def _store_type_annotation_node(self, type_annotation: nodes.NodeNG): (source)

Given a type annotation, store all the name nodes it refers to.

def _undefined_and_used_before_checker(self, node: nodes.Name, stmt: nodes.NodeNG): (source)

Undocumented

This is a queue, last in first out.

_postponed_evaluation_enabled = (source)

Undocumented

_to_consume = (source)

Undocumented

_type_annotation_names: list = (source)

Undocumented

@cached_property
_allow_global_unused_variables: bool = (source)

Undocumented

@cached_property
_analyse_fallback_blocks: bool = (source)

Undocumented

Undocumented