1
0
Эх сурвалжийг харах

inner function (#2494)

* set selector width
resolves #2459

* update dependencies (eslint & config)
date-fns in dll
move inline style to class

* inline-block

* handle inner functions as lambdas
resolves #2287

* inner function is treated as lambda

* inner function is treated as lambda

---------

Co-authored-by: Fred Lefévère-Laoide <Fred.Lefevere-Laoide@Taipy.io>
Fred Lefévère-Laoide 2 сар өмнө
parent
commit
5c56f125a2

+ 1 - 1
taipy/gui/_renderers/builder.py

@@ -171,7 +171,7 @@ class _Builder:
                 if _is_function(val) and not hash_name:
                 if _is_function(val) and not hash_name:
                     # if it's not a callable (and not a string), forget it
                     # if it's not a callable (and not a string), forget it
                     if _is_unnamed_function(val):
                     if _is_unnamed_function(val):
-                        # lambda or callable instance
+                        # lambda, inner function or callable instance
                         hash_name = _get_lambda_id(t.cast(LambdaType, val))
                         hash_name = _get_lambda_id(t.cast(LambdaType, val))
                         gui._bind_var_val(hash_name, val)  # type: ignore[arg-type]
                         gui._bind_var_val(hash_name, val)  # type: ignore[arg-type]
                     else:
                     else:

+ 1 - 1
taipy/gui/builder/_element.py

@@ -108,7 +108,7 @@ class _Element(ABC):
             return value
             return value
         if isinstance(value, FunctionType):
         if isinstance(value, FunctionType):
             if key.startswith("on_") or self._is_callable(key):
             if key.startswith("on_") or self._is_callable(key):
-                return value if value.__name__.startswith("<") else value.__name__
+                return value if "<" in value.__qualname__ else value.__name__
             # Parse lambda function_is_callable
             # Parse lambda function_is_callable
             if (lambda_call := self.__parse_lambda_property(key, value)) is not None:
             if (lambda_call := self.__parse_lambda_property(key, value)) is not None:
                 return lambda_call
                 return lambda_call

+ 5 - 1
taipy/gui/utils/callable.py

@@ -27,4 +27,8 @@ def _function_name(s: t.Any) -> str:
 
 
 
 
 def _is_unnamed_function(s: t.Any):
 def _is_unnamed_function(s: t.Any):
-    return (hasattr(s, "__name__") and s.__name__ == "<lambda>") or (callable(s) and not hasattr(s, "__name__"))
+    return (
+        (hasattr(s, "__name__") and s.__name__ == "<lambda>")
+        or (callable(s) and not hasattr(s, "__name__"))
+        or (hasattr(s, "__qualname__") and "<locals>" in s.__qualname__)
+    )

+ 2 - 1
tests/gui/actions/test_download.py

@@ -64,8 +64,9 @@ def test_download_fn(gui: Gui, helpers):
     helpers.assert_outward_ws_simple_message(
     helpers.assert_outward_ws_simple_message(
         received_messages[0],
         received_messages[0],
         "DF",
         "DF",
-        {"name": "filename.txt", "context": "test_download", "onAction": "tp_on_download_action_0"},
+        {"name": "filename.txt", "context": "test_download"},
     )
     )
+    assert "onAction" in received_messages[0]["args"] # inner function is treated as lambda
 
 
 
 
 def test_bad_download(gui: Gui, helpers):
 def test_bad_download(gui: Gui, helpers):

+ 1 - 1
tests/gui/builder/test_on_action.py

@@ -19,7 +19,7 @@ def test_builder_on_function(gui: Gui, test_client, helpers):
     gui._bind_var_val("on_slider", on_slider)
     gui._bind_var_val("on_slider", on_slider)
     with tgb.Page(frame=None) as page:
     with tgb.Page(frame=None) as page:
         tgb.slider(value="{value}", on_change=on_slider)  # type: ignore[attr-defined] # noqa: B023
         tgb.slider(value="{value}", on_change=on_slider)  # type: ignore[attr-defined] # noqa: B023
-    expected_list = ['<Slider','onChange="on_slider"']
+    expected_list = ['<Slider','onChange="__lambda_'] # inner function is treated as lambda
     helpers.test_control_builder(gui, page, expected_list)
     helpers.test_control_builder(gui, page, expected_list)