File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed
tests/test_core/test_optional_imports Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Stand-alone module to provide information about whether optional deps exist.
3+
4+ """
5+ from __future__ import absolute_import
6+
7+ from importlib import import_module
8+
9+ _not_importable = set ()
10+
11+
12+ def get_module (name ):
13+ """
14+ Return module or None. Absolute import is required.
15+
16+ :param (str) name: Dot-separated module path. E.g., 'scipy.stats'.
17+ :raise: (ImportError) Only when exc_msg is defined.
18+ :return: (module|None) If import succeeds, the module will be returned.
19+
20+ """
21+ if name not in _not_importable :
22+ try :
23+ return import_module (name )
24+ except ImportError :
25+ _not_importable .add (name )
Original file line number Diff line number Diff line change 1+ from __future__ import absolute_import
2+
3+ from unittest import TestCase
4+
5+ from plotly .optional_imports import get_module
6+
7+
8+ class OptionalImportsTest (TestCase ):
9+
10+ def test_get_module_exists (self ):
11+ import math
12+ module = get_module ('math' )
13+ self .assertIsNotNone (module )
14+ self .assertEqual (math , module )
15+
16+ def test_get_module_exists_submodule (self ):
17+ import requests .sessions
18+ module = get_module ('requests.sessions' )
19+ self .assertIsNotNone (module )
20+ self .assertEqual (requests .sessions , module )
21+
22+ def test_get_module_does_not_exist (self ):
23+ module = get_module ('hoopla' )
24+ self .assertIsNone (module )
You can’t perform that action at this time.
0 commit comments