Skip to content

Commit 3006905

Browse files
committed
simplify validation & fix exception type
1 parent c6dc6a8 commit 3006905

File tree

3 files changed

+30
-29
lines changed

3 files changed

+30
-29
lines changed

dash/_validate.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,35 @@
22
import re
33

44
from .development.base_component import Component
5-
from .dependencies import Input, Output, State
65
from . import exceptions
76
from ._utils import patch_collections_abc, _strings, stringify_id
87

98

10-
def validate_callback(output, inputs, state):
9+
def validate_callback(output, inputs, state, extra_args, types):
10+
Input, Output, State = types
11+
if extra_args:
12+
if not isinstance(extra_args[0], (Output, Input, State)):
13+
raise exceptions.IncorrectTypeException(
14+
"""
15+
Callback arguments must be `Output`, `Input`, or `State` objects,
16+
optionally wrapped in a list or tuple. We found (possibly after
17+
unwrapping a list or tuple):
18+
{}
19+
""".format(
20+
repr(extra_args[0])
21+
)
22+
)
23+
24+
raise exceptions.IncorrectTypeException(
25+
"""
26+
In a callback definition, you must provide all Outputs first,
27+
"then all Inputs, then all States. Found this out of order:
28+
{}
29+
""".format(
30+
repr(extra_args)
31+
)
32+
)
33+
1134
is_multi = isinstance(output, (list, tuple))
1235

1336
outputs = output if is_multi else [output]
@@ -17,27 +40,7 @@ def validate_callback(output, inputs, state):
1740

1841

1942
def validate_callback_args(args, cls):
20-
name = cls.__name__
21-
if not isinstance(args, (list, tuple)):
22-
raise exceptions.IncorrectTypeException(
23-
"""
24-
The {} argument `{}` must be a list or tuple of
25-
`dash.dependencies.{}`s.
26-
""".format(
27-
name.lower(), str(args), name
28-
)
29-
)
30-
3143
for arg in args:
32-
if not isinstance(arg, cls):
33-
raise exceptions.IncorrectTypeException(
34-
"""
35-
The {} argument `{}` must be of type `dash.dependencies.{}`.
36-
""".format(
37-
name.lower(), str(arg), name
38-
)
39-
)
40-
4144
if not isinstance(getattr(arg, "component_property", None), _strings):
4245
raise exceptions.IncorrectTypeException(
4346
"""

dash/dash.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,6 @@ def _insert_callback(self, output, inputs, state, prevent_initial_call):
831831
if prevent_initial_call is None:
832832
prevent_initial_call = self.config.prevent_initial_callbacks
833833

834-
_validate.validate_callback(output, inputs, state)
835834
callback_id = create_callback_id(output)
836835
callback_spec = {
837836
"output": callback_id,

dash/dependencies.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import json
22

3+
from ._validate import validate_callback
4+
35

46
class _Wildcard: # pylint: disable=too-few-public-methods
57
def __init__(self, name):
@@ -157,6 +159,7 @@ def handle_callback_args(args, kwargs):
157159
flat_args += arg if isinstance(arg, (list, tuple)) else [arg]
158160

159161
outputs = extract_callback_args(flat_args, kwargs, "output", Output)
162+
validate_outputs = outputs
160163
if len(outputs) == 1:
161164
out0 = kwargs.get("output", args[0] if args else None)
162165
if not isinstance(out0, (list, tuple)):
@@ -165,11 +168,7 @@ def handle_callback_args(args, kwargs):
165168
inputs = extract_callback_args(flat_args, kwargs, "inputs", Input)
166169
states = extract_callback_args(flat_args, kwargs, "state", State)
167170

168-
if flat_args:
169-
raise TypeError(
170-
"In a callback definition, you must provide all Outputs first,\n"
171-
"then all Inputs, then all States. Trailing this we found:\n"
172-
+ repr(flat_args)
173-
)
171+
types = Input, Output, State
172+
validate_callback(validate_outputs, inputs, states, flat_args, types)
174173

175174
return outputs, inputs, states, prevent_initial_call

0 commit comments

Comments
 (0)