-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Handle optional import where imported module raises exception on import #1192
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,10 @@ | |
from __future__ import absolute_import | ||
|
||
from importlib import import_module | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
print(__name__) | ||
_not_importable = set() | ||
|
||
|
||
|
@@ -23,3 +26,9 @@ def get_module(name): | |
return import_module(name) | ||
except ImportError: | ||
_not_importable.add(name) | ||
except Exception as e: | ||
print('went boom') | ||
_not_importable.add(name) | ||
msg = "Error importing optional module {}".format(name) | ||
logger.exception(msg) | ||
# logger.error(msg) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this comment |
2 changes: 2 additions & 0 deletions
2
plotly/tests/test_core/test_optional_imports/exploding_module.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# A module that raises and exception on import | ||
raise Exception("Boom!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
from __future__ import absolute_import | ||
|
||
import sys | ||
from unittest import TestCase | ||
|
||
from plotly.optional_imports import get_module | ||
|
||
if sys.version_info.major == 3 and sys.version_info.minor >= 4: | ||
import unittest.mock as mock | ||
else: | ||
import mock | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mock is not used here |
||
|
||
class OptionalImportsTest(TestCase): | ||
|
||
|
@@ -22,3 +26,33 @@ def test_get_module_exists_submodule(self): | |
def test_get_module_does_not_exist(self): | ||
module = get_module('hoopla') | ||
self.assertIsNone(module) | ||
|
||
def test_get_module_import_exception(self): | ||
# Get module that raises an exception on import | ||
module_str = ('plotly.tests.test_core.' | ||
'test_optional_imports.exploding_module') | ||
|
||
if sys.version_info.major == 3 and sys.version_info.minor >= 4: | ||
with self.assertLogs('plotly.optional_imports', level='ERROR') as cm: | ||
module = get_module(module_str) | ||
|
||
# No exception should be raised and None should be returned | ||
self.assertIsNone(module) | ||
|
||
# Check logging level and log message | ||
expected_start = ('ERROR:plotly.optional_imports:' | ||
'Error importing optional module ' + module_str) | ||
|
||
self.assertEqual( | ||
cm.output[0][:len(expected_start)], expected_start) | ||
|
||
# Check that exception message is included after log message | ||
expected_end = 'Boom!' | ||
self.assertEqual( | ||
cm.output[0][-len(expected_end):], expected_end) | ||
else: | ||
# Don't check logging | ||
module = get_module(module_str) | ||
|
||
# No exception should be raised and None should be returned | ||
self.assertIsNone(module) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove print statement