def visit_any_assign(self, node: AnyAssign) -> None: """Used to check assignment variable to itself.""" names = list(name_nodes.flat_variable_names([node])) self._check_reassignment(node, names) self._check_unique_assignment(node, names) self.generic_visit(node)
def visit_locals(self, node: Union[AnyAssignWithWalrus, ast.arg]) -> None: """Visits local variable definitions and function arguments.""" if isinstance(node, ast.arg): names = {node.arg} else: names = set(flat_variable_names([node])) self._scope(node, names, is_local=True) self._outer_scope(node, names) self.generic_visit(node)
def _check_consistent_variable_return(self, node: ast.Return) -> None: if not node.value or not self._is_named_return(node): return previous_node = self._get_previous_stmt(node) if not isinstance(previous_node, AssignNodes): return return_names = name_nodes.get_variables_from_node(node.value) previous_names = list(name_nodes.flat_variable_names([previous_node])) self._check_for_violations(node, return_names, previous_names)
def _check_attributes_shadowing(self, node: ast.ClassDef) -> None: class_attributes, instance_attributes = self._get_attributes(node) class_attribute_names = set( name_nodes.flat_variable_names(class_attributes), ) for instance_attr in instance_attributes: if instance_attr.attr in class_attribute_names: self.add_violation( oop.ShadowedClassAttributeViolation( instance_attr, text=instance_attr.attr, ), )
def visit_any_assign(self, node: AnyAssign) -> None: """ Used to check assignment variable to itself. Raises: ReassigningVariableToItselfViolation """ names = list(name_nodes.flat_variable_names([node])) # TODO: support NamedExpr self._check_reassignment(node, names) self._check_unique_assignment(node, names) self.generic_visit(node)
def visit_locals(self, node: Union[AnyAssign, ast.arg]) -> None: """ Visits local variable definitions and function arguments. Raises: BlockAndLocalOverlapViolation """ if isinstance(node, ast.arg): names = {node.arg} else: names = set(flat_variable_names([node])) self._scope(node, names, is_local=True) self.generic_visit(node)
def visit_any_assign(self, node: AnyAssignWithWalrus) -> None: """ Checks that we cannot assign explicit unused variables. We do not check assigns inside modules and classes, since there ``_`` prefixed variable means that it is protected, not unused. """ is_inside_class_or_module = isinstance( nodes.get_context(node), (ast.ClassDef, ast.Module), ) self._check_assign_unused( node, name_nodes.flat_variable_names([node]), is_local=not is_inside_class_or_module, ) self.generic_visit(node)
def _almost_swapped(self, assigns: Sequence[ast.Assign]) -> None: previous_var: Set[Optional[str]] = set() for assign in assigns: current_var = { first(name_nodes.flat_variable_names([assign])), first(name_nodes.get_variables_from_node(assign.value)), } if not all(map(bool, current_var)): previous_var.clear() continue if current_var == previous_var: self.add_violation(AlmostSwappedViolation(assign)) if len(previous_var & current_var) == 1: current_var ^= previous_var previous_var = current_var
def find_attributed_getters_and_setters( node: ast.ClassDef, ) -> Iterable[AnyFunctionDef]: """Returns nodes of attributed getter or setter methods.""" class_attributes, instance_attributes = classes.get_attributes( node, include_annotated=True, ) flat_class_attributes = name_nodes.flat_variable_names(class_attributes) attributes_stripped = { class_attribute.lstrip(UNUSED_PLACEHOLDER) for class_attribute in flat_class_attributes }.union({ instance.attr.lstrip(UNUSED_PLACEHOLDER) for instance in instance_attributes }) for method in _find_getters_and_setters(node): if method.name[GETTER_LENGTH:] in attributes_stripped: yield method
def _check_getters_setters_methods(self, node: ast.ClassDef) -> None: class_attributes, instance_attributes = classes.get_attributes( node, include_annotated=True, ) flat_class_attributes = name_nodes.flat_variable_names(class_attributes) attributes_stripped = { class_attribute.lstrip(constants.UNUSED_PLACEHOLDER) for class_attribute in flat_class_attributes }.union({ instance.attr.lstrip(constants.UNUSED_PLACEHOLDER) for instance in instance_attributes }) for method in classes.find_getters_and_setters(node): if method.name[classes.GETTER_LENGTH:] in attributes_stripped: self.add_violation( oop.UnpythonicGetterSetterViolation( method, text=method.name, ), )