def _check_return_path(self, node: ast.Try) -> None: try_has = any(is_contained(line, ast.Return) for line in node.body) except_has = any( is_contained(except_handler, ast.Return) for except_handler in node.handlers) else_has = any(is_contained(line, ast.Return) for line in node.orelse) finally_has = any( is_contained(line, ast.Return) for line in node.finalbody) if finally_has and (try_has or except_has): self.add_violation(TryExceptMultipleReturnPathViolation(node)) if else_has and try_has: self.add_violation(TryExceptMultipleReturnPathViolation(node))
def _find_returing_nodes( node: ast.Try, bad_returning_nodes: AnyNodes, ) -> Tuple[bool, bool, bool, bool]: try_has = any( # TODO: also check ast.Break is_contained(line, bad_returning_nodes) for line in node.body) except_has = any( is_contained(except_handler, bad_returning_nodes) for except_handler in node.handlers) else_has = any( is_contained(line, bad_returning_nodes) for line in node.orelse) finally_has = any( is_contained(line, bad_returning_nodes) for line in node.finalbody) return try_has, except_has, else_has, finally_has
def is_generator(node: AnyFunctionDef) -> bool: """Tells whether a given function is a generator.""" for body_item in node.body: if is_contained(node=body_item, to_check=(Yield, YieldFrom)): return True return False
def _check_lambda_inside_loop(self, node: AnyLoop) -> None: for subnode in node.body: if is_contained(subnode, (ast.Lambda, )): self.add_violation(LambdaInsideLoopViolation(node))
def _check_method_contents(self, node: types.AnyFunctionDef) -> None: if node.name == constants.INIT: if is_contained(node, self._not_appropriate_for_init): self.add_violation(YieldInsideInitViolation(node))