Enable B904 check of flake8 (#165047)

The description of `B904` is `Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling. `

Pull Request resolved: https://github.com/pytorch/pytorch/pull/165047
Approved by: https://github.com/Lucaskabela
This commit is contained in:
Yuanyuan Chen
2025-10-10 03:08:01 +00:00
committed by PyTorch MergeBot
parent ae25dd51fc
commit ce6b589545
3 changed files with 6 additions and 8 deletions

View File

@ -12,7 +12,7 @@ ignore =
# to line this up with executable bit
EXE001,
# these ignores are from flake8-bugbear; please fix!
B007,B008,B017,B019,B023,B028,B903,B904,B905,B906,B907,B908,B910
B007,B008,B017,B019,B023,B028,B903,B905,B906,B907,B908,B910
# these ignores are from flake8-comprehensions; please fix!
C407,
# these ignores are from flake8-logging-format; please fix!

View File

@ -127,7 +127,7 @@ class ExceptionTests(torch._dynamo.test_case.TestCase):
x = torch.sigmoid(x)
try:
x = torch.cos(x)
raise AssertionError
raise AssertionError # noqa: B904
except AssertionError:
x = torch.cos(x)
@ -631,7 +631,7 @@ class ExceptionTests(torch._dynamo.test_case.TestCase):
raise ZeroDivisionError
except ZeroDivisionError:
try:
raise ValueError
raise ValueError # noqa: B904
except ValueError:
pass
raise
@ -681,7 +681,7 @@ class ExceptionTests(torch._dynamo.test_case.TestCase):
yield 1
except ValueError:
try:
raise TypeError
raise TypeError # noqa: B904
finally:
pass
@ -711,7 +711,7 @@ class ExceptionTests(torch._dynamo.test_case.TestCase):
raise ValueError
except ValueError:
try:
raise TypeError
raise TypeError # noqa: B904
finally:
pass

View File

@ -579,9 +579,7 @@ def _create_if_dir_not_exist(path_dir: str) -> None:
Path(path_dir).mkdir(parents=True, exist_ok=True)
except OSError as exc: # Guard against race condition
if exc.errno != errno.EEXIST:
raise RuntimeError( # noqa: TRY200 (Use `raise from`)
f"Fail to create path {path_dir}"
)
raise RuntimeError(f"Fail to create path {path_dir}") from exc
def _remove_dir(path_dir: str) -> None: