1
1
import collections
2
2
import re
3
+ from textwrap import dedent
3
4
4
5
from .development .base_component import Component
5
6
from . import exceptions
@@ -15,26 +16,26 @@ def validate_callback(output, inputs, state, extra_args, types):
15
16
if extra_args :
16
17
if not isinstance (extra_args [0 ], (Output , Input , State )):
17
18
raise exceptions .IncorrectTypeException (
18
- """
19
- Callback arguments must be `Output`, `Input`, or `State` objects,
20
- optionally wrapped in a list or tuple. We found (possibly after
21
- unwrapping a list or tuple):
22
- {}
23
- """ . format (
24
- repr ( extra_args [ 0 ])
25
- )
19
+ dedent (
20
+ """
21
+ Callback arguments must be `Output`, `Input`, or `State` objects,
22
+ optionally wrapped in a list or tuple. We found (possibly after
23
+ unwrapping a list or tuple):
24
+ {}
25
+ """
26
+ ). format ( repr ( extra_args [ 0 ]))
26
27
)
27
28
28
29
raise exceptions .IncorrectTypeException (
29
- """
30
- In a callback definition, you must provide all Outputs first,
31
- then all Inputs, then all States. After this item:
32
- {}
33
- we found this item next:
34
- {}
35
- """ . format (
36
- repr (( outputs + inputs + state )[ - 1 ]), repr ( extra_args [ 0 ])
37
- )
30
+ dedent (
31
+ """
32
+ In a callback definition, you must provide all Outputs first,
33
+ then all Inputs, then all States. After this item:
34
+ {}
35
+ we found this item next:
36
+ {}
37
+ """
38
+ ). format ( repr (( outputs + inputs + state )[ - 1 ]), repr ( extra_args [ 0 ]))
38
39
)
39
40
40
41
for args in [outputs , inputs , state ]:
@@ -45,11 +46,11 @@ def validate_callback(output, inputs, state, extra_args, types):
45
46
def validate_callback_arg (arg ):
46
47
if not isinstance (getattr (arg , "component_property" , None ), _strings ):
47
48
raise exceptions .IncorrectTypeException (
48
- """
49
- component_property must be a string, found {!r}
50
- """ . format (
51
- arg . component_property
52
- )
49
+ dedent (
50
+ """
51
+ component_property must be a string, found {!r}
52
+ """
53
+ ). format ( arg . component_property )
53
54
)
54
55
55
56
if hasattr (arg , "component_event" ):
@@ -68,11 +69,11 @@ def validate_callback_arg(arg):
68
69
69
70
else :
70
71
raise exceptions .IncorrectTypeException (
71
- """
72
- component_id must be a string or dict, found {!r}
73
- """ . format (
74
- arg . component_id
75
- )
72
+ dedent (
73
+ """
74
+ component_id must be a string or dict, found {!r}
75
+ """
76
+ ). format ( arg . component_id )
76
77
)
77
78
78
79
@@ -85,12 +86,12 @@ def validate_id_dict(arg):
85
86
# cause unwanted collisions
86
87
if not isinstance (k , _strings ):
87
88
raise exceptions .IncorrectTypeException (
88
- """
89
- Wildcard ID keys must be non-empty strings,
90
- found {!r} in id {!r}
91
- """ . format (
92
- k , arg_id
93
- )
89
+ dedent (
90
+ """
91
+ Wildcard ID keys must be non-empty strings,
92
+ found {!r} in id {!r}
93
+ """
94
+ ). format ( k , arg_id )
94
95
)
95
96
96
97
@@ -113,13 +114,13 @@ def validate_id_string(arg):
113
114
def validate_multi_return (outputs_list , output_value , callback_id ):
114
115
if not isinstance (output_value , (list , tuple )):
115
116
raise exceptions .InvalidCallbackReturnValue (
116
- """
117
- The callback {} is a multi-output.
118
- Expected the output type to be a list or tuple but got:
119
- {}.
120
- """ . format (
121
- callback_id , repr ( output_value )
122
- )
117
+ dedent (
118
+ """
119
+ The callback {} is a multi-output.
120
+ Expected the output type to be a list or tuple but got:
121
+ {}.
122
+ """
123
+ ). format ( callback_id , repr ( output_value ))
123
124
)
124
125
125
126
if len (output_value ) != len (outputs_list ):
@@ -137,26 +138,26 @@ def validate_multi_return(outputs_list, output_value, callback_id):
137
138
vi = output_value [i ]
138
139
if not isinstance (vi , (list , tuple )):
139
140
raise exceptions .InvalidCallbackReturnValue (
140
- """
141
- The callback {} output {} is a wildcard multi-output.
142
- Expected the output type to be a list or tuple but got:
143
- {}.
144
- output spec: {}
145
- """ . format (
146
- callback_id , i , repr ( vi ), repr ( outi )
147
- )
141
+ dedent (
142
+ """
143
+ The callback {} output {} is a wildcard multi-output.
144
+ Expected the output type to be a list or tuple but got:
145
+ {}.
146
+ output spec: {}
147
+ """
148
+ ). format ( callback_id , i , repr ( vi ), repr ( outi ))
148
149
)
149
150
150
151
if len (vi ) != len (outi ):
151
152
raise exceptions .InvalidCallbackReturnValue (
152
- """
153
- Invalid number of output values for {} item {}.
154
- Expected {}, got {}
155
- output spec: {}
156
- output value : {}
157
- """ . format (
158
- callback_id , i , len ( vi ), len ( outi ), repr ( outi ), repr ( vi )
159
- )
153
+ dedent (
154
+ """
155
+ Invalid number of output values for {} item {}.
156
+ Expected {}, got {}
157
+ output spec : {}
158
+ output value: {}
159
+ """
160
+ ). format ( callback_id , i , len ( vi ), len ( outi ), repr ( outi ), repr ( vi ))
160
161
)
161
162
162
163
@@ -170,34 +171,38 @@ def _raise_invalid(bad_val, outer_val, path, index=None, toplevel=False):
170
171
)
171
172
outer_type = type (outer_val ).__name__
172
173
if toplevel :
173
- location = """
174
- The value in question is either the only value returned,
175
- or is in the top level of the returned list,
176
- """
174
+ location = dedent (
175
+ """
176
+ The value in question is either the only value returned,
177
+ or is in the top level of the returned list,
178
+ """
179
+ )
177
180
else :
178
181
index_string = "[*]" if index is None else "[{:d}]" .format (index )
179
- location = """
180
- The value in question is located at
181
- {} {} {}
182
- {},
183
- """ . format (
184
- index_string , outer_type , outer_id , path
185
- )
182
+ location = dedent (
183
+ """
184
+ The value in question is located at
185
+ {} {} {}
186
+ {},
187
+ """
188
+ ). format ( index_string , outer_type , outer_id , path )
186
189
187
190
raise exceptions .InvalidCallbackReturnValue (
188
- """
189
- The callback for `{output}`
190
- returned a {object:s} having type `{type}`
191
- which is not JSON serializable.
191
+ dedent (
192
+ """
193
+ The callback for `{output}`
194
+ returned a {object:s} having type `{type}`
195
+ which is not JSON serializable.
192
196
193
- {location}
194
- and has string representation
195
- `{bad_val}`
197
+ {location}
198
+ and has string representation
199
+ `{bad_val}`
196
200
197
- In general, Dash properties can only be
198
- dash components, strings, dictionaries, numbers, None,
199
- or lists of those.
200
- """ .format (
201
+ In general, Dash properties can only be
202
+ dash components, strings, dictionaries, numbers, None,
203
+ or lists of those.
204
+ """
205
+ ).format (
201
206
output = repr (output ),
202
207
object = "tree with one value" if not toplevel else "value" ,
203
208
type = bad_type ,
0 commit comments