def _check_negated_conditions(self, node: AnyIf) -> None: if isinstance(node, ast.If) and not ifs.has_else(node): return if isinstance(node.test, ast.UnaryOp): if isinstance(node.test.op, ast.Not): self.add_violation(NegatedConditionsViolation(node)) elif isinstance(node.test, ast.Compare): if any(isinstance(elem, ast.NotEq) for elem in node.test.ops): self.add_violation(NegatedConditionsViolation(node))
def _check_simplifiable_returning_if(self, node: ast.If) -> None: body = node.body simple_if_and_root = not (ifs.has_elif(node) or ifs.is_elif(node)) if keywords.is_simple_return(body) and simple_if_and_root: if ifs.has_else(node): else_body = node.orelse if keywords.is_simple_return(else_body): self.add_violation(SimplifiableReturningIfViolation(node)) return self._check_simplifiable_returning_parent(node)
def _check_simplifiable_returning_if(self, node: ast.If) -> None: body = node.body simple_if_and_root = not (ifs.has_elif(node) or ifs.is_elif(node)) if keywords.is_simple_return(body) and simple_if_and_root: if ifs.has_else(node): else_body = node.orelse if keywords.is_simple_return(else_body): self.add_violation(SimplifiableReturningIfViolation(node)) return body = getattr(node, 'wps_parent').body next_index_in_parent = body.index(node) + 1 if keywords.next_node_returns_bool(body, next_index_in_parent): self.add_violation(SimplifiableReturningIfViolation(node))