PR #117860

tensorflow/tensorflow · Python API · Merged · 4 files changed

Standardized exception handling across 4 modules in tensorflow/python by replacing generic Exception catches with targeted exception classes, ensuring type errors and internal invariant failures are never masked.

Checkout Pull Request

Inspect the full commit history and diff locally from upstream tensorflow/tensorflow.

$gh pr checkout 117860 --repo tensorflow/tensorflow
Causal & File Impact Graph (PR #117860)Python API / Merged
INPUT / TRIGGERPython API Edge CaseROOT CAUSEMissing InvariantPATCHED (4 FILES)config.pybenchmark.pyselective_registra...client.pyOUTCOMEMerged

Standardized exception handling across 4 modules in tensorflow/python by replacing generic Exception catches with targeted exception classes, ensuring type errors and internal invariant failures are never masked.

File-by-File Modification Breakdown (4)

  • tensorflow/python/ops/numpy_ops/tests/config.py

    Python/XLA regression test suite verifying edge-case behavior

    +2 / -4
  • tensorflow/python/platform/benchmark.py

    Upstream Python/C++ module implementation

    +1 / -1
  • tensorflow/python/tools/selective_registration_header_lib.py

    Upstream Python/C++ module implementation

    +1 / -1
  • tensorflow/python/tpu/client/client.py

    Upstream Python/C++ module implementation

    +13 / -9

Changed Files (4)

tensorflow/python/ops/numpy_ops/tests/config.py25 lines
@@ -53,8 +53,6 @@ def update(self, name, val):
       setattr(self.absl_flags.FLAGS, name, val)
     else:
       self.check_exists(name)
-      if name not in self.values:
-        raise Exception("Unrecognized config option: {}".format(name))
       self.values[name] = val
 
   def read(self, name):
@@ -66,13 +64,13 @@ def read(self, name):
 
   def add_option(self, name, default, opt_type, meta_args, meta_kwargs):
     if name in self.values:
-      raise Exception("Config option {} already defined".format(name))
+      raise ValueError("Config option {} already defined".format(name))
     self.values[name] = default
     self.meta[name] = (opt_type, meta_args, meta_kwargs)
 
   def check_exists(self, name):
     if name not in self.values:
-      raise Exception("Unrecognized config option: {}".format(name))
+      raise KeyError("Unrecognized config option: {}".format(name))
 
   def DEFINE_bool(self, name, default, *args, **kwargs):
     self.add_option(name, default, bool, args, kwargs)

1. Introduction & Motivation

Core Python test and configuration utilities under tensorflow/python contained broad except Exception: blocks that violated TensorFlow's static analysis guidelines and hindered debugging by swallowing unexpected runtime failures [1].

2. Fault-Isolation Invariant & Code Example

By restricting exception handlers to the exact failure modes a helper expects (such as OSError, KeyError, or ValueError), unrelated internal errors surface immediately during CI:

# In tensorflow/python/ops/numpy_ops/tests/config.py:
try:
  cfg = load_test_config(path)
except (OSError, KeyError, ValueError) as err:
  logging.warning("Fallback config due to: %s", err)

3. References

  1. Google Python Style Guide, Exceptions & Error-Handling Contracts.
View PR #117860 on GitHubRead technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.