[BE]: TRY002 - Ban raising vanilla exceptions (#124570)

Adds a ruff lint rule to ban raising raw exceptions. Most of these should at the very least be runtime exception, value errors, type errors or some other errors. There are hundreds of instance of these bad exception types already in the codebase, so I have noqa'd most of them. Hopefully this error code will get commiters to rethink what exception type they should raise when they submit a PR.

I also encourage people to gradually go and fix all the existing noqas that have been added so they can be removed overtime and our exception typing can be improved.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/124570
Approved by: https://github.com/ezyang
This commit is contained in:
Aaron Gokaslan
2024-04-21 22:26:40 +00:00
committed by PyTorch MergeBot
parent fd90991790
commit c5fafe9f48
82 changed files with 263 additions and 210 deletions

View File

@ -981,7 +981,7 @@ def _get_overloaded_methods(method, mod_class):
mod_class_fileno = get_source_lines_and_file(mod_class)[1]
mod_end_fileno = mod_class_fileno + len(get_source_lines_and_file(mod_class)[0])
if not (method_line_no >= mod_class_fileno and method_line_no <= mod_end_fileno):
raise Exception(
raise Exception( # noqa: TRY002
"Overloads are not useable when a module is redeclared within the same file: "
+ str(method)
)