예제 #1
0
    def _check_duplicate_exceptions(self, node: ast.Try) -> None:
        exceptions_list = exceptions.get_all_exception_names(node)

        for exc_name, count in Counter(exceptions_list).items():
            if count > 1:
                self.add_violation(
                    DuplicateExceptionViolation(node, text=exc_name), )
예제 #2
0
    def _check_duplicate_exceptions(self, node: ast.Try) -> None:
        exceptions: List[str] = []
        for exc_handler in node.handlers:
            # There might be complex things hidden inside an exception type,
            # so we want to get the string representation of it:
            if isinstance(exc_handler.type, ast.Name):
                exceptions.append(astor.to_source(exc_handler.type).strip())
            elif isinstance(exc_handler.type, ast.Tuple):
                exceptions.extend([
                    astor.to_source(node).strip()
                    for node in exc_handler.type.elts
                ])

        for exc_name, count in Counter(exceptions).items():
            if count > 1:
                self.add_violation(
                    DuplicateExceptionViolation(node, text=exc_name), )