8000 Clean up subclasses of PlotlyGraphObjectError. · ii0/plotly.py@21535dc · GitHub
[go: up one dir, main page]

Skip to content

Commit 21535dc

Browse files
committed
Clean up subclasses of PlotlyGraphObjectError.
* `note` back to `notes` (we actually need it…) * simplify (unify) call signatures * return `.help()` output in exception messages.
1 parent 56412ba commit 21535dc

File tree

2 files changed

+53
-80
lines changed

2 files changed

+53
-80
lines changed

plotly/exceptions.py

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -81,93 +81,68 @@ class PlotlyEmptyDataError(PlotlyError):
8181

8282
# Graph Objects Errors
8383
class PlotlyGraphObjectError(PlotlyError):
84-
def __init__(self, message='', path=(), note='', plain_message=''):
84+
def __init__(self, message='', path=(), notes=(), plain_message=''):
8585
self.message = message
8686
self.plain_message = plain_message
8787
self.path = list(path)
88-
self.note = note
88+
self.notes = notes
8989
super(PlotlyGraphObjectError, self).__init__(message)
9090

9191
def __str__(self):
9292
format_dict = {
9393
'message': self.message,
9494
'path': '[' + ']['.join(repr(k) for k in self.path) + ']',
95-
'note': self.note
95+
'notes': '\n'.join(self.notes)
9696
}
9797
message = '{message}\n\nPath To Error: {path}'.format(**format_dict)
98-
if format_dict['note']:
99-
message += '\n\nAdditional Notes:\n {note}'.format(**format_dict)
98+
if format_dict['notes']:
99+
message += '\n\nAdditional Notes:\n\n{notes}'.format(**format_dict)
100100
return message
101101

102102

103103
class PlotlyDictKeyError(PlotlyGraphObjectError):
104-
def __init__(self, obj, path=(), **kwargs):
105-
key = path[-1]
106-
message = (
107-
"Invalid key, '{key}', for class, '{obj_name}'.\n\nRun "
108-
"'help(plotly.graph_objs.{obj_name})' for more information."
109-
"".format(key=key, obj_name=obj.__class__.__name__)
104+
def __init__(self, obj, path, notes=()):
105+
format_dict = {'attribute': path[-1], 'object_name': obj._name}
106+
message = ("'{attribute}' is not allowed in '{object_name}'"
107+
.format(**format_dict))
108+
notes = [obj.help(return_help=True)] + list(notes)
109+
super(PlotlyDictKeyError, self).__init__(
110+
message=message, path=path, notes=notes, plain_message=message
110111
)
111-
plain_message = ("Invalid key, '{key}', found in '{obj}' object"
112-
"".format(key=key, obj=obj.__class__.__name__))
113-
super(PlotlyDictKeyError, self).__init__(message=message,
114-
path=path,
115-
plain_message=plain_message,
116-
**kwargs)
117112

118113

119114
class PlotlyDictValueError(PlotlyGraphObjectError):
120-
def __init__(self, obj, value, val_types, path=(), **kwargs):
121-
key = path[-1]
122-
message = (
123-
"Invalid value type, '{value_name}', associated with key, "
124-
"'{key}', for class, '{obj_name}'.\nValid types for this key "
125-
"are:\n '{val_types}'.\n\n"
126-
"Run 'help(plotly.graph_objs.{obj_name})' for more information."
127-
.format(key=key, value_name=value.__class__.__name__,
128-
val_types=val_types, obj_name=obj.__class__.__name__)
115+
def __init__(self, obj, path, notes=()):
116+
format_dict = {'attribute': path[-1], 'object_name': obj._name}
117+
message = ("'{attribute}' has invalid value inside '{object_name}'"
118+
.format(**format_dict))
119+
notes = [obj.help(path[-1], return_help=True)] + list(notes)
120+
super(PlotlyDictValueError, self).__init__(
121+
message=message, plain_message=message, notes=notes, path=path
129122
)
130-
plain_message = ("Invalid value found in '{obj}' associated with key, "
131-
"'{key}'".format(key=key, obj=obj.__class__.__name__))
132-
super(PlotlyDictValueError, self).__init__(message=message,
133-
plain_message=plain_message,
134-
path=path,
135-
**kwargs)
136123

137124

138125
class PlotlyListEntryError(PlotlyGraphObjectError):
139-
def __init__(self, obj, path=(), **kwargs):
140-
index = path[-1]
141-
message = (
142-
"The entry at index, '{0}', is invalid in a '{1}' object"
143-
"".format(index, obj.__class__.__name__)
126+
def __init__(self, obj, path, notes=()):
127+
format_dict = {'index': path[-1], 'object_name': obj._name}
128+
message = ("Invalid entry found in '{object_name}' at index, '{index}'"
129+
.format(**format_dict))
130+
notes = [obj.help(return_help=True)] + list(notes)
131+
super(PlotlyListEntryError, self).__init__(
132+
message=message, plain_message=message, path=path, notes=notes
144133
)
145-
plain_message = (
146-
"Invalid entry found in '{obj}' object at index, '{index}'."
147-
"".format(obj=obj.__class__.__name__, index=index)
148-
)
149-
super(PlotlyListEntryError, self).__init__(message=message,
150-
plain_message=plain_message,
151-
path=path,
152-
**kwargs)
153134

154135

155136
class PlotlyDataTypeError(PlotlyGraphObjectError):
156-
def __init__(self, obj, path=(), **kwargs):
157-
index = path[-1]
158-
message = (
159-
"The entry at index, '{0}', is invalid because it does not "
160-
"contain a valid 'type' key-value. This is required for valid "
161-
"'{1}' lists.".format(index, obj.__class__.__name__)
137+
def __init__(self, obj, path, notes=()):
138+
format_dict = {'index': path[-1], 'object_name': obj._name}
139+
message = ("Invalid entry found in '{object_name}' at index, '{index}'"
140+
.format(**format_dict))
141+
note = "It's invalid because it does't contain a valid 'type' value."
142+
notes = [note] + list(notes)
143+
super(PlotlyDataTypeError, self).__init__(
144+
message=message, plain_message=message, path=path, notes=notes
162145
)
163-
plain_message = (
164-
"Invalid entry found in 'data' object at index, '{0}'. It "
165-
"does not contain a valid 'type' key, required for 'data' "
166-
"lists.".format(index))
167-
super(PlotlyDataTypeError, self).__init__(message=message,
168-
plain_message=plain_message,
169-
path=path,
170-
**kwargs)
171146

172147

173148
# Local Config Errors

plotly/graph_objs/graph_objs.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -145,21 +145,20 @@ def __init__(self, *args, **kwargs):
145145
)
146146

147147
if args and isinstance(args[0], dict):
148-
raise exceptions.PlotlyListEntryError(
149-
obj=self,
150-
path=[0],
151-
note="Just like a `list`, `{name}` must be instantiated with "
152-
"a *single* collection.\n"
153-
"In other words these are OK:\n"
154-
">>> {name}()\n"
155-
">>> {name}([])\n"
156-
">>> {name}([dict()])\n"
157-
">>> {name}([dict(), dict()])\n"
158-
"However, these don't make sense:\n"
159-
">>> {name}(dict())\n"
160-
">>> {name}(dict(), dict())"
161-
.format(name=self._get_class_name())
148+
note = (
149+
"Just like a `list`, `{name}` must be instantiated "
150+
"with a *single* collection.\n"
151+
"In other words these are OK:\n"
152+
">>> {name}()\n"
153+
">>> {name}([])\n"
154+
">>> {name}([dict()])\n"
155+
">>> {name}([dict(), dict()])\n"
156+
"However, these don't make sense:\n"
157+
">>> {name}(dict())\n"
158+
">>> {name}(dict(), dict())"
159+
.format(name=self._get_class_name())
162160
)
161+
raise exceptions.PlotlyListEntryError(self, [0], notes=[note])
163162

164163
super(PlotlyList, self).__init__()
165164

@@ -220,7 +219,7 @@ def _value_to_graph_object(self, index, value, _raise=True):
220219
if not isinstance(value, dict):
221220
if _raise:
222221
path = self._get_path() + (index, )
223-
raise exceptions.PlotlyListEntryError(self, path=path)
222+
raise exceptions.PlotlyListEntryError(self, path)
224223
else:
225224
return
226225

@@ -430,7 +429,7 @@ def __setitem__(self, key, value, _raise=True):
430429
else:
431430
if _raise:
432431
path = self._get_path() + (key, )
433-
raise exceptions.PlotlyDictKeyError(self, path=path)
432+
raise exceptions.PlotlyDictKeyError(self, path)
434433
return
435434

436435
if self._get_attribute_role(key) == 'object':
@@ -539,8 +538,7 @@ def _value_to_graph_object(self, key, value, _raise=True):
539538
if not isinstance(value, val_types):
540539
if _raise:
541540
path = self._get_path() + (key, )
542-
raise exceptions.PlotlyDictValueError(self, value, val_types,
543-
path=path)
541+
raise exceptions.PlotlyDictValueError(self, path)
544542
else:
545543
return
546544

@@ -940,17 +938,17 @@ def _value_to_graph_object(self, index, value, _raise=True):
940938

941939
if not isinstance(value, dict):
942940
if _raise:
943-
note = 'Entry should subclass dict.'
941+
notes = ['Entry should subclass dict.']
944942
path = self._get_path() + (index, )
945-
raise exceptions.PlotlyListEntryError(self, path, note=note)
943+
raise exceptions.PlotlyListEntryError(self, path, notes=notes)
946944
else:
947945
return
948946

949947
item = value.get('type', 'scatter')
950948
if item not in graph_reference.ARRAYS['data']['items']:
951949
if _raise:
952950
path = self._get_path() + (0, )
953-
raise exceptions.PlotlyDataTypeError(self, path=path)
951+
raise exceptions.PlotlyDataTypeError(self, path)
954952

955953
return GraphObjectFactory.create(item, _raise=_raise, _parent=self,
956954
_parent_key=index, **value)

0 commit comments

Comments
 (0)
0