Skip to content

Commit

Permalink
fix #791: don't try to use Component.keys()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcjohnson committed Jul 13, 2019
1 parent 5132fc5 commit 9a98038
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 9 deletions.
10 changes: 4 additions & 6 deletions dash/dash.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,9 @@ def _validate_callback(self, output, inputs, state):
arg_id = arg.component_id
arg_prop = getattr(arg, 'component_property', None)
if (arg_id not in layout and arg_id != layout_id):
all_ids = [k for k in layout]
if layout_id:
all_ids.append(layout_id)
raise exceptions.NonExistentIdException('''
Attempting to assign a callback to the
component with the id "{0}" but no
Expand All @@ -860,12 +863,7 @@ def _validate_callback(self, output, inputs, state):
(and therefore not in the initial layout), then
you can suppress this exception by setting
`suppress_callback_exceptions=True`.
'''.format(
arg_id,
list(layout.keys()) + (
[layout_id] if layout_id else []
)
).replace(' ', ''))
'''.format(arg_id, all_ids).replace(' ', ''))

component = (
layout if layout_id == arg_id else layout[arg_id]
Expand Down
49 changes: 46 additions & 3 deletions tests/integration/test_integration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from multiprocessing import Value
import datetime
import itertools
import re
import time
import pytest

Expand All @@ -20,7 +18,7 @@
from dash.exceptions import (
PreventUpdate, DuplicateCallbackOutput, CallbackException,
MissingCallbackContextException, InvalidCallbackReturnValue,
IncorrectTypeException
IncorrectTypeException, NonExistentIdException
)
from dash.testing.wait import until

Expand Down Expand Up @@ -898,3 +896,48 @@ def test_inin022_no_callback_context():
for attr in ['inputs', 'states', 'triggered', 'response']:
with pytest.raises(MissingCallbackContextException):
getattr(callback_context, attr)


def test_inin023_wrong_callback_id():
app = Dash(__name__)
app.layout = html.Div([
html.Div([
html.Div(id='inner-div'),
dcc.Input(id='inner-input')
], id='outer-div'),
dcc.Input(id='outer-input')
], id='main')

ids = ['main', 'inner-div', 'inner-input', 'outer-div', 'outer-input']

with pytest.raises(NonExistentIdException) as err:
@app.callback(
Output('nuh-uh', 'children'),
[Input('inner-input', 'value')]
)
def f(a):
return a

assert '"nuh-uh"' in err.value.args[0]
for component_id in ids:
assert component_id in err.value.args[0]

with pytest.raises(NonExistentIdException) as err:
@app.callback(
Output('inner-div', 'children'),
[Input('yeah-no', 'value')]
)
def g(a):
return a

assert '"yeah-no"' in err.value.args[0]
for component_id in ids:
assert component_id in err.value.args[0]

# the right way
@app.callback(
Output('inner-div', 'children'),
[Input('inner-input', 'value')]
)
def h(a):
return a

0 comments on commit 9a98038

Please sign in to comment.