10000 [BE]: Enable RUFF TRY400 rule - log.exception (#153473) · pytorch/pytorch@4f4ecc5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4f4ecc5

Browse files
Skylion007pytorchmergebot
authored andcommitted
[BE]: Enable RUFF TRY400 rule - log.exception (#153473)
Change logging.error to logging.exception to log additional information when relevant. A few places have slipped in logging.errors in try except since I last did a clean up here and the rule is stabilized so I am enabling it codebase wide. I have NOQA'd much of our custom exception stack trace handling for RPC calls and distributed and tried to a fix a few errors based on whether we immediately reraised it or if we didn't print any exception handling where it could be useful. Pull Request resolved: #153473 Approved by: https://github.com/albanD, https://github.com/cyyever
1 parent 7482eb2 commit 4f4ecc5

File tree

23 files changed

+51
-46
lines changed

23 files changed

+51
-46
lines changed

.flake8

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ ignore =
1616
# these ignores are from flake8-comprehensions; please fix!
1717
C407,
1818
# these ignores are from flake8-logging-format; please fix!
19-
G100,G101,G200
19+
G100,G101,G200,
20+
# G201 replaced by LOG400 in ruff
21+
G201,
2022
# these ignores are from flake8-simplify. please fix or ignore with commented reason
2123
SIM105,SIM108,SIM110,SIM111,SIM113,SIM114,SIM115,SIM116,SIM117,SIM118,SIM119,SIM12,
2224
# SIM104 is already covered by pyupgrade ruff

.github/scripts/runner_determinator.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,9 @@ def main() -> None:
623623
is_canary,
624624
)
625625

626-
except Exception as e:
627-
log.error(
628-
f"Failed to get issue. Defaulting to Meta runners and no experiments. Exception: {e}"
626+
except Exception:
627+
log.exception(
628+
"Failed to get issue. Defaulting to Meta runners and no experiments."
629629
)
630630

631631
set_github_output(GH_OUTPUT_KEY_LABEL_TYPE, runner_label_prefix)

benchmarks/dynamo/common.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,8 +1703,8 @@ def maybe_snapshot_memory(should_snapshot_memory, suffix):
17031703
f"{output_filename.rstrip('.csv')}_{suffix}.pickle",
17041704
)
17051705
)
1706-
except Exception as e:
1707-
log.error("Failed to save memory snapshot, %s", e)
1706+
except Exception:
1707+
log.exception("Failed to save memory snapshot")
17081708

17091709
torch.cuda.memory._record_memory_history(enabled=None)
17101710

@@ -2745,7 +2745,7 @@ def minify_model(
27452745
try:
27462746
shutil.move("repro.py", f"{repro_dir}/{name}_repro.py")
27472747
except OSError:
2748-
log.error("Could not find repro script for model %s", name)
2748+
log.exception("Could not find repro script for model %s", name)
27492749
else:
27502750
log.info(
27512751
"Repro script for model %s with minified graph saved to %s",

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ select = [
197197
"TC",
198198
"TRY002", # ban vanilla raise (todo fix NOQAs)
199199
"TRY203",
200+
"TRY400", # use logging.exception
200201
"TRY401", # verbose-log-message
201202
"UP",
202203
"YTT",

tools/packaging/split_wheel.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,15 @@ def requirements_installed() -> bool:
4747

4848
return True
4949
except ImportError:
50-
logger.error(
51-
"Requirements not installed, run the following command to install:"
50+
logger.error( # noqa: TRY400
51+
"Requirements not installed, run the following command to install:",
52+
exc_info=False,
5253
)
53-
logger.error(
54-
" > %s -m pip install -r %s/requirements.txt", sys.executable, ROOT_PATH
54+
logger.error( # noqa: TRY400
55+
" > %s -m pip install -r %s/requirements.txt",
56+
sys.executable,
57+
ROOT_PATH,
58+
exc_info=False,
5559
)
5660
return False
5761

torch/_dynamo/repro/after_aot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def debug_wrapper(
138138
example_inputs,
139139
compiler_name,
140140
)
141-
log.error("CompilerError")
141+
log.exception("CompilerError")
142142
raise
143143

144144
# We may run regular PyTorch compute that may trigger Dynamo, do NOT

torch/_dynamo/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2148,7 +2148,7 @@ def torchscript(model, example_inputs, verbose=False):
21482148
if verbose:
21492149
log.exception("jit error")
21502150
else:
2151-
log.error("Both torch.jit.trace and torch.jit.script failed")
2151+
log.error("Both torch.jit.trace and torch.jit.script failed") # noqa: TRY400
21522152
return None
21532153

21542154

torch/_guards.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ def create(self, builder: GuardBuilderBase):
359359
except Exception:
360360
log.exception("Error while creating guard:\n%s", str(self).rstrip())
361361
if self.stack:
362-
log.error("Created at:\n%s", "".join(self.stack.format()[-4:]).rstrip())
362+
log.error("Created at:\n%s", "".join(self.stack.format()[-4:]).rstrip()) # noqa: TRY400
363363
raise
364364

365365
def is_specialized_nn_module(self):

torch/_inductor/codegen/cuda/cuda_env.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def get_cuda_arch() -> Optional[str]:
2222
major, minor = torch.cuda.get_device_capability(0)
2323
return str(major * 10 + minor)
2424
return str(cuda_arch)
25-
except Exception as e:
26-
log.error("Error getting cuda arch: %s", e)
25+
except Exception:
26+
log.exception("Error getting cuda arch")
2727
return None
2828

2929

@@ -35,8 +35,8 @@ def get_cuda_version() -> Optional[str]:
3535
if cuda_version is None:
3636
cuda_version = torch.version.cuda
3737
return cuda_version
38-
except Exception as e:
39-
log.error("Error getting cuda version: %s", e)
38+
except Exception:
39+
log.exception("Error getting cuda version")
4040
return None
4141

4242

torch/_inductor/compile_fx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def _fx_compile_mode_default() -> tuple[FxCompileMode, bool]:
181181
import logging
182182

183183
log = logging.getLogger(__name__)
184-
log.error(
184+
log.error( # noqa: TRY400
185185
"Invalid value of %s for %s. Expected one of %s. Using default.",
186186
value,
187187
name,

0 commit comments

Comments
 (0)
0