Skip to content

Commit 6360421

Browse files
committed
Refactor no_output variable to has_output and less branching
1 parent dfaf5d1 commit 6360421

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

dash/_callback.py

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,12 @@ def register_callback( # pylint: disable=R0914
287287
# Insert callback with scalar (non-multi) Output
288288
insert_output = output
289289
multi = False
290-
no_output = False
290+
has_output = True
291291
else:
292292
# Insert callback as multi Output
293293
insert_output = flatten_grouping(output)
294294
multi = True
295-
no_output = len(output) == 0
295+
has_output = len(output) > 0
296296

297297
long = _kwargs.get("long")
298298
manager = _kwargs.get("manager")
@@ -321,7 +321,7 @@ def register_callback( # pylint: disable=R0914
321321
manager=manager,
322322
dynamic_creator=allow_dynamic_callbacks,
323323
running=running,
324-
no_output=no_output,
324+
no_output=not has_output,
325325
)
326326

327327
# pylint: disable=too-many-locals
@@ -342,7 +342,7 @@ def add_context(*args, **kwargs):
342342
"callback_context", AttributeDict({"updated_props": {}})
343343
)
344344
callback_manager = long and long.get("manager", app_callback_manager)
345-
if not no_output:
345+
if has_output:
346346
_validate.validate_output_spec(insert_output, output_spec, Output)
347347

348348
context_value.set(callback_ctx)
@@ -467,27 +467,28 @@ def add_context(*args, **kwargs):
467467
if NoUpdate.is_no_update(output_value):
468468
raise PreventUpdate
469469

470-
if no_output:
470+
component_ids = collections.defaultdict(dict)
471+
472+
if not has_output:
471473
if output_value is not None:
472474
raise InvalidCallbackReturnValue(
473475
f"No output callback received return value: {output_value}"
474476
)
475477
output_value = []
476478
flat_output_values = []
477-
elif not multi:
478-
output_value, output_spec = [output_value], [output_spec]
479-
flat_output_values = output_value
480479
else:
481-
if isinstance(output_value, (list, tuple)):
482-
# For multi-output, allow top-level collection to be
483-
# list or tuple
484-
output_value = list(output_value)
480+
if not multi:
481+
output_value, output_spec = [output_value], [output_spec]
482+
flat_output_values = output_value
483+
else:
484+
if isinstance(output_value, (list, tuple)):
485+
# For multi-output, allow top-level collection to be
486+
# list or tuple
487+
output_value = list(output_value)
488+
489+
# Flatten grouping and validate grouping structure
490+
flat_output_values = flatten_grouping(output_value, output)
485491

486-
# Flatten grouping and validate grouping structure
487-
flat_output_values = flatten_grouping(output_value, output)
488-
489-
component_ids = collections.defaultdict(dict)
490-
if not no_output:
491492
_validate.validate_multi_return(
492493
output_spec, flat_output_values, callback_id
493494
)

0 commit comments

Comments
 (0)