Skip to content

Commit 0ef2b4a

Browse files
committed
improve error message and logic for _set_random_id exclusions
1 parent 41e322b commit 0ef2b4a

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

dash/development/base_component.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -168,32 +168,37 @@ def __init__(self, **kwargs):
168168
setattr(self, k, v)
169169

170170
def _set_random_id(self):
171+
172+
if hasattr(self, "id"):
173+
return getattr(self, "id")
174+
175+
kind = f"`{self._namespace}.{self._type}`" # pylint: disable=no-member
176+
171177
if getattr(self, "persistence", False):
172178
raise RuntimeError(
173-
"""
179+
f"""
174180
Attempting to use an auto-generated ID with the `persistence` prop.
175181
This is prohibited because persistence is tied to component IDs and
176182
auto-generated IDs can easily change.
177183
178-
Please assign an explicit ID to this component.
179-
"""
184+
Please assign an explicit ID to this {kind} component.
185+
"""
180186
)
181187
if "dash_snapshots" in sys.modules:
182188
raise RuntimeError(
183-
"""
189+
f"""
184190
Attempting to use an auto-generated ID in an app with `dash_snapshots`.
185191
This is prohibited because snapshots saves the whole app layout,
186192
including component IDs, and auto-generated IDs can easily change.
187-
Callbacks referencing the new IDs will not work old snapshots.
193+
Callbacks referencing the new IDs will not work with old snapshots.
188194
189-
Please assign an explicit ID to this component.
190-
"""
195+
Please assign an explicit ID to this {kind} component.
196+
"""
191197
)
192198

193-
if not hasattr(self, "id"):
194-
v = str(uuid.UUID(int=rd.randint(0, 2 ** 128)))
195-
setattr(self, "id", v)
196-
return getattr(self, "id")
199+
v = str(uuid.UUID(int=rd.randint(0, 2 ** 128)))
200+
setattr(self, "id", v)
201+
return v
197202

198203
def to_plotly_json(self):
199204
# Add normal properties

tests/unit/development/test_base_component.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,11 @@ def update(v):
526526

527527
assert "persistence" in e.value.args[0]
528528
assert "Please assign an explicit ID" in e.value.args[0]
529+
assert "dash_core_components.Input" in e.value.args[0]
530+
531+
input1.id = "explicit"
532+
533+
# now it works without error
534+
@app.callback(Output(output1, "children"), Input(input1, "value"))
535+
def update2(v):
536+
return f"Input 1 {v}"

0 commit comments

Comments
 (0)