Skip to content

Commit

Permalink
Expanded import_or_raise to catch all exceptions (#759)
Browse files Browse the repository at this point in the history
* Added support for exceptions in `import_or_raise`

* Updated changelog

* Updated test to use mocking + fixed typo

* Fixed lint errors
  • Loading branch information
christopherbunn committed May 15, 2020
1 parent 6d6af3b commit 4c0a24f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/source/changelog.rst
Expand Up @@ -13,6 +13,7 @@ Changelog
* Add util methods to graph ROC and confusion matrix :pr:`720`
* Refactor `AutoBase` to `AutoSearchBase` :pr:`758`
* Refactor most of `AutoSearchBase._do_iteration` impl into `AutoSearchBase._evaluate` :pr:`762`
* Expanded `import_or_raise` to catch all exceptions :pr:`759`
* Documentation Changes
* Add instructions to freeze `master` on `release.md` :pr:`726`
* Update release instructions with more details :pr:`727` :pr:`733`
Expand Down
14 changes: 13 additions & 1 deletion evalml/tests/utils_tests/test_gen_utils.py
@@ -1,4 +1,5 @@
import inspect
from unittest.mock import patch

import numpy as np
import pytest
Expand All @@ -13,11 +14,22 @@
)


def test_import_or_raise_errors():
@patch('importlib.import_module')
def test_import_or_raise_errors(dummy_importlib):
def _mock_import_function(library_str):
if library_str == "_evalml":
raise ImportError("Mock ImportError executed!")
if library_str == "attr_error_lib":
raise Exception("Mock Exception executed!")

dummy_importlib.side_effect = _mock_import_function

with pytest.raises(ImportError, match="Missing optional dependency '_evalml'"):
import_or_raise("_evalml")
with pytest.raises(ImportError, match="Missing optional dependency '_evalml'. Please use pip to install _evalml. Additional error message"):
import_or_raise("_evalml", "Additional error message")
with pytest.raises(Exception, match="An exception occurred while trying to import `attr_error_lib`: Mock Exception executed!"):
import_or_raise("attr_error_lib")


def test_import_or_raise_imports():
Expand Down
3 changes: 3 additions & 0 deletions evalml/utils/gen_utils.py
Expand Up @@ -20,6 +20,9 @@ def import_or_raise(library, error_msg=None):
error_msg = ""
msg = (f"Missing optional dependency '{library}'. Please use pip to install {library}. {error_msg}")
raise ImportError(msg)
except Exception as ex:
msg = (f"An exception occurred while trying to import `{library}`: {str(ex)}")
raise Exception(msg)


def convert_to_seconds(input_str):
Expand Down

0 comments on commit 4c0a24f

Please sign in to comment.