|
2 | 2 | import re |
3 | 3 |
|
4 | 4 | from .development.base_component import Component |
5 | | -from .dependencies import Input, Output, State |
6 | 5 | from . import exceptions |
7 | 6 | from ._utils import patch_collections_abc, _strings, stringify_id |
8 | 7 |
|
9 | 8 |
|
10 | | -def validate_callback(output, inputs, state): |
| 9 | +def validate_callback(output, inputs, state, extra_args, types): |
11 | 10 | is_multi = isinstance(output, (list, tuple)) |
12 | 11 |
|
13 | 12 | outputs = output if is_multi else [output] |
14 | 13 |
|
15 | | - for args, cls in [(outputs, Output), (inputs, Input), (state, State)]: |
16 | | - validate_callback_args(args, cls) |
17 | | - |
| 14 | + Input, Output, State = types |
| 15 | + if extra_args: |
| 16 | + if not isinstance(extra_args[0], (Output, Input, State)): |
| 17 | + 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 | + ) |
| 26 | + ) |
18 | 27 |
|
19 | | -def validate_callback_args(args, cls): |
20 | | - name = cls.__name__ |
21 | | - if not isinstance(args, (list, tuple)): |
22 | 28 | raise exceptions.IncorrectTypeException( |
23 | 29 | """ |
24 | | - The {} argument `{}` must be a list or tuple of |
25 | | - `dash.dependencies.{}`s. |
| 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 | + {} |
26 | 35 | """.format( |
27 | | - name.lower(), str(args), name |
| 36 | + repr((outputs + inputs + state)[-1]), repr(extra_args[0]) |
28 | 37 | ) |
29 | 38 | ) |
30 | 39 |
|
31 | | - 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 | + for args in [outputs, inputs, state]: |
| 41 | + for arg in args: |
| 42 | + validate_callback_arg(arg) |
40 | 43 |
|
41 | | - if not isinstance(getattr(arg, "component_property", None), _strings): |
42 | | - raise exceptions.IncorrectTypeException( |
43 | | - """ |
44 | | - component_property must be a string, found {!r} |
45 | | - """.format( |
46 | | - arg.component_property |
47 | | - ) |
48 | | - ) |
49 | 44 |
|
50 | | - if hasattr(arg, "component_event"): |
51 | | - raise exceptions.NonExistentEventException( |
52 | | - """ |
53 | | - Events have been removed. |
54 | | - Use the associated property instead. |
55 | | - """ |
| 45 | +def validate_callback_arg(arg): |
| 46 | + if not isinstance(getattr(arg, "component_property", None), _strings): |
| 47 | + raise exceptions.IncorrectTypeException( |
| 48 | + """ |
| 49 | + component_property must be a string, found {!r} |
| 50 | + """.format( |
| 51 | + arg.component_property |
56 | 52 | ) |
| 53 | + ) |
57 | 54 |
|
58 | | - if isinstance(arg.component_id, dict): |
59 | | - validate_id_dict(arg) |
| 55 | + if hasattr(arg, "component_event"): |
| 56 | + raise exceptions.NonExistentEventException( |
| 57 | + """ |
| 58 | + Events have been removed. |
| 59 | + Use the associated property instead. |
| 60 | + """ |
| 61 | + ) |
60 | 62 |
|
61 | | - elif isinstance(arg.component_id, _strings): |
62 | | - validate_id_string(arg) |
| 63 | + if isinstance(arg.component_id, dict): |
| 64 | + validate_id_dict(arg) |
63 | 65 |
|
64 | | - else: |
65 | | - raise exceptions.IncorrectTypeException( |
66 | | - """ |
67 | | - component_id must be a string or dict, found {!r} |
68 | | - """.format( |
69 | | - arg.component_id |
70 | | - ) |
| 66 | + elif isinstance(arg.component_id, _strings): |
| 67 | + validate_id_string(arg) |
| 68 | + |
| 69 | + else: |
| 70 | + raise exceptions.IncorrectTypeException( |
| 71 | + """ |
| 72 | + component_id must be a string or dict, found {!r} |
| 73 | + """.format( |
| 74 | + arg.component_id |
71 | 75 | ) |
| 76 | + ) |
72 | 77 |
|
73 | 78 |
|
74 | 79 | def validate_id_dict(arg): |
|
0 commit comments