diff --git a/CHANGELOG.md b/CHANGELOG.md index b8df35d816..1ac77d5ff9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](https://semver.org/). - [#4218](https://github.com/plotly/dash/pull/4218) Fix `pages_folder` written with `/` on Windows, such as `"src/pages"`, giving wrong page module names and paths. Fixes [#4217](https://github.com/plotly/dash/issues/4217). - [#4220](https://github.com/plotly/dash/pull/4220) Fix the `DASH_VALIDATE_CALLBACKS` environment variable being ignored, although `Dash.run` and `Dash.enable_dev_tools` document it for `dev_tools_validate_callbacks`. Fixes [#4219](https://github.com/plotly/dash/issues/4219). - [#4235](https://github.com/plotly/dash/pull/4235) Fix FastAPI deep links for mounted Dash apps and disabled or repeated lifespans. Apps sharing a server respect their URL prefixes, and explicit routes take precedence even when added after startup. Fixes [#4234](https://github.com/plotly/dash/issues/4234). +- [#4240](https://github.com/plotly/dash/pull/4240) Fix `flatten_grouping` treating dict and list subclasses such as `OrderedDict` as a single value, which broke callbacks whose output or inputs are an `OrderedDict`, a regression from #3303. ## [4.5.2] - 2026-11-27 diff --git a/dash/_grouping.py b/dash/_grouping.py index 7271d973bb..f886742e2b 100644 --- a/dash/_grouping.py +++ b/dash/_grouping.py @@ -45,13 +45,11 @@ def flatten_grouping(grouping, schema=None): push((grouping, schema)) while stack: group, sch = pop() - # Inline isinstance checks for perf - typ = type(sch) - if typ is tuple or typ is list: + if isinstance(sch, (tuple, list)): # Avoid double recursion / excessive list construction for ge, se in zip(group, sch): push((ge, se)) - elif typ is dict: + elif isinstance(sch, dict): for k in sch: push((group[k], sch[k])) else: diff --git a/tests/unit/library/test_grouped_callbacks.py b/tests/unit/library/test_grouped_callbacks.py index d332ea0561..1b00a9e73f 100644 --- a/tests/unit/library/test_grouped_callbacks.py +++ b/tests/unit/library/test_grouped_callbacks.py @@ -1,3 +1,5 @@ +from collections import OrderedDict + import dash from dash._grouping import make_grouping_by_index, grouping_len, flatten_grouping from dash._utils import create_callback_id @@ -90,6 +92,41 @@ def test_callback_output_size(mixed_grouping_size): check_output_for_grouping(mixed_grouping_size[0]) +def test_callback_output_and_input_dict_subclass(): + app = dash.Dash() + + @app.callback( + output=OrderedDict( + [("x", Output("out-x", "children")), ("y", Output("out-y", "children"))] + ), + inputs=OrderedDict( + [("a", Input("in-a", "value")), ("b", Input("in-b", "value"))] + ), + ) + def fn(a, b): + return {"x": a, "y": b} + + callback_id = create_callback_id( + [Output("out-x", "children"), Output("out-y", "children")], [] + ) + wrapped_fn = app.callback_map[callback_id]["callback"] + result = json.loads( + wrapped_fn( + 1, + 2, + outputs_list=[ + {"id": "out-x", "property": "children"}, + {"id": "out-y", "property": "children"}, + ], + ) + ) + + assert result["response"] == { + "out-x": {"children": 1}, + "out-y": {"children": 2}, + } + + def check_callback_inputs_for_grouping(grouping): """ Check the expected behavior of a callback function configured to input arguments diff --git a/tests/unit/library/test_grouping.py b/tests/unit/library/test_grouping.py index d773f0596e..0abaf034ac 100644 --- a/tests/unit/library/test_grouping.py +++ b/tests/unit/library/test_grouping.py @@ -1,3 +1,5 @@ +from collections import OrderedDict + from dash.dependencies import Input from dash._grouping import ( flatten_grouping, @@ -74,6 +76,17 @@ def test_flatten_odd_value(): assert len(result) == grouping_len(vals_collection) +def test_flatten_dict_subclass(): + grouping = OrderedDict([("a", 0), ("b", (1, 2))]) + result = flatten_grouping(grouping) + assert result == [0, 1, 2] + assert len(result) == grouping_len(grouping) + assert make_grouping_by_index(grouping, ["x", "y", "z"]) == { + "a": "x", + "b": ["y", "z"], + } + + # Test make_grouping_by_position def make_flat_values(size): return list(string.ascii_lowercase[:size])