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

Handle optional import where imported module raises exception on import #1192

Merged
merged 2 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 9 additions & 0 deletions plotly/optional_imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
from __future__ import absolute_import

from importlib import import_module
import logging

logger = logging.getLogger(__name__)
print(__name__)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove print statement

_not_importable = set()


Expand All @@ -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)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove this comment

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!")
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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mock is not used here


class OptionalImportsTest(TestCase):

Expand All @@ -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)