Parcourir la source

fix function vs method (#1874)

resolves #1864
resolves #1873

Co-authored-by: Fred Lefévère-Laoide <Fred.Lefevere-Laoide@Taipy.io>
Fred Lefévère-Laoide il y a 7 mois
Parent
commit
1bab84ae48
3 fichiers modifiés avec 13 ajouts et 12 suppressions
  1. 9 8
      taipy/gui/_renderers/builder.py
  2. 3 3
      taipy/gui/page.py
  3. 1 1
      taipy/gui_core/_adapters.py

+ 9 - 8
taipy/gui/_renderers/builder.py

@@ -17,7 +17,8 @@ import typing as t
 import xml.etree.ElementTree as etree
 from datetime import date, datetime, time
 from enum import Enum
-from inspect import isclass, isfunction
+from inspect import isclass, isroutine
+from types import LambdaType
 from urllib.parse import quote
 
 from .._warnings import _warn
@@ -139,7 +140,7 @@ class _Builder:
             hash_value = gui._evaluate_expr(value)
             try:
                 func = gui._get_user_function(hash_value)
-                if isfunction(func):
+                if isroutine(func):
                     return (func, hash_value)
                 return (_getscopeattr_drill(gui, hash_value), hash_value)
             except AttributeError:
@@ -164,14 +165,14 @@ class _Builder:
                 else:
                     looks_like_a_lambda = False
                     val = v
-                if isfunction(val):
+                if isroutine(val):
                     # if it's not a callable (and not a string), forget it
                     if val.__name__ == "<lambda>":
                         # if it is a lambda and it has already a hash_name, we're fine
                         if looks_like_a_lambda or not hash_name:
-                            hash_name = _get_lambda_id(val)
+                            hash_name = _get_lambda_id(t.cast(LambdaType, val))
                             gui._bind_var_val(hash_name, val)  # type: ignore[arg-type]
-                    else:
+                    elif not hash_name:
                         hash_name = _get_expr_var_name(val.__name__)
 
                 if val is not None or hash_name:
@@ -349,7 +350,7 @@ class _Builder:
             if not optional:
                 _warn(f"Property {name} is required for control {self.__control_type}.")
             return self
-        elif isfunction(str_attr):
+        elif isroutine(str_attr):
             str_attr = self.__hashes.get(name)
             if str_attr is None:
                 return self
@@ -574,7 +575,7 @@ class _Builder:
         if not isinstance(self.__attributes.get("style"), (type(None), dict, _MapDict)):
             _warn("Table: property 'style' has been renamed to 'row_class_name'.")
         if row_class_name := self.__attributes.get("row_class_name"):
-            if isfunction(row_class_name):
+            if isroutine(row_class_name):
                 value = self.__hashes.get("row_class_name")
             elif isinstance(row_class_name, str):
                 value = row_class_name.strip()
@@ -585,7 +586,7 @@ class _Builder:
             elif value:
                 self.set_attribute("rowClassName", value)
         if tooltip := self.__attributes.get("tooltip"):
-            if isfunction(tooltip):
+            if isroutine(tooltip):
                 value = self.__hashes.get("tooltip")
             elif isinstance(tooltip, str):
                 value = tooltip.strip()

+ 3 - 3
taipy/gui/page.py

@@ -60,12 +60,12 @@ class Page:
             # Extract the page module's attributes and methods
             cls = type(self)
             cls_locals = dict(vars(self))
-            funcs = [
+            functions = [
                 i[0]
                 for i in inspect.getmembers(cls)
-                if not i[0].startswith("_") and (inspect.ismethod(i[1]) or inspect.isfunction(i[1]))
+                if not i[0].startswith("_") and inspect.isroutine(i[1])
             ]
-            for f in funcs:
+            for f in functions:
                 func = getattr(self, f)
                 if hasattr(func, "__func__") and func.__func__ is not None:
                     cls_locals[f] = func.__func__

+ 1 - 1
taipy/gui_core/_adapters.py

@@ -344,7 +344,7 @@ def _get_entity_property(col: str, *types: t.Type):
     col_parts = col.split("(", 2)  # handle the case where the col is a method (ie get_simple_label())
     col_fn = (
         next(
-            (col_parts[0] for i in inspect.getmembers(types[0], predicate=inspect.isfunction) if i[0] == col_parts[0]),
+            (col_parts[0] for i in inspect.getmembers(types[0], predicate=inspect.isroutine) if i[0] == col_parts[0]),
             None,
         )
         if len(col_parts) > 1