def _check_useless_compare(self, node: ast.Compare) -> None: last_variable = get_assigned_expr(node.left) for next_variable in map(get_assigned_expr, node.comparators): if is_same_variable(last_variable, next_variable): self.add_violation(UselessCompareViolation(node)) break last_variable = next_variable
def _check_condition(self, node: ast.AST, cond: ast.AST) -> None: if isinstance(cond, ast.NameConstant) and cond.value is True: if isinstance(node, ast.While): return # We should allow plain `while True:` real_node = operators.unwrap_unary_node(walrus.get_assigned_expr(cond)) if isinstance(real_node, self._forbidden_nodes): self.add_violation(WrongKeywordConditionViolation(cond))
def _check_literal_compare(self, node: ast.Compare) -> None: last_was_literal = nodes.is_literal(get_assigned_expr(node.left)) for comparator in map(get_assigned_expr, node.comparators): next_is_literal = nodes.is_literal(comparator) if last_was_literal and next_is_literal: self.add_violation(ConstantCompareViolation(node)) break last_was_literal = next_is_literal
def _check_constant_condition(self, node: ast.AST) -> None: if isinstance(node, ast.BoolOp): for condition in node.values: self._check_constant_condition(condition) else: real_node = operators.unwrap_unary_node(get_assigned_expr(node)) if isinstance(real_node, self._forbidden_nodes): self.add_violation(ConstantConditionViolation(node))
def _check_comparators(self, node: ast.Compare) -> None: for op, comp in zip(node.ops, node.comparators): if not isinstance(op, self._in_nodes): continue real = get_assigned_expr(comp) self._check_single_item_container(real) self._check_wrong_comparators(real)
def _check_is_constant_compare( self, op: ast.cmpop, comparator: ast.expr, ) -> None: if not isinstance(op, (ast.Is, ast.IsNot)): return unwrapped = operators.unwrap_unary_node( get_assigned_expr(comparator), ) if isinstance(unwrapped, self._forbidden_for_is): self.add_violation(WrongIsCompareViolation(comparator))
def _check_ordering(self, node: ast.Compare) -> None: if self._is_left_node_valid(get_assigned_expr(node.left)): return if self._is_special_case(node): return if len(node.comparators) > 1: return if not self._has_wrong_nodes_on_the_right(node.comparators): return self.add_violation(CompareOrderViolation(node))
def _check_constant(self, op: ast.cmpop, comparator: ast.expr) -> None: if not isinstance(op, (ast.Eq, ast.NotEq, ast.Is, ast.IsNot)): return real = get_assigned_expr(comparator) if not isinstance(real, (ast.List, ast.Dict, ast.Tuple)): return length = len(real.keys) if isinstance( real, ast.Dict, ) else len(real.elts) if not length: self.add_violation(FalsyConstantCompareViolation(comparator))
def _check_constant_condition(self, node: AnyIf) -> None: real_node = operators.unwrap_unary_node(get_assigned_expr(node.test), ) if isinstance(real_node, self._forbidden_nodes): self.add_violation(ConstantConditionViolation(node))
def _check_wrong_comparators(self, node: ast.AST) -> None: node = get_assigned_expr(node) if isinstance(node, self._wrong_in_comparators): self.add_violation(WrongInCompareTypeViolation(node))