Skip to content

Commit cd1ef15

Browse files
committed
Added init tests to check lazy imports
1 parent 9d2fc12 commit cd1ef15

File tree

7 files changed

+100
-3
lines changed

7 files changed

+100
-3
lines changed

packages/python/plotly/plotly/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
"colors",
5656
"__version__",
5757
]
58+
59+
# Set default template (for >= 3.7 this is done in ploty/io/__init__.py)
60+
from plotly.io import templates
61+
templates._default = "plotly"
5862
else:
5963
__all__, __getattr__ = relative_import(
6064
__name__,
@@ -68,6 +72,7 @@
6872
".io",
6973
".data",
7074
".colors",
75+
".express",
7176
],
7277
[".version.__version__"],
7378
)
@@ -76,7 +81,4 @@
7681
# TODO: come back to _docstring_gen
7782
# from plotly import _docstring_gen
7883

79-
# TODO: Set default template here to make sure import process is complete
80-
from plotly.io import templates
8184

82-
templates._default = "plotly"

packages/python/plotly/plotly/io/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@
4545
"._renderers.show",
4646
],
4747
)
48+
49+
# Set default template (for < 3.7 this is done in ploty/__init__.py)
50+
from plotly.io import templates
51+
templates._default = "plotly"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
The test_init test the plotly.py initialization logic and must be run individually.
2+
3+
For example, run...
4+
```
5+
$ pytest test_init/test_lazy_imports.py
6+
$ pytest test_init/test_dependencies_not_imported.py
7+
```
8+
9+
instead of ...
10+
```
11+
$ pytest test_init
12+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import sys
2+
import pytest
3+
4+
version_skip = pytest.mark.skipif(
5+
sys.version_info < (3, 7), reason="Python version < 3.7"
6+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
from . import version_skip
3+
4+
5+
@version_skip
6+
def test_dependencies_not_imported():
7+
8+
# Check that creating a figure without using numpy and pandas does not result in
9+
# the import of numpy and pandas, even if they are installed.
10+
assert 'plotly' not in sys.modules
11+
assert 'numpy' not in sys.modules
12+
assert 'pandas' not in sys.modules
13+
14+
import plotly.graph_objects as go
15+
fig = go.Figure().add_scatter(x=[0], y=[1])
16+
fig.to_json()
17+
18+
assert 'plotly' in sys.modules
19+
assert 'numpy' not in sys.modules
20+
assert 'pandas' not in sys.modules
21+
22+
# check that numpy is installed
23+
import numpy as np
24+
fig = go.Figure().add_scatter(x=np.array([0]), y=np.array([1]))
25+
fig.to_json()
26+
assert 'numpy' in sys.modules
27+
assert 'pandas' not in sys.modules
28+
29+
# check that pandas is installed
30+
import pandas as pd
31+
fig = go.Figure().add_scatter(x=pd.Series([0]), y=pd.Series([1]))
32+
fig.to_json()
33+
assert 'pandas' in sys.modules
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import sys
2+
from . import version_skip
3+
4+
5+
@version_skip
6+
def test_lazy_imports():
7+
8+
# plotly not imported yet
9+
assert 'plotly' not in sys.modules
10+
11+
# Import top-level plotly module
12+
import plotly
13+
assert 'plotly' in sys.modules
14+
15+
# Check that submodules are not auto-imported, but can be be accessed using
16+
# attribute syntax
17+
submodules = ['graph_objs', 'io', 'express']
18+
for m in submodules:
19+
module_str = 'plotly.' + m
20+
assert module_str not in sys.modules
21+
22+
getattr(plotly, m)
23+
assert module_str in sys.modules
24+
25+
# Check that constructing and serializing empty figure doesn't auto-import
26+
# nested graph objects
27+
plotly.graph_objects.Figure().to_json()
28+
submodules = [('layout', 'title'), ('scatter', 'marker'), ('scattergl', 'marker')]
29+
for module_parts in submodules:
30+
module_str = 'plotly.graph_objs.' + '.'.join(module_parts)
31+
assert module_str not in sys.modules
32+
33+
# Use getattr to
34+
module = plotly.graph_objects
35+
for m in module_parts:
36+
module = getattr(module, m)
37+
38+
assert module_str in sys.modules

packages/python/plotly/tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ basepython={env:PLOTLY_TOX_PYTHON_37:}
9898
commands=
9999
python --version
100100
pytest {posargs} -x plotly/tests/test_core
101+
pytest {posargs} -x test_init/test_dependencies_not_imported.py
102+
pytest {posargs} -x test_init/test_lazy_imports.py
101103

102104
; OPTIONAL ENVIRONMENTS
103105
;[testenv:py27-optional]

0 commit comments

Comments
 (0)