Skip to content

Commit fa3e928

Browse files
committed
Init sub-objects as generic objects.
(Except for `Figure` and `Data`, which are patched)
1 parent 00c1f83 commit fa3e928

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

plotly/graph_objs/graph_objs.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -764,15 +764,23 @@ def create(object_name, *args, **kwargs):
764764
:return: (PlotlyList|PlotlyDict) The instantiated graph object.
765765
766766
"""
767-
if (object_name not in graph_reference.OBJECTS and
768-
object_name not in graph_reference.ARRAYS):
767+
is_array = object_name in graph_reference.ARRAYS
768+
is_object = object_name in graph_reference.OBJECTS
769+
if not (is_array or is_object):
769770
raise exceptions.PlotlyError(
770771
"'{}' is not a valid object name.".format(object_name)
771772
)
772-
class_name = graph_reference.string_to_class_name(object_name)
773-
graph_object_class = globals()[class_name]
774773

775-
return graph_object_class(*args, **kwargs)
774+
# We patch Figure and Data, so they actually require the subclass.
775+
class_name = graph_reference.OBJECT_NAME_TO_CLASS_NAME.get(object_name)
776+
if class_name in ['Figure', 'Data']:
777+
return globals()[class_name](*args, **kwargs)
778+
else:
779+
kwargs['_name'] = object_name
780+
if is_array:
781+
return PlotlyList(*args, **kwargs)
782+
else:
783+
return PlotlyDict(*args, **kwargs)
776784

777785

778786
def _add_classes_to_globals(globals):
@@ -782,12 +790,16 @@ def _add_classes_to_globals(globals):
782790
:param (dict) globals: The globals() dict from this module.
783791
784792
"""
785-
object_names = list(graph_reference.OBJECTS.keys())
786-
array_names = list(graph_reference.ARRAYS.keys())
787-
for object_name in object_names + array_names:
793+
for class_name, class_dict in graph_reference.CLASSES.items():
794+
object_name = class_dict['object_name']
795+
base_type = class_dict['base_type']
796+
797+
# This is for backwards compat (e.g., Trace) and future changes.
798+
if object_name is None:
799+
globals[class_name] = base_type
800+
continue
788801

789802
doc = graph_objs_tools.get_help(object_name)
790-
class_name = graph_reference.string_to_class_name(object_name)
791803
if object_name in graph_reference.ARRAYS:
792804
class_bases = (PlotlyList, )
793805
else:
@@ -800,12 +812,6 @@ def _add_classes_to_globals(globals):
800812

801813
globals[class_name] = cls
802814

803-
for key, val in graph_reference.CLASS_NAMES_TO_OBJECT_NAMES.items():
804-
if val == object_name:
805-
class_dict.update(__name__=key)
806-
cls = type(str(key), class_bases, class_dict)
807-
globals[key] = cls
808-
809815

810816
def _patch_figure_class(figure_class):
811817

@@ -980,8 +986,7 @@ def get_data(self, flatten=False):
980986
_add_classes_to_globals(globals())
981987
_patch_figure_class(globals()['Figure'])
982988
_patch_data_class(globals()['Data'])
983-
Trace = dict # for backwards compat.
984989

985990
# We don't want to expose this module to users, just the classes.
986991
# See http://blog.labix.org/2008/06/27/watch-out-for-listdictkeys-in-python-3
987-
__all__ = list(graph_reference.CLASS_NAMES_TO_OBJECT_NAMES.keys())
992+
__all__ = list(graph_reference.CLASSES.keys())

0 commit comments

Comments
 (0)