88from ipywidgets import Button , HBox , Layout , Output , VBox , Widget
99
1010from .._utils import Formatter
11- from ._check import Check , ChecksResult
11+ from ._check import Check , CheckResult
1212
1313
1414class CheckableWidget :
@@ -38,7 +38,9 @@ def compute_output_to_check(
3838 """
3939 raise NotImplementedError ("compute_output_to_check has not been implemented" )
4040
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 :
4244 """
4345 Function that controls how results of the checks are handled.
4446 """
@@ -102,7 +104,7 @@ def compute_and_set_references(self):
102104
103105 self ._check_registry .compute_and_set_references (self )
104106
105- def check (self ) -> Union [ChecksResult , Exception ]:
107+ def check (self ) -> List [ Union [CheckResult , Exception ] ]:
106108 if self ._check_registry is None :
107109 raise ValueError (
108110 "No check registry given on initialization, " "check cannot be used"
@@ -220,44 +222,47 @@ def compute_and_set_references(self, widget: Widget):
220222 try :
221223 check .compute_and_set_references ()
222224 except Exception as exception :
223- widget .handle_checks_result (exception )
225+ widget .handle_checks_result ([ exception ] )
224226 raise exception
225227
226228 def compute_outputs (self , widget : CheckableWidget ):
227229 for check in self ._checks [widget ]:
228230 try :
229231 return check .compute_outputs ()
230232 except Exception as exception :
231- widget .handle_checks_result (exception )
233+ widget .handle_checks_result ([ exception ] )
232234 raise exception
233235
234236 def compute_and_set_all_references (self ):
235237 for widget in self ._checks .keys ():
236238 self .compute_and_set_references (widget )
237239
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 = []
239244 try :
240- results = ChecksResult ()
241245 for check in self ._checks [widget ]:
242246 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
246250 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
249254
250255 def check_all_widgets (
251256 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 ] ]] = (
254259 OrderedDict ()
255260 )
256261 for widget in self ._checks .keys ():
257262 try :
258263 messages [widget ] = self .check_widget (widget )
259264 except Exception as exception :
260- messages [widget ] = exception
265+ messages [widget ] = [ exception ]
261266 return messages
262267
263268 def _on_click_set_all_references_button (self , change : dict ):
@@ -276,47 +281,60 @@ def _on_click_check_all_widgets_button(self, change: dict):
276281 widgets_results = self .check_all_widgets ()
277282 for widget , widget_results in widgets_results .items ():
278283 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+ ]:
280302 print (
281303 Formatter .color_error_message (
282304 Formatter .format_title_message (
283- f"Widget { self ._names [widget ]} " f" raised error: "
305+ f"Widget { self ._names [widget ]} raised error. "
284306 )
285307 )
286308 )
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."
296320 )
297321 )
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."
306329 )
307330 )
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."
314331 )
315332 except Exception as exception :
316333 with self ._output :
317334 print (
318335 Formatter .color_error_message (
319336 "Error raised while checking widgets:"
320- )
337+ ),
338+ exception ,
321339 )
322340 raise exception
0 commit comments