Skip to content

Commit

Permalink
Merge pull request #2335 from Avaiga/feature/#576-add-check-required-…
Browse files Browse the repository at this point in the history
…properties-to-task-config-checker

feature/enterprise/#576 added _check_required_properties for task config checker
  • Loading branch information
toan-quach authored Dec 18, 2024
2 parents 0ce3c0d + a268b53 commit 11ff277
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
14 changes: 14 additions & 0 deletions taipy/core/config/checkers/_task_config_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def _check(self) -> IssueCollector:
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):
Expand Down Expand Up @@ -88,3 +89,16 @@ def _check_existing_function(self, task_config_id: str, task_config: TaskConfig)
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 TaskConfig._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 property "
f"`{required_key}` or the value is set to None.",
)
3 changes: 3 additions & 0 deletions taipy/core/config/task_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class TaskConfig(Section):
(*exposed_type* field) of the input data nodes and returning results compatible with the
data type (*exposed_type* field) of the outputs list."""

# NOTE: # {task_type: ["required_key1"]}
_REQUIRED_PROPERTIES: Dict[str, List[str]] = {}

def __init__(
self,
id: str,
Expand Down
48 changes: 48 additions & 0 deletions tests/core/config/checkers/test_task_config_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,51 @@ def mock_func():
Config.check()
assert len(Config._collector.errors) == 0
assert len(Config._collector.warnings) == 2

def test_check_required_property(self, caplog):
prev_required_properties = TaskConfig._REQUIRED_PROPERTIES.copy()

TaskConfig._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

TaskConfig._REQUIRED_PROPERTIES = prev_required_properties

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": "sthg",
"required_key_2": "sthg",
}
Config._collector = IssueCollector()
Config.check()
assert len(Config._collector.errors) == 0

0 comments on commit 11ff277

Please sign in to comment.