Skip to content

Commit 1b0f007

Browse files
committed
Add object_name_to_class_name function. (more)
Different from `string_to_class_name`, which is more generic. Since we don’t auto-generate classes for all objects, we need to be smart about which object_names get shown to users as `dict` and `list` vs a subclass of `PlotlyList` and `PlotlyDict`.
1 parent 41e9675 commit 1b0f007

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

plotly/graph_reference.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,20 @@ def string_to_class_name(string):
141141
return str(string)
142142

143143

144+
def object_name_to_class_name(object_name):
145+
"""Not all objects have classes auto-generated."""
146+
if object_name in TRACE_NAMES:
147+
return string_to_class_name(object_name)
148+
149+
if object_name in OBJECT_NAME_TO_CLASS_NAME:
150+
return OBJECT_NAME_TO_CLASS_NAME[object_name]
151+
152+
if object_name in ARRAYS:
153+
return 'list'
154+
else:
155+
return 'dict'
156+
157+
144158
def get_attributes_dicts(object_name, parent_object_names=()):
145159
"""
146160
Returns *all* attribute information given the context of parents.

plotly/tests/test_core/test_graph_reference/test_graph_reference.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,88 @@ def test_capitalize_after_underscore(self):
7575
self.assertEqual(string_to_class_name(object_name), class_name)
7676

7777

78+
class TestObjectNameToClassName(TestCase):
79+
80+
def test_backwards_compat(self):
81+
82+
# Old classes should still be shown to users.
83+
84+
test_tuples = [
85+
('angularaxis', 'AngularAxis'),
86+
('annotation', 'Annotation'),
87+
('annotations', 'Annotations'),
88+
('area', 'Area'),
89+
('colorbar', 'ColorBar'),
90+
('contour', 'Contour'),
91+
('contours', 'Contours'),
92+
('data', 'Data'),
93+
('error_x', 'ErrorX'),
94+
('error_y', 'ErrorY'),
95+
('error_z', 'ErrorZ'),
96+
('figure', 'Figure'),
97+
('font', 'Font'),
98+
('layout', 'Layout'),
99+
('legend', 'Legend'),
100+
('margin', 'Margin'),
101+
('marker', 'Marker'),
102+
('radialaxis', 'RadialAxis'),
103+
('scene', 'Scene'),
104+
('stream', 'Stream'),
105+
('xaxis', 'XAxis'),
106+
('xbins', 'XBins'),
107+
('yaxis', 'YAxis'),
108+
('ybins', 'YBins'),
109+
('zaxis', 'ZAxis')
110+
]
111+
112+
for object_name, expected_class_name in test_tuples:
113+
class_name = gr.object_name_to_class_name(object_name)
114+
msg = (object_name, expected_class_name, class_name)
115+
self.assertEqual(class_name, expected_class_name, msg=msg)
116+
117+
def test_old_traces(self):
118+
119+
# While the old trace classes exist, the newer should be returned.
120+
121+
test_tuples = [
122+
('histogram2dcontour', 'Histogram2dcontour')
123+
]
124+
125+
for object_name, expected_class_name in test_tuples:
126+
class_name = gr.object_name_to_class_name(object_name)
127+
msg = (object_name, expected_class_name, class_name)
128+
self.assertEqual(class_name, expected_class_name, msg=msg)
129+
130+
def test_new_traces(self):
131+
132+
# New traces should get have classes defined.
133+
134+
test_tuples = [
135+
('choropleth', 'Choropleth'),
136+
('pie', 'Pie')
137+
]
138+
139+
for object_name, expected_class_name in test_tuples:
140+
class_name = gr.object_name_to_class_name(object_name)
141+
msg = (object_name, expected_class_name, class_name)
142+
self.assertEqual(class_name, expected_class_name, msg=msg)
143+
144+
def test_new_non_trace_objects(self):
145+
146+
# New objects get 'dict' or 'list'.
147+
148+
test_tuples = [
149+
('geo', 'dict'),
150+
('shapes', 'list'),
151+
('shape', 'dict'),
152+
]
153+
154+
for object_name, expected_class_name in test_tuples:
155+
class_name = gr.object_name_to_class_name(object_name)
156+
msg = (object_name, expected_class_name, class_name)
157+
self.assertEqual(class_name, expected_class_name, msg=msg)
158+
159+
78160
class TestGetAttributesMethods(TestCase):
79161

80162
def test_get_subplot_attributes(self):

0 commit comments

Comments
 (0)