def _check_wrong_body_nodes(self, node: ast.ClassDef) -> None: for sub_node in node.body: if isinstance(sub_node, self._allowed_body_nodes): continue if strings.is_doc_string(sub_node): continue self.add_violation(oop.WrongClassBodyContentViolation(sub_node))
def _get_call_stmt_of_useless_method( self, node: types.AnyFunctionDef, ) -> Optional[ast.Call]: """ Fetches ``super`` call statement from function definition. Consider next body as possible candidate of useless method: 1. Optional[docstring] 2. single return statement with call 3. single statement with call, but without return Related: https://github.com/wemake-services/wemake-python-styleguide/issues/1168 """ statements_number = len(node.body) if statements_number > 2 or statements_number == 0: return None if statements_number == 2: if not strings.is_doc_string(node.body[0]): return None stmt = node.body[-1] if isinstance(stmt, ast.Return): call_stmt = stmt.value return call_stmt if isinstance(call_stmt, ast.Call) else None elif isinstance(stmt, ast.Expr) and isinstance(stmt.value, ast.Call): return stmt.value return None
def _check_modulo_patterns( self, node: AnyText, text_data: Optional[str], ) -> None: parent = nodes.get_parent(node) if parent and strings.is_doc_string(parent): return # we allow `%s` in docstrings: they cannot be formatted. if self._modulo_string_pattern.search(text_data): self.add_violation(consistency.ModuloStringFormatViolation(node))
def _check_init_contents(self, node: ast.Module) -> None: if not self._is_init() or not node.body: return if not self.options.i_control_code: return if len(node.body) > 1: self.add_violation(InitModuleHasLogicViolation()) return if not is_doc_string(node.body[0]): self.add_violation(InitModuleHasLogicViolation())
def _check_expression( self, node: ast.Expr, is_first: bool = False, ) -> None: if isinstance(node.value, self._have_effect): return if is_first and strings.is_doc_string(node): if isinstance(nodes.get_parent(node), self._have_doc_strings): return self.add_violation(StatementHasNoEffectViolation(node))
def _get_call_stmt_of_useless_method( self, node: types.AnyFunctionDef, ) -> Optional[ast.Call]: # consider next body as possible candidate of useless method: # 1) Optional[docstring] # 2) return statement with call statements_number = len(node.body) if statements_number > 2 or statements_number == 0: return None if statements_number == 2: if not strings.is_doc_string(node.body[0]): return None return_stmt = node.body[-1] if not isinstance(return_stmt, ast.Return): return None call_stmt = return_stmt.value if not isinstance(call_stmt, ast.Call): return None return call_stmt