Skip to content

Fix patch operators on the base object. #2859

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
## Fixed

- [#2854](https://github.com/plotly/dash/pull/2854) Fix dcc.Dropdown resetting empty values to null and triggering callbacks. Fixes [#2850](https://github.com/plotly/dash/issues/2850)
- [#2859](https://github.com/plotly/dash/pull/2859) Fix base patch operators. fixes [#2855](https://github.com/plotly/dash/issues/2855)

## [2.17.0] - 2024-05-03

Expand Down
18 changes: 14 additions & 4 deletions dash/_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ def __getstate__(self):
def __setstate__(self, state):
vars(self).update(state)

def __getitem__(self, item):
def __getitem__(self, item) -> "Patch":
validate_slice(item)
return Patch(location=self._location + [item], parent=self)

def __getattr__(self, item):
def __getattr__(self, item) -> "Patch":
if item == "tolist":
# to_json fix
raise AttributeError
if item == "_location":
return self._location
return self._location # type: ignore
if item == "_operations":
return self._operations
return self._operations # type: ignore
return self.__getitem__(item)

def __setattr__(self, key, value):
Expand Down Expand Up @@ -81,22 +81,32 @@ def __iadd__(self, other):
self.extend(other)
else:
self._operations.append(_operation("Add", self._location, value=other))
if not self._location:
return self
return _noop

def __isub__(self, other):
self._operations.append(_operation("Sub", self._location, value=other))
if not self._location:
return self
return _noop

def __imul__(self, other):
self._operations.append(_operation("Mul", self._location, value=other))
if not self._location:
return self
return _noop

def __itruediv__(self, other):
self._operations.append(_operation("Div", self._location, value=other))
if not self._location:
return self
return _noop

def __ior__(self, other):
self.update(E=other)
if not self._location:
return self
return _noop

def __iter__(self):
Expand Down
92 changes: 92 additions & 0 deletions tests/integration/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,95 @@ def test_pch005_clientside_duplicate(dash_duo):

dash_duo.find_element("#click2").click()
dash_duo.wait_for_text_to_equal("#output", "click2")


def test_pch006_base_operators(dash_duo):
app = Dash()

app.layout = [
dcc.Store(data=2, id="store"),
html.Button("add-one", id="add-one"),
html.Button("remove-one", id="remove-one"),
html.Button("mul-two", id="mul-two"),
html.Button("div-two", id="div-two"),
html.Button("merge", id="merge"),
dcc.Store(data={"initial": "initial"}, id="dict-store"),
html.Div(id="store-output"),
html.Div(id="dict-store-output"),
]

@app.callback(
Output("store", "data"), Input("add-one", "n_clicks"), prevent_initial_call=True
)
def on_add(_):
patched = Patch()
patched += 1
return patched

@app.callback(
Output("store", "data", allow_duplicate=True),
Input("remove-one", "n_clicks"),
prevent_initial_call=True,
)
def on_remove(_):
patched = Patch()
patched -= 1
return patched

@app.callback(
Output("store", "data", allow_duplicate=True),
Input("mul-two", "n_clicks"),
prevent_initial_call=True,
)
def on_mul(_):
patched = Patch()
patched *= 2
return patched

@app.callback(
Output("store", "data", allow_duplicate=True),
Input("div-two", "n_clicks"),
prevent_initial_call=True,
)
def on_div(_):
patched = Patch()
patched /= 2
return patched

@app.callback(
Output("dict-store", "data", allow_duplicate=True),
Input("merge", "n_clicks"),
prevent_initial_call=True,
)
def on_merge(_):
patched = Patch()
patched |= {"merged": "merged"}
return patched

app.clientside_callback(
"data => data", Output("store-output", "children"), Input("store", "data")
)
app.clientside_callback(
"data => JSON.stringify(data)",
Output("dict-store-output", "children"),
Input("dict-store", "data"),
)

dash_duo.start_server(app)

dash_duo.find_element("#add-one").click()
dash_duo.wait_for_text_to_equal("#store-output", "3")

dash_duo.find_element("#remove-one").click()
dash_duo.wait_for_text_to_equal("#store-output", "2")

dash_duo.find_element("#mul-two").click()
dash_duo.wait_for_text_to_equal("#store-output", "4")

dash_duo.find_element("#div-two").click()
dash_duo.wait_for_text_to_equal("#store-output", "2")

dash_duo.find_element("#merge").click()
dash_duo.wait_for_text_to_equal(
"#dict-store-output", '{"initial":"initial","merged":"merged"}'
)