Skip to content

Commit

Permalink
update tests to catch ImportError
Browse files Browse the repository at this point in the history
  • Loading branch information
ewu63 committed Mar 17, 2024
1 parent 352db37 commit 1fc3dfd
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 35 deletions.
20 changes: 11 additions & 9 deletions pyoptsparse/pyOpt_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,16 +575,16 @@ def _broadcast_to_array(name: str, value: ArrayType, n_values: int, allow_none:
raise Error(f"The {name} argument cannot be 'None'.")
return value

def try_import_compiled_module_from_path(module_name: str, path: str) -> types.ModuleType | str:
def try_import_compiled_module_from_path(module_name: str, path: str|None = None) -> types.ModuleType | str:
"""
Attempt to import a module from a given path.
Parameters
----------
module_name : str
The name of the module
path : str
The path to import from
path : str | None
The path to import from. If None, the default ``sys.path`` is used.
Returns
-------
Expand All @@ -594,15 +594,17 @@ def try_import_compiled_module_from_path(module_name: str, path: str) -> types.M
"""
path = os.path.abspath(os.path.expandvars(os.path.expanduser(path)))
orig_path = sys.path
sys.path = [path]
if path is not None:
sys.path = [path]
try:
module = importlib.import_module(module_name)
except ImportError as e:
# warnings.warn(
# f"`{module_name}` module could not be imported from {path}.",
# ImportWarning,
# stacklevel=2,
# )
if path is not None:
warnings.warn(
f"{module_name} module could not be imported from {path}.",
ImportWarning,
stacklevel=2,
)
module = str(e)
finally:
sys.path = orig_path
Expand Down
5 changes: 2 additions & 3 deletions pyoptsparse/pyParOpt/ParOpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ class ParOpt(Optimizer):
def __init__(self, raiseError=True, options={}):
name = "ParOpt"
category = "Local Optimizer"
if _ParOpt is None:
if raiseError:
raise Error("There was an error importing ParOpt")
if _ParOpt is None and raiseError:
raise ImportError("There was an error importing ParOpt")

# Create and fill-in the dictionary of default option values
self.defOpts = {}
Expand Down
5 changes: 3 additions & 2 deletions tests/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import unittest

# First party modules
from pyoptsparse.pyOpt_utils import try_import_compiled_module_from_path # noqa: E402
from pyoptsparse.pyOpt_utils import try_import_compiled_module_from_path


class TestImportSnoptFromPath(unittest.TestCase):
def test_nonexistent_path(self):
with self.assertWarns(ImportWarning):
self.assertIsNone(try_import_compiled_module_from_path("snopt", "/a/nonexistent/path"))
module = try_import_compiled_module_from_path("snopt", "/a/nonexistent/path")
self.assertTrue(isinstance(module, str))

def test_sys_path_unchanged(self):
path = tuple(sys.path)
Expand Down
18 changes: 6 additions & 12 deletions tests/test_snopt_bugfix.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,8 @@ def test_opt(self):
# Optimizer
try:
opt = SNOPT(options=optOptions)
except Error as e:
if "There was an error importing" in e.message:
raise unittest.SkipTest("Optimizer not available: SNOPT")
raise e
except ImportError:
raise unittest.SkipTest("Optimizer not available: SNOPT")

sol = opt(optProb, sens=sens)

Expand Down Expand Up @@ -137,10 +135,8 @@ def test_opt_bug1(self):
# Optimizer
try:
opt = SNOPT(options=optOptions)
except Error as e:
if "There was an error importing" in e.message:
raise unittest.SkipTest("Optimizer not available: SNOPT")
raise e
except ImportError:
raise unittest.SkipTest("Optimizer not available: SNOPT")

opt(optProb, sens=sens)

Expand Down Expand Up @@ -180,10 +176,8 @@ def test_opt_bug_print_2con(self):
# Optimizer
try:
opt = SNOPT(options=optOptions)
except Error as e:
if "There was an error importing" in e.message:
raise unittest.SkipTest("Optimizer not available: SNOPT")
raise e
except ImportError:
raise unittest.SkipTest("Optimizer not available: SNOPT")

sol = opt(optProb, sens=sens)

Expand Down
12 changes: 4 additions & 8 deletions tests/test_user_termination.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ def test_obj(self, optName):

try:
opt = OPT(optName, options=optOptions)
except Error as e:
if "There was an error importing" in e.message:
raise unittest.SkipTest(f"Optimizer not available: {optName}")
raise e
except ImportError:
raise unittest.SkipTest(f"Optimizer not available: {optName}")

sol = opt(optProb, sens=termcomp.sens)

Expand All @@ -128,10 +126,8 @@ def test_sens(self, optName):

try:
opt = OPT(optName, options=optOptions)
except Error as e:
if "There was an error importing" in e.message:
raise unittest.SkipTest("Optimizer not available: SNOPT")
raise e
except ImportError:
raise unittest.SkipTest("Optimizer not available: SNOPT")

sol = opt(optProb, sens=termcomp.sens)

Expand Down
2 changes: 1 addition & 1 deletion tests/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def optimize(self, sens=None, setDV=None, optOptions=None, storeHistory=False, h
try:
opt = OPT(self.optName, options=optOptions)
self.optVersion = opt.version
except Error as e:
except ImportError as e:
if self.optName in DEFAULT_OPTIMIZERS:
raise e
else:
Expand Down

0 comments on commit 1fc3dfd

Please sign in to comment.