8000 Just add `path` when raising exception. · modulexcite/plotly.py@17b6378 · GitHub
[go: up one dir, main page]

Skip to content

Commit 17b6378

Browse files
committed
Just add path when raising exception.
1 parent 2d28cb3 commit 17b6378

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

plotly/exceptions.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def __str__(self):
101101

102102

103103
class PlotlyDictKeyError(PlotlyGraphObjectError):
104-
def __init__(self, obj='', key='', **kwargs):
104+
def __init__(self, obj, path=(), **kwargs):
105+
key = path[-1]
105106
message = (
106107
"Invalid key, '{key}', for class, '{obj_name}'.\n\nRun "
107108
"'help(plotly.graph_objs.{obj_name})' for more information."
@@ -110,13 +111,14 @@ def __init__(self, obj='', key='', **kwargs):
110111
plain_message = ("Invalid key, '{key}', found in '{obj}' object"
111112
"".format(key=key, obj=obj.__class__.__name__))
112113
super(PlotlyDictKeyError, self).__init__(message=message,
113-
path=[key],
114+
path=path,
114115
plain_message=plain_message,
115116
**kwargs)
116117

117118

118119
class PlotlyDictValueError(PlotlyGraphObjectError):
119-
def __init__(self, obj='', key='', value='', val_types='', **kwargs):
120+
def __init__(self, obj, value, val_types, path=(), **kwargs):
121+
key = path[-1]
120122
message = (
121123
"Invalid value type, '{value_name}', associated with key, "
122124
"'{key}', for class, '{obj_name}'.\nValid types for this key "
@@ -130,12 +132,13 @@ def __init__(self, obj='', key='', value='', val_types='', **kwargs):
130132
"'{key}'".format(key=key, obj=obj.__class__.__name__))
131133
super(PlotlyDictValueError, self).__init__(message=message,
132134
plain_message=plain_message,
133-
path=[key],
135+
path=path,
134136
**kwargs)
135137

136138

137139
class PlotlyListEntryError(PlotlyGraphObjectError):
138-
def __init__(self, obj='', index='', entry='', **kwargs):
140+
def __init__(self, obj, path=(), **kwargs):
141+
index = path[-1]
139142
message = (
140143
"The entry at index, '{0}', is invalid in a '{1}' object"
141144
"".format(index, obj.__class__.__name__)
@@ -146,12 +149,13 @@ def __init__(self, obj='', index='', entry='', **kwargs):
146149
)
147150
super(PlotlyListEntryError, self).__init__(message=message,
148151
plain_message=plain_message,
149-
path=[index],
152+
path=path,
150153
**kwargs)
151154

152155

153156
class PlotlyDataTypeError(PlotlyGraphObjectError):
154-
def __init__(self, obj='', index='', **kwargs):
157+
def __init__(self, obj, path=(), **kwargs):
158+
index = path[-1]
155159
message = (
156160
"The entry at index, '{0}', is invalid because it does not "
157161
"contain a valid 'type' key-value. This is required for valid "
@@ -163,7 +167,7 @@ def __init__(self, obj='', index='', **kwargs):
163167
"lists.".format(index))
164168
super(PlotlyDataTypeError, self).__init__(message=message,
165169
plain_message=plain_message,
166-
path=[index],
170+
path=path,
167171
**kwargs)
168172

169173

plotly/graph_objs/graph_objs.py

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def __init__(self, *args, **kwargs):
139139
if args and isinstance(args[0], dict):
140140
raise exceptions.PlotlyListEntryError(
141141
obj=self,
142-
index=0,
142+
path=[0],
143143
note="Just like a `list`, `{name}` must be instantiated with "
144144
"a *single* collection.\n"
145145
"In other words these are OK:\n"
@@ -211,9 +211,8 @@ def _value_to_graph_object(self, index, value, _raise=True):
211211
"""
212212
if not isinstance(value, dict):
213213
if _raise:
214-
e = exceptions.PlotlyListEntryError(self, index, value)
215-
e.path = self._get_path() + (index, )
216-
raise e
214+
path = self._get_path() + (index, )
215+
raise exceptions.PlotlyListEntryError(self, path=path)
217216
else:
218217
return
219218

@@ -422,9 +421,8 @@ def __setitem__(self, key, value, _raise=True):
422421
return super(PlotlyDict, self).__setitem__(key, value)
423422
else:
424423
if _raise:
425-
e = exceptions.PlotlyDictKeyError(self, key)
426-
e.path = self._get_path() + (key, )
427-
raise e
424+
path = self._get_path() + (key, )
425+
raise exceptions.PlotlyDictKeyError(self, path=path)
428426
return
429427

430428
if self._get_attribute_role(key) == 'object':
@@ -532,10 +530,9 @@ def _value_to_graph_object(self, key, value, _raise=True):
532530

533531
if not isinstance(value, val_types):
534532
if _raise:
535-
e = exceptions.PlotlyDictValueError(self, key, value,
536-
val_types)
537-
e.path = self._get_path() + (key, )
538-
raise e
533+
path = self._get_path() + (key, )
534+
raise exceptions.PlotlyDictValueError(self, value, val_types,
535+
path=path)
539536
else:
540537
return
541538

@@ -927,17 +924,16 @@ def _value_to_graph_object(self, index, value, _raise=True):
927924
if not isinstance(value, dict):
928925
if _raise:
929926
note = 'Entry should subclass dict.'
930-
raise exceptions.PlotlyListEntryError(self, index, value,
931-
note=note)
927+
path = self._get_path() + (index, )
928+
raise exceptions.PlotlyListEntryError(self, path, note=note)
932929
else:
933930
return
934931

935932
item = value.get('type', 'scatter')
936933
if item not in graph_reference.ARRAYS['data']['items']:
937934
if _raise:
938-
err = exceptions.PlotlyDataTypeError(self, index)
939-
err.path = self._get_path() + (0, )
940-
raise err
935+
path = self._get_path() + (0, )
936+
raise exceptions.PlotlyDataTypeError(self, path=path)
941937

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

0 commit comments

Comments
 (0)
0