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

Expanded import_or_raise to catch all exceptions #759

Merged
merged 4 commits into from May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. This will be a great place to add some logging once @angela97lin's work adds it!



def convert_to_seconds(input_str):
Expand Down