def _check_wrong_function_called(self, node: ast.Call) -> None: function_name = functions.given_function_called( node, FUNCTIONS_BLACKLIST, ) if function_name: self.add_violation( WrongFunctionCallViolation(node, text=function_name), )
def _check_isinstance_call(self, node: ast.Call) -> None: function_name = functions.given_function_called(node, {'isinstance'}) if not function_name or len(node.args) != 2: return if isinstance(node.args[1], ast.Tuple): if len(node.args[1].elts) == 1: self.add_violation(WrongIsinstanceWithTupleViolation(node))
def _check_unpythonic_compare(self, node: ast.Compare) -> None: all_nodes = [node.left, *node.comparators] for index, compare in enumerate(all_nodes): if not isinstance(compare, ast.Call): continue if functions.given_function_called(compare, {'len'}): ps = index - len(all_nodes) + 1 if not _is_correct_len(node.ops[ps], node.comparators[ps]): self.add_violation(UselessLenCompareViolation(node))
def _check_open_call_context(self, node: ast.Call) -> None: function_name = functions.given_function_called(node, {'open'}) if not function_name: return if isinstance(nodes.get_parent(node), ast.withitem): # We do not care about `with` or `async with` - both are fine. return self.add_violation(OpenWithoutContextManagerViolation(node))
def _duplicated_isinstance_call(node: ast.BoolOp) -> List[str]: counter: DefaultDict[str, int] = defaultdict(int) for call in node.values: if not isinstance(call, ast.Call) or len(call.args) != 2: continue if not given_function_called(call, {'isinstance'}): continue isinstance_object = source.node_to_string(call.args[0]) counter[isinstance_object] += 1 return [node_name for node_name, count in counter.items() if count > 1]
def _check_super_call(self, node: ast.Call) -> None: function_name = functions.given_function_called(node, {'super'}) if function_name: self._ensure_super_context(node) self._ensure_super_arguments(node)
def _check_useless_len(self, node: AnyIf) -> None: if isinstance(node.test, ast.Call): if given_function_called(node.test, {'len'}): self.add_violation(UselessLenCompareViolation(node))
def _is_wrong_len(self, node: ast.BinOp, element: str) -> bool: return ( isinstance(node.left, ast.Call) and bool(functions.given_function_called(node.left, {'len'})) and source.node_to_string(node.left.args[0]) == element )
def _check_unnecessary_literals(self, node: ast.Call) -> None: function_name = functions.given_function_called( node, LITERALS_BLACKLIST, ) if function_name and not node.args: self.add_violation(consistency.UnnecessaryLiteralsViolation(node))