Ver Fonte

added _check_required_properties for task config checker

Toan Quach há 5 meses atrás
pai
commit
eea7548769

+ 17 - 0
taipy/core/config/checkers/_task_config_checker.py

@@ -21,6 +21,9 @@ from ..task_config import TaskConfig
 
 
 class _TaskConfigChecker(_ConfigChecker):
+    # NOTE: # {task_type: ["required_key1"]}
+    _REQUIRED_PROPERTIES: Dict[str, List[str]] = {}
+
     def __init__(self, config: _Config, collector: IssueCollector):
         super().__init__(config, collector)
 
@@ -41,6 +44,7 @@ class _TaskConfigChecker(_ConfigChecker):
                 self._check_inputs(task_config_id, task_config)
                 self._check_outputs(task_config_id, task_config)
                 self._check_if_children_config_id_is_overlapping_with_properties(task_config_id, task_config)
+                self._check_required_properties(task_config_id, task_config)
         return self._collector
 
     def _check_if_children_config_id_is_overlapping_with_properties(self, task_config_id: str, task_config: TaskConfig):
@@ -88,3 +92,16 @@ class _TaskConfigChecker(_ConfigChecker):
                 f"{task_config._FUNCTION} field of TaskConfig `{task_config_id}` must be"
                 f" populated with Callable value.",
             )
+
+    def _check_required_properties(self, task_config_id: str, task_config: TaskConfig):
+        task_config_properties = task_config.properties
+        for task_type, required_keys in self._REQUIRED_PROPERTIES.items():
+            if task_config_properties.get(task_type, False):
+                for required_key in required_keys:
+                    if task_config_properties.get(required_key, None) is None:
+                        self._error(
+                            required_key,
+                            None,
+                            f"TaskConfig `{task_config_id}` is either missing the required "
+                            f"property `{required_key}` or the value is set to None.",
+                        )

+ 37 - 0
tests/core/config/checkers/test_task_config_checker.py

@@ -311,3 +311,40 @@ class TestTaskConfigChecker:
         Config.check()
         assert len(Config._collector.errors) == 0
         assert len(Config._collector.warnings) == 2
+
+    def test_check_required_property(self, caplog):
+        from taipy.core.config.checkers._task_config_checker import _TaskConfigChecker
+
+        prev_required_properties = _TaskConfigChecker._REQUIRED_PROPERTIES.copy()
+
+        _TaskConfigChecker._REQUIRED_PROPERTIES = {"task_type": ["required_key_1", "required_key_2"]}
+
+        config = Config._applied_config
+        Config._compile_configs()
+
+        config._sections[TaskConfig.name]["default"]._outputs = "bar"
+        Config._collector = IssueCollector()
+        Config.check()
+        assert len(Config._collector.errors) == 0
+        assert len(Config._collector.warnings) == 0
+
+        config._sections[TaskConfig.name]["new"] = config._sections[TaskConfig.name]["default"]
+        config._sections[TaskConfig.name]["new"].id = "new"
+        config._sections[TaskConfig.name]["new"].function = print
+        config._sections[TaskConfig.name]["new"]._outputs = [DataNodeConfig("bar")]
+        config._sections[TaskConfig.name]["new"]._properties = {"task_type": True, "required_key_1": None}
+        with pytest.raises(SystemExit):
+            Config._collector = IssueCollector()
+            Config.check()
+        assert len(Config._collector.errors) == 2
+        expected_error_message_1 = (
+            "TaskConfig `new` is either missing the required property " "`required_key_1` or the value is set to None."
+        )
+        assert expected_error_message_1 in caplog.text
+        expected_error_message_2 = (
+            "TaskConfig `new` is either missing the required property " "`required_key_2` or the value is set to None."
+        )
+        assert expected_error_message_2 in caplog.text
+        assert len(Config._collector.warnings) == 1
+
+        _TaskConfigChecker._REQUIRED_PROPERTIES = prev_required_properties