From 48c86e9556ddbe8081974a7bafcb0d00c181921b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20Lef=C3=A9v=C3=A8re-Laoide?= <90181748+FredLL-Avaiga@users.noreply.github.com> Date: Mon, 14 Oct 2024 11:43:27 +0200 Subject: [PATCH] on_action params grouped (#2047) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * on_action params grouped resolves #2045 * with tests --------- Co-authored-by: Fred Lefévère-Laoide --- taipy/gui/gui.py | 4 ++-- tests/gui/gui_specific/test_gui.py | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/taipy/gui/gui.py b/taipy/gui/gui.py index d888aa44e3..52d4b31100 100644 --- a/taipy/gui/gui.py +++ b/taipy/gui/gui.py @@ -1506,7 +1506,7 @@ def __on_action(self, id: t.Optional[str], payload: t.Any) -> None: return else: # pragma: no cover _warn(f"on_action(): '{action}' is not a valid function.") - if hasattr(self, "on_action"): + if getattr(self, "on_action", None) is not None: self.__call_function_with_args(action_function=self.on_action, id=id, payload=payload) def __call_function_with_args(self, **kwargs): @@ -1520,7 +1520,7 @@ def __call_function_with_args(self, **kwargs): args = [self._get_real_var_name(id)[0], payload] except Exception: args = [id, payload] - self._call_function_with_state(t.cast(t.Callable, action_function), [args]) + self._call_function_with_state(t.cast(t.Callable, action_function), args) return True except Exception as e: # pragma: no cover if not self._call_on_exception(action_function, e): diff --git a/tests/gui/gui_specific/test_gui.py b/tests/gui/gui_specific/test_gui.py index 60b8e7a983..01dfd8e219 100644 --- a/tests/gui/gui_specific/test_gui.py +++ b/tests/gui/gui_specific/test_gui.py @@ -89,3 +89,24 @@ def test__get_valid_adapter_result(gui: Gui): res = gui._get_valid_adapter_result(("id", "label")) assert isinstance(res, tuple) assert res[0] == "id" + +def test_on_action_call(gui:Gui): + an_id = "my_id" + + a_non_action_payload = {"a": "b"} + def on_action(state, id, payload): + assert id == an_id + assert payload is a_non_action_payload + + an_action_payload = {"action": "on_an_action"} + def on_an_action(state, id, payload): + assert id == an_id + assert payload is an_action_payload + + # set gui frame + gui._set_frame(inspect.currentframe()) + + gui.run(run_server=False) + with gui.get_flask_app().app_context(): + gui._Gui__on_action(an_id, a_non_action_payload) # type: ignore[reportAttributeAccessIssue] + gui._Gui__on_action(an_id, an_action_payload) # type: ignore[reportAttributeAccessIssue]