Skip to content

Commit

Permalink
Use raise from e syntax for reraising exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
timj committed Feb 8, 2025
1 parent 965619e commit 53a3af6
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 24 deletions.
12 changes: 7 additions & 5 deletions python/lsst/pex/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -729,13 +729,14 @@ def __get__(self, instance, owner=None, at=None, label="default"):
# try statements are almost free in python if they succeed
try:
return instance._storage[self.name]
except AttributeError:
except AttributeError as e:

Check warning on line 732 in python/lsst/pex/config/config.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/config.py#L732

Added line #L732 was not covered by tests
if not isinstance(instance, Config):
return self
else:
raise AttributeError(
e.add_note(

Check warning on line 736 in python/lsst/pex/config/config.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/config.py#L736

Added line #L736 was not covered by tests
f"Config {instance} is missing _storage attribute, likely incorrectly initialized"
)
raise

Check warning on line 739 in python/lsst/pex/config/config.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/config.py#L739

Added line #L739 was not covered by tests

def __set__(
self, instance: Config, value: FieldTypeVar | None, at: Any = None, label: str = "assignment"
Expand Down Expand Up @@ -790,7 +791,7 @@ def __set__(
try:
self._validateValue(value)
except BaseException as e:
raise FieldValidationError(self, instance, str(e))
raise FieldValidationError(self, instance, str(e)) from e

instance._storage[self.name] = value
if at is None:
Expand Down Expand Up @@ -1135,8 +1136,9 @@ def update(self, **kw):
try:
field = self._fields[name]
field.__set__(self, value, at=at, label=label)
except KeyError:
raise KeyError(f"No field of name {name} exists in config type {_typeStr(self)}")
except KeyError as e:
e.add_note(f"No field of name {name} exists in config type {_typeStr(self)}")
raise

Check warning on line 1141 in python/lsst/pex/config/config.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/config.py#L1139-L1141

Added lines #L1139 - L1141 were not covered by tests

def load(self, filename, root="config"):
"""Modify this config in place by executing the Python code in a
Expand Down
12 changes: 6 additions & 6 deletions python/lsst/pex/config/configChoiceField.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ def __init__(self, dict_, value, at=None, label="assignment", setHistory=True):
if v not in self._dict:
# invoke __getitem__ to ensure it's present
self._dict.__getitem__(v, at=at)
except TypeError:
except TypeError as e:
msg = f"Value {value} is of incorrect type {_typeStr(value)}. Sequence type expected"
raise FieldValidationError(self._field, self._config, msg)
raise FieldValidationError(self._field, self._config, msg) from e

Check warning on line 81 in python/lsst/pex/config/configChoiceField.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/configChoiceField.py#L81

Added line #L81 was not covered by tests
self._set = set(value)
else:
self._set = set()
Expand Down Expand Up @@ -293,10 +293,10 @@ def __getitem__(self, k, at=None, label="default"):
except KeyError:
try:
dtype = self.types[k]
except Exception:
except Exception as e:
raise FieldValidationError(
self._field, self._config, f"Unknown key {k!r} in Registry/ConfigChoiceField"
)
) from e
name = _joinNamePath(self._config._name, self._field.name, k)
if at is None:
at = getCallStack()
Expand All @@ -310,8 +310,8 @@ def __setitem__(self, k, value, at=None, label="assignment"):

try:
dtype = self.types[k]
except Exception:
raise FieldValidationError(self._field, self._config, f"Unknown key {k!r}")
except Exception as e:
raise FieldValidationError(self._field, self._config, f"Unknown key {k!r}") from e

Check warning on line 314 in python/lsst/pex/config/configChoiceField.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/configChoiceField.py#L313-L314

Added lines #L313 - L314 were not covered by tests

if value != dtype and type(value) is not dtype:
msg = (
Expand Down
6 changes: 3 additions & 3 deletions python/lsst/pex/config/configurableField.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def retarget(self, target, ConfigClass=None, at=None, label="retarget"):
try:
ConfigClass = self._field.validateTarget(target, ConfigClass)
except BaseException as e:
raise FieldValidationError(self._field, self._config, e.message)
raise FieldValidationError(self._field, self._config, e.message) from e

Check warning on line 169 in python/lsst/pex/config/configurableField.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/configurableField.py#L169

Added line #L169 was not covered by tests

if at is None:
at = getCallStack()
Expand Down Expand Up @@ -304,8 +304,8 @@ def validateTarget(self, target, ConfigClass):
if ConfigClass is None:
try:
ConfigClass = target.ConfigClass
except Exception:
raise AttributeError("'target' must define attribute 'ConfigClass'")
except Exception as e:
raise AttributeError("'target' must define attribute 'ConfigClass'") from e
if not issubclass(ConfigClass, Config):
raise TypeError(
f"'ConfigClass' is of incorrect type {_typeStr(ConfigClass)}. "
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/pex/config/dictField.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ def __init__(self, config, field, value, at, label, setHistory=True):
for k in value:
# do not set history per-item
self.__setitem__(k, value[k], at=at, label=label, setHistory=False)
except TypeError:
except TypeError as e:
msg = f"Value {value} is of incorrect type {_typeStr(value)}. Mapping type expected."
raise FieldValidationError(self._field, self._config, msg)
raise FieldValidationError(self._field, self._config, msg) from e
if setHistory:
self._history.append((dict(self._dict), at, label))

Expand Down
8 changes: 4 additions & 4 deletions python/lsst/pex/config/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ class Color:
def __init__(self, text, category):
try:
color = Color.categories[category]
except KeyError:
raise RuntimeError(f"Unknown category: {category}")
except KeyError as e:
raise RuntimeError(f"Unknown category: {category}") from e

Check warning on line 100 in python/lsst/pex/config/history.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/history.py#L99-L100

Added lines #L99 - L100 were not covered by tests

self.rawText = str(text)
x = color.lower().split(";")
Expand All @@ -109,8 +109,8 @@ def __init__(self, text, category):

try:
self._code = "%s" % (30 + Color.colors[self.color])
except KeyError:
raise RuntimeError(f"Unknown colour: {self.color}")
except KeyError as e:
raise RuntimeError(f"Unknown colour: {self.color}") from e

Check warning on line 113 in python/lsst/pex/config/history.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/history.py#L112-L113

Added lines #L112 - L113 were not covered by tests

if bold:
self._code += ";1"
Expand Down
4 changes: 2 additions & 2 deletions python/lsst/pex/config/listField.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def __init__(self, config, field, value, at, label, setHistory=True):
try:
for i, x in enumerate(value):
self.insert(i, x, setHistory=False)
except TypeError:
except TypeError as e:
msg = f"Value {value} is of incorrect type {_typeStr(value)}. Sequence type expected"
raise FieldValidationError(self._field, config, msg)
raise FieldValidationError(self._field, config, msg) from e
if setHistory:
self.history.append((list(self._list), at, label))

Expand Down
4 changes: 2 additions & 2 deletions python/lsst/pex/config/wrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ def makeConfigClass(ctrl, name=None, base=Config, doc=None, module=None, cls=Non
nestedModuleObj = importlib.import_module(nestedModuleName)
try:
dtype = getattr(nestedModuleObj, ctype).ConfigClass
except AttributeError:
raise AttributeError(f"'{moduleName}.{ctype}.ConfigClass' does not exist")
except AttributeError as e:
raise AttributeError(f"'{moduleName}.{ctype}.ConfigClass' does not exist") from e

Check warning on line 205 in python/lsst/pex/config/wrap.py

View check run for this annotation

Codecov / codecov/patch

python/lsst/pex/config/wrap.py#L204-L205

Added lines #L204 - L205 were not covered by tests
fields[k] = ConfigField(doc=doc, dtype=dtype)
else:
try:
Expand Down

0 comments on commit 53a3af6

Please sign in to comment.