Skip to content

Commit 267c187

Browse files
authored
bug_2931: resolving numpy integer check and adding test (#2451)
1 parent 03d161c commit 267c187

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

Diff for: packages/python/plotly/plotly/basedatatypes.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1560,7 +1560,14 @@ def _validate_rows_cols(name, n, vals):
15601560
if len(vals) != n:
15611561
BaseFigure._raise_invalid_rows_cols(name=name, n=n, invalid=vals)
15621562

1563-
if [r for r in vals if not isinstance(r, int)]:
1563+
try:
1564+
import numpy as np
1565+
1566+
int_type = (int, np.integer)
1567+
except ImportError:
1568+
int_type = (int,)
1569+
1570+
if [r for r in vals if not isinstance(r, int_type)]:
15641571
BaseFigure._raise_invalid_rows_cols(name=name, n=n, invalid=vals)
15651572
else:
15661573
BaseFigure._raise_invalid_rows_cols(name=name, n=n, invalid=vals)

Diff for: packages/python/plotly/plotly/tests/test_core/test_utils/test_utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,21 @@ def test_node_generator(self):
5252
]
5353
for i, item in enumerate(node_generator(node0)):
5454
self.assertEqual(item, expected_node_path_tuples[i])
55+
56+
57+
class TestNumpyIntegerBaseType(TestCase):
58+
def test_numpy_integer_import(self):
59+
# should generate a figure with subplots of array and not throw a ValueError
60+
import numpy as np
61+
import plotly.graph_objects as go
62+
from plotly.subplots import make_subplots
63+
64+
indices_rows = np.array([1], dtype=np.int)
65+
indices_cols = np.array([1], dtype=np.int)
66+
fig = make_subplots(rows=1, cols=1)
67+
fig.add_trace(go.Scatter(y=[1]), row=indices_rows[0], col=indices_cols[0])
68+
69+
data_path = ("data", 0, "y")
70+
value = get_by_path(fig, data_path)
71+
expected_value = (1,)
72+
self.assertEqual(value, expected_value)

0 commit comments

Comments
 (0)