def error_of_type( handler: astroid.ExceptHandler, error_type: Union[str, Type[Exception], Tuple[Type[Exception]]]) -> bool: """ Check if the given exception handler catches the given error_type. The *handler* parameter is a node, representing an ExceptHandler node. The *error_type* can be an exception, such as AttributeError, the name of an exception, or it can be a tuple of errors. The function will return True if the handler catches any of the given errors. """ def stringify_error(error): if not isinstance(error, str): return error.__name__ return error if not isinstance(error_type, tuple): error_type = (error_type, ) # type: ignore expected_errors = {stringify_error(error) for error in error_type} # type: ignore if not handler.type: return True return handler.catch(expected_errors)
def error_of_type(handler: astroid.ExceptHandler, error_type) -> bool: """ Check if the given exception handler catches the given error_type. The *handler* parameter is a node, representing an ExceptHandler node. The *error_type* can be an exception, such as AttributeError, the name of an exception, or it can be a tuple of errors. The function will return True if the handler catches any of the given errors. """ def stringify_error(error): if not isinstance(error, str): return error.__name__ return error if not isinstance(error_type, tuple): error_type = (error_type,) # type: ignore expected_errors = {stringify_error(error) for error in error_type} # type: ignore if not handler.type: return True return handler.catch(expected_errors)