Browse Source

fixed recursion problem

Toan Quach 9 months ago
parent
commit
c4b622f734

+ 1 - 2
taipy/core/_entity/_properties.py

@@ -31,9 +31,8 @@ class _Properties(UserDict):
         if hasattr(self, "_entity_owner"):
             from ... import core as tp
 
-            entity_owner = tp.get(self._entity_owner)
             try:
-                entity_owner._get_attributes(_validate_id(key), key)
+                self._entity_owner._get_attributes(_validate_id(key), key)
                 raise PropertyKeyAlreadyExisted(key)
             except AttributeError:
                 super(_Properties, self).__setitem__(key, value)

+ 6 - 0
taipy/core/data/data_node.py

@@ -347,6 +347,12 @@ class DataNode(_Entity, _Labeled):
     def __setstate__(self, state):
         vars(self).update(state)
 
+    # def __setattr__(self, name: str, value: Any) -> None:
+    #     return super().__setattr__(name, value)
+
+    def _get_attributes(self, protected_attribute_name, attribute_name):
+        raise AttributeError
+
     def __getattr__(self, attribute_name):
         protected_attribute_name = _validate_id(attribute_name)
         if protected_attribute_name in self._properties:

+ 14 - 29
taipy/core/scenario/scenario.py

@@ -118,6 +118,7 @@ class Scenario(_Entity, Submittable, _Labeled):
     _SEQUENCE_TASKS_KEY = "tasks"
     _SEQUENCE_PROPERTIES_KEY = "properties"
     _SEQUENCE_SUBSCRIBERS_KEY = "subscribers"
+    __CHECK_INIT_DONE_ATTR_NAME = "_init_done"
 
     def __init__(
         self,
@@ -156,6 +157,7 @@ class Scenario(_Entity, Submittable, _Labeled):
             )
 
         self._version = version or _VersionManagerFactory._build_manager()._get_latest_version()
+        self._init_done = True
 
     @staticmethod
     def _new_id(config_id: str) -> ScenarioId:
@@ -178,25 +180,7 @@ class Scenario(_Entity, Submittable, _Labeled):
         return isinstance(other, Scenario) and self.id == other.id
 
     def __setattr__(self, name: str, value: Any) -> None:
-        entity_taipy_attributes = [
-            "_config_id",
-            "id",
-            "_tasks",
-            "_additional_data_nodes",
-            "_creation_date",
-            "_cycle",
-            "_primary_scenario",
-            "_tags",
-            "_properties",
-            "_sequences",
-            "_version",
-            "_submittable_id",
-            "_subscribers",  # from Submittable class
-            "_in_context_attributes_changed_collector",
-            "_is_in_context"  # from _Entity class
-            "name",  # from def name setter
-        ]
-        if name in entity_taipy_attributes:
+        if self.__CHECK_INIT_DONE_ATTR_NAME not in dir(self) or name in dir(self):
             return super().__setattr__(name, value)
         else:
             protected_attribute_name = _validate_id(name)
@@ -204,8 +188,7 @@ class Scenario(_Entity, Submittable, _Labeled):
                 if protected_attribute_name not in self._properties and not self._get_attributes(
                     protected_attribute_name, name
                 ):
-                    raise AttributeError  #   throw AttributeError if found
-
+                    raise AttributeError
                 raise AttributeKeyAlreadyExisted(name)
             except AttributeError:
                 return super().__setattr__(name, value)
@@ -214,10 +197,10 @@ class Scenario(_Entity, Submittable, _Labeled):
         sequences = self._get_sequences()
         if protected_attribute_name in sequences:
             return sequences[protected_attribute_name]
-        tasks = self.tasks
+        tasks = self.__get_tasks()
         if protected_attribute_name in tasks:
             return tasks[protected_attribute_name]
-        data_nodes = self.data_nodes
+        data_nodes = self.__get_data_nodes()
         if protected_attribute_name in data_nodes:
             return data_nodes[protected_attribute_name]
 
@@ -225,9 +208,8 @@ class Scenario(_Entity, Submittable, _Labeled):
 
     def __getattr__(self, attribute_name):
         protected_attribute_name = _validate_id(attribute_name)
-        if hasattr(self, "_properties"):
-            if protected_attribute_name in self._properties:
-                return _tpl._replace_templates(self._properties[protected_attribute_name])
+        if protected_attribute_name in self._properties:
+            return _tpl._replace_templates(self._properties[protected_attribute_name])
 
         return self._get_attributes(protected_attribute_name, attribute_name)
 
@@ -497,14 +479,17 @@ class Scenario(_Entity, Submittable, _Labeled):
     def _get_set_of_tasks(self) -> Set[Task]:
         return set(self.tasks.values())
 
-    @property  # type: ignore
-    @_self_reload(_MANAGER_NAME)
-    def data_nodes(self) -> Dict[str, DataNode]:
+    def __get_data_nodes(self) -> Dict[str, DataNode]:
         data_nodes_dict = self.__get_additional_data_nodes()
         for _, task in self.__get_tasks().items():
             data_nodes_dict.update(task.data_nodes)
         return data_nodes_dict
 
+    @property  # type: ignore
+    @_self_reload(_MANAGER_NAME)
+    def data_nodes(self) -> Dict[str, DataNode]:
+        return self.__get_data_nodes()
+
     @property  # type: ignore
     @_self_reload(_MANAGER_NAME)
     def creation_date(self):

+ 1 - 0
tests/core/scenario/test_scenario.py

@@ -495,6 +495,7 @@ def test_add_rename_and_remove_sequences_within_context(data_node):
     task_1 = Task("task_1", {}, print, output=[data_node])
     task_2 = Task("task_2", {}, print, input=[data_node])
     _TaskManagerFactory._build_manager()._set(task_1)
+    _TaskManagerFactory._build_manager()._set(task_2)
     scenario = Scenario(config_id="scenario", tasks={task_1, task_2}, properties={})
     _ScenarioManagerFactory._build_manager()._set(scenario)