Skip to content

Commit 02fb3a4

Browse files
authored
Merge pull request #4727 from plotly/add-skipped-key-tests
Add tests for the keys that should be skipped in base64 conversion
2 parents 8d7edf6 + 4452868 commit 02fb3a4

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

packages/python/plotly/_plotly_utils/basevalidators.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22
import numbers
33
import textwrap
4+
import traceback
45
import uuid
56
from importlib import import_module
67
import copy
@@ -291,6 +292,15 @@ def is_typed_array_spec(v):
291292
return isinstance(v, dict) and "bdata" in v and "dtype" in v
292293

293294

295+
def has_skipped_key(all_parent_keys):
296+
"""
297+
Return whether any keys in the parent hierarchy are in the list of keys that
298+
are skipped for conversion to the typed array spec
299+
"""
300+
skipped_keys = ["geojson", "layer", "range"]
301+
return any(skipped_key in all_parent_keys for skipped_key in skipped_keys)
302+
303+
294304
def is_none_or_typed_array_spec(v):
295305
return v is None or is_typed_array_spec(v)
296306

@@ -488,9 +498,10 @@ def description(self):
488498
)
489499

490500
def validate_coerce(self, v):
491-
492501
if is_none_or_typed_array_spec(v):
493502
pass
503+
elif has_skipped_key(self.parent_name):
504+
v = to_scalar_or_list(v)
494505
elif is_homogeneous_array(v):
495506
v = to_typed_array_spec(v)
496507
elif is_simple_array(v):
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
from unittest import TestCase
2+
import numpy as np
3+
from plotly.tests.test_optional.optional_utils import NumpyTestUtilsMixin
4+
import plotly.graph_objs as go
5+
6+
7+
class TestShouldNotUseBase64InUnsupportedKeys(NumpyTestUtilsMixin, TestCase):
8+
def test_np_geojson(self):
9+
normal_coordinates = [
10+
[
11+
[-87, 35],
12+
[-87, 30],
13+
[-85, 30],
14+
[-85, 35],
15+
]
16+
]
17+
18+
numpy_coordinates = np.array(normal_coordinates)
19+
20+
data = [
21+
{
22+
"type": "choropleth",
23+
"locations": ["AL"],
24+
"featureidkey": "properties.id",
25+
"z": np.array([10]),
26+
"geojson": {
27+
"type": "Feature",
28+
"properties": {"id": "AL"},
29+
"geometry": {"type": "Polygon", "coordinates": numpy_coordinates},
30+
},
31+
}
32+
]
33+
34+
fig = go.Figure(data=data)
35+
36+
assert (
37+
fig["data"][0]["geojson"]["geometry"]["coordinates"] == normal_coordinates
38+
).all()
39+
40+
def test_np_layers(self):
41+
layout = {
42+
"mapbox": {
43+
"layers": [
44+
{
45+
"sourcetype": "geojson",
46+
"type": "line",
47+
"line": {"dash": np.array([2.5, 1])},
48+
"source": {
49+
"type": "FeatureCollection",
50+
"features": [
51+
{
52+
"type": "Feature",
53+
"geometry": {
54+
"type": "LineString",
55+
"coordinates": np.array(
56+
[[0.25, 52], [0.75, 50]]
57+
),
58+
},
59+
}
60+
],
61+
},
62+
},
63+
],
64+
"center": {"lon": 0.5, "lat": 51},
65+
},
66+
}
67+
data = [{"type": "scattermapbox"}]
68+
69+
fig = go.Figure(data=data, layout=layout)
70+
71+
assert fig.layout["mapbox"]["layers"][0]["line"]["dash"] == (2.5, 1)
72+
73+
assert (
74+
fig.layout["mapbox"]["layers"][0]["source"]["features"][0]["geometry"][
75+
"coordinates"
76+
]
77+
== [[0.25, 52], [0.75, 50]]
78+
).all()
79+
80+
def test_np_range(self):
81+
layout = {"xaxis": {"range": np.array([0, 1])}}
82+
83+
fig = go.Figure(data=[{"type": "scatter"}], layout=layout)
84+
85+
assert fig.layout["xaxis"]["range"] == (0, 1)

0 commit comments

Comments
 (0)