Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace generic Exception with more specific exceptions where possible #405

Merged
merged 6 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
- name: Pylint code analysis
run: |
pip install pylint
pylint --disable=all --enable=missing-docstring scico
pylint --disable=all --enable=missing-docstring,broad-exception-raised scico
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,4 @@ repos:
language_version: python3
types: [python]
exclude: ^(scico/test/|examples|docs)
args: ['--score=n', '--disable=all', '--enable=missing-docstring']
args: ['--score=n', '--disable=all', '--enable=missing-docstring,broad-exception-raised']
4 changes: 2 additions & 2 deletions scico/linop/radon_svmbir.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2021-2022 by SCICO Developers
# Copyright (C) 2021-2023 by SCICO Developers
# All rights reserved. BSD 3-clause License.
# This file is part of the SCICO package. Details of the copyright and
# user license can be found in the 'LICENSE' file distributed with the
Expand Down Expand Up @@ -365,7 +365,7 @@ def __init__(
if snp.all(W.diagonal >= 0):
self.W = W
else:
raise Exception(f"The weights, W, must be non-negative.")
raise ValueError(f"The weights, W, must be non-negative.")
else:
raise TypeError(f"Parameter W must be None or a linop.Diagonal, got {type(W)}.")

Expand Down
4 changes: 2 additions & 2 deletions scico/optimize/_admm.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ def __init__(
"""
N = len(g_list)
if len(C_list) != N:
raise Exception(f"len(C_list)={len(C_list)} not equal to len(g_list)={N}.")
raise ValueError(f"len(C_list)={len(C_list)} not equal to len(g_list)={N}.")
if len(rho_list) != N:
raise Exception(f"len(rho_list)={len(rho_list)} not equal to len(g_list)={N}.")
raise ValueError(f"len(rho_list)={len(rho_list)} not equal to len(g_list)={N}.")

self.f: Functional = f
self.g_list: List[Functional] = g_list
Expand Down
2 changes: 1 addition & 1 deletion scico/optimize/_pgm.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(
self.f: Union[Loss, Functional] = f

if g.has_prox is not True:
raise Exception(f"The functional g ({type(g)}) must have a prox method.")
raise ValueError(f"The functional g ({type(g)}) must have a prox method.")

#: Functional to minimize; must have prox defined
self.g: Functional = g
Expand Down
2 changes: 1 addition & 1 deletion scico/test/flax/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def test_except_only_apply(testobj):
testobj.model_conf["depth"], testobj.chn, testobj.model_conf["num_filters"]
)

with pytest.raises(Exception):
with pytest.raises(RuntimeError):
out_ = sflax.only_apply(
testobj.train_conf,
model,
Expand Down
2 changes: 1 addition & 1 deletion scico/test/flax/test_train_aux.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ def test_except_incomplete_stats_obj():
"train_loss": 1.4e-2,
"train_snr": 3,
}
with pytest.raises(Exception):
with pytest.raises(AttributeError):
itstat_object.insert(itstat_insert_func(ArgumentStruct(**summary2)))


Expand Down
2 changes: 1 addition & 1 deletion scico/test/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def check_results(jout, sout):
np.testing.assert_allclose(jout, sout, rtol=1e-4)
else:
# some type of output that isn't being captured?
raise Exception
raise ValueError(f"Unexpected input type {type(jout)} or {type(sout)}.")


def test_reshape_array():
Expand Down
6 changes: 3 additions & 3 deletions scico/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ def test_add_seed_adapter():
# seed defaults to zero
assert fun(jax.random.PRNGKey(0)) == fun_alt()[0]

# other parameters still work...
# other parameters still work ...
key = jax.random.PRNGKey(0)
sz = (10, 3)
dtype = np.float64

# ...positional
# ... positional
np.testing.assert_array_equal(fun(key, sz), fun_alt(sz)[0])
np.testing.assert_array_equal(fun(key, sz, dtype), fun_alt(sz, dtype)[0])
np.testing.assert_array_equal(fun(key, sz, dtype), fun_alt(sz, dtype, key)[0])
Expand All @@ -67,5 +67,5 @@ def test_add_seed_adapter():
np.testing.assert_array_equal(key_a, key_b)

# error when key and seed are specified
with pytest.raises(Exception):
with pytest.raises(ValueError):
_ = fun_alt(key=jax.random.PRNGKey(0), seed=42)[0]