8
8
from ipywidgets import Button , HBox , Layout , Output , VBox , Widget
9
9
10
10
from .._utils import Formatter
11
- from ._check import Check , ChecksResult
11
+ from ._check import Check , CheckResult
12
12
13
13
14
14
class CheckableWidget :
@@ -38,7 +38,9 @@ def compute_output_to_check(
38
38
"""
39
39
raise NotImplementedError ("compute_output_to_check has not been implemented" )
40
40
41
- def handle_checks_result (self , result : Union [ChecksResult , Exception ]) -> None :
41
+ def handle_checks_result (
42
+ self , results : List [Union [CheckResult , Exception ]]
43
+ ) -> None :
42
44
"""
43
45
Function that controls how results of the checks are handled.
44
46
"""
@@ -102,7 +104,7 @@ def compute_and_set_references(self):
102
104
103
105
self ._check_registry .compute_and_set_references (self )
104
106
105
- def check (self ) -> Union [ChecksResult , Exception ]:
107
+ def check (self ) -> List [ Union [CheckResult , Exception ] ]:
106
108
if self ._check_registry is None :
107
109
raise ValueError (
108
110
"No check registry given on initialization, " "check cannot be used"
@@ -220,44 +222,47 @@ def compute_and_set_references(self, widget: Widget):
220
222
try :
221
223
check .compute_and_set_references ()
222
224
except Exception as exception :
223
- widget .handle_checks_result (exception )
225
+ widget .handle_checks_result ([ exception ] )
224
226
raise exception
225
227
226
228
def compute_outputs (self , widget : CheckableWidget ):
227
229
for check in self ._checks [widget ]:
228
230
try :
229
231
return check .compute_outputs ()
230
232
except Exception as exception :
231
- widget .handle_checks_result (exception )
233
+ widget .handle_checks_result ([ exception ] )
232
234
raise exception
233
235
234
236
def compute_and_set_all_references (self ):
235
237
for widget in self ._checks .keys ():
236
238
self .compute_and_set_references (widget )
237
239
238
- def check_widget (self , widget : CheckableWidget ) -> Union [ChecksResult , Exception ]:
240
+ def check_widget (
241
+ self , widget : CheckableWidget
242
+ ) -> List [Union [CheckResult , Exception ]]:
243
+ checks_result = []
239
244
try :
240
- results = ChecksResult ()
241
245
for check in self ._checks [widget ]:
242
246
result = check .check_function ()
243
- results . extend (result )
244
- widget .handle_checks_result (result )
245
- return results
247
+ checks_result . append (result )
248
+ widget .handle_checks_result (checks_result )
249
+ return checks_result
246
250
except Exception as exception :
247
- widget .handle_checks_result (exception )
248
- return exception
251
+ checks_result .append (exception )
252
+ widget .handle_checks_result (checks_result )
253
+ return checks_result
249
254
250
255
def check_all_widgets (
251
256
self ,
252
- ) -> OrderedDict [CheckableWidget , Union [ChecksResult , Exception ]]:
253
- messages : OrderedDict [CheckableWidget , Union [ChecksResult , Exception ]] = (
257
+ ) -> OrderedDict [CheckableWidget , List [ Union [CheckResult , Exception ] ]]:
258
+ messages : OrderedDict [CheckableWidget , List [ Union [CheckResult , Exception ] ]] = (
254
259
OrderedDict ()
255
260
)
256
261
for widget in self ._checks .keys ():
257
262
try :
258
263
messages [widget ] = self .check_widget (widget )
259
264
except Exception as exception :
260
- messages [widget ] = exception
265
+ messages [widget ] = [ exception ]
261
266
return messages
262
267
263
268
def _on_click_set_all_references_button (self , change : dict ):
@@ -276,47 +281,60 @@ def _on_click_check_all_widgets_button(self, change: dict):
276
281
widgets_results = self .check_all_widgets ()
277
282
for widget , widget_results in widgets_results .items ():
278
283
with self ._output :
279
- if isinstance (widget_results , Exception ):
284
+ if wrong_types := [
285
+ result
286
+ for result in widget_results
287
+ if not (
288
+ isinstance (result , Exception )
289
+ or isinstance (result , CheckResult )
290
+ )
291
+ ]:
292
+ raise ValueError (
293
+ f"Not supported result type { type (wrong_types [0 ])} . "
294
+ "Only results of type `Exception` and `CheckResult` "
295
+ "are supported."
296
+ )
297
+ elif [
298
+ result
299
+ for result in widget_results
300
+ if isinstance (result , Exception )
301
+ ]:
280
302
print (
281
303
Formatter .color_error_message (
282
304
Formatter .format_title_message (
283
- f"Widget { self ._names [widget ]} " f" raised error: "
305
+ f"Widget { self ._names [widget ]} raised error. "
284
306
)
285
307
)
286
308
)
287
- raise widget_results
288
- elif isinstance (widget_results , ChecksResult ):
289
- if widget_results .successful :
290
- print (
291
- Formatter .color_success_message (
292
- Formatter .format_title_message (
293
- f"Widget { self ._names [widget ]} all checks "
294
- f"were successful"
295
- )
309
+
310
+ elif not [
311
+ result
312
+ for result in widget_results
313
+ if isinstance (result , CheckResult ) and not result .successful
314
+ ]:
315
+ print (
316
+ Formatter .color_success_message (
317
+ Formatter .format_title_message (
318
+ f"Widget { self ._names [widget ]} all checks "
319
+ f"were successful."
296
320
)
297
321
)
298
- print (widget_results .message ())
299
- else :
300
- print (
301
- Formatter .color_error_message (
302
- Formatter .format_title_message (
303
- f"Widget { self ._names [widget ]} not all checks "
304
- "were successful:"
305
- )
322
+ )
323
+ else :
324
+ print (
325
+ Formatter .color_error_message (
326
+ Formatter .format_title_message (
327
+ f"Widget { self ._names [widget ]} not all checks "
328
+ "were successful."
306
329
)
307
330
)
308
- print (widget_results .message ())
309
- else :
310
- raise ValueError (
311
- f"Not supported result type { type (widget_results )} . "
312
- "Only results of type `Exception` and `CheckResult` "
313
- "are supported."
314
331
)
315
332
except Exception as exception :
316
333
with self ._output :
317
334
print (
318
335
Formatter .color_error_message (
319
336
"Error raised while checking widgets:"
320
- )
337
+ ),
338
+ exception ,
321
339
)
322
340
raise exception
0 commit comments