Skip to content

Commit

Permalink
Refactor/#33 Address mypy issues (#34)
Browse files Browse the repository at this point in the history
* fix: minor mypy issue

* fix: update injected config methods as staticmethod

* fix: add _register_default to .pyi to avoid error: "Type[Config]" has no attribute "_register_default"

* fix: raise instead of return NotImplementedError
  • Loading branch information
trgiangdo committed Jan 11, 2023
1 parent 01a8bf3 commit eeadc40
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/taipy/config/_base_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class _BaseSerializer(object):
@classmethod
@abstractmethod
def _write(cls, configuration: _Config, filename: str):
return NotImplementedError
raise NotImplementedError

@classmethod
def _str(cls, configuration: _Config):
Expand Down
5 changes: 3 additions & 2 deletions src/taipy/config/checker/_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@


class _Checker:
"""holds the various checkers to perform on the config."""
_checkers: List[_ConfigChecker] = [_GlobalConfigChecker]
"""Holds the various checkers to perform on the config."""

_checkers: List[_ConfigChecker] = [_GlobalConfigChecker] # type: ignore

@classmethod
def _check(cls, _applied_config):
Expand Down
4 changes: 2 additions & 2 deletions src/taipy/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ def _register_default(cls, default_section: Section):
def_sections[default_section.id] = default_section
else:
cls._default_config._sections[default_section.name] = {default_section.id: default_section}
cls._serializer._section_class[default_section.name] = default_section.__class__
cls.__json_serializer._section_class[default_section.name] = default_section.__class__
cls._serializer._section_class[default_section.name] = default_section.__class__ # type: ignore
cls.__json_serializer._section_class[default_section.name] = default_section.__class__ # type: ignore
cls.__compile_configs()

@classmethod
Expand Down
52 changes: 25 additions & 27 deletions src/taipy/config/config.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,19 @@ class Config:
@_Classproperty
def unique_sections(cls) -> Dict[str, UniqueSection]:
"""Return all unique sections."""

@_Classproperty
def sections(cls) -> Dict[str, Dict[str, Section]]:
"""Return all non unique sections."""

@_Classproperty
def global_config(cls) -> GlobalAppConfig:
"""Return configuration values related to the global application as a `GlobalAppConfig^`."""

@classmethod
@_ConfigBlocker._check()
def load(cls, filename):
"""Load a configuration file to replace the current python config and trigger the Config compilation.
Parameters:
filename (Union[str, Path]): The path of the toml configuration file to load.
"""

@classmethod
def export(cls, filename):
"""Export a configuration.
Expand All @@ -55,7 +51,6 @@ class Config:
Note:
If _filename_ already exists, it is overwritten.
"""

@classmethod
def backup(cls, filename):
"""Backup a configuration.
Expand All @@ -71,7 +66,6 @@ class Config:
Note:
If _filename_ already exists, it is overwritten.
"""

@classmethod
@_ConfigBlocker._check()
def restore(cls, filename):
Expand All @@ -80,7 +74,6 @@ class Config:
Parameters:
filename (Union[str, Path]): The path of the toml configuration file to load.
"""

@classmethod
@_ConfigBlocker._check()
def override(cls, filename):
Expand All @@ -89,15 +82,20 @@ class Config:
Parameters:
filename (Union[str, Path]): The path of the toml configuration file to load.
"""

@classmethod
def block_update(cls):
"""Block update on the configuration signgleton."""

@classmethod
def unblock_update(cls):
"""Unblock update on the configuration signgleton."""

@classmethod
@_ConfigBlocker._check()
def _register_default(cls, default_section: Section):
""""""
@classmethod
@_ConfigBlocker._check()
def _register(cls, section):
""""""
@classmethod
def configure_global_app(
cls,
Expand Down Expand Up @@ -473,7 +471,7 @@ class Config:
Returns:
`DataNodeConfig^`: The new Generic data node configuration.
"""
@classmethod
@staticmethod
def configure_job_executions(
mode: str = ...,
nb_of_workers: Union[int, str] = ...,
Expand All @@ -494,8 +492,8 @@ class Config:
Returns:
`JobConfig^`: The job execution configuration.
"""
@classmethod
def configure_pipeline(id: str, task_configs: Union[TaskConfig, List[TaskConfig]], **properties: Dict[str, Any]):
@staticmethod
def configure_pipeline(id: str, task_configs: Union[TaskConfig, List[TaskConfig]], **properties: Dict[str, Any]): # type: ignore
"""Configure a new pipeline configuration.
Parameters:
id (str): The unique identifier of the new pipeline configuration.
Expand All @@ -507,8 +505,8 @@ class Config:
Returns:
`PipelineConfig^`: The new pipeline configuration.
"""
@classmethod
def configure_default_pipeline(task_configs: Union[TaskConfig, List[TaskConfig]], **properties: Dict[str, Any]):
@staticmethod
def configure_default_pipeline(task_configs: Union[TaskConfig, List[TaskConfig]], **properties: Dict[str, Any]): # type: ignore
"""Configure the default values for pipeline configurations.
This function creates the _default pipeline configuration_ object,
where all pipeline configuration objects will find their default
Expand All @@ -522,10 +520,10 @@ class Config:
Returns:
`PipelineConfig^`: The default pipeline configuration.
"""
@classmethod
@staticmethod
def configure_scenario(
id: str,
pipeline_configs: List[PipelineConfig],
pipeline_configs: List[PipelineConfig], # type: ignore
frequency: Optional[Frequency] = ...,
comparators: Optional[Dict[str, Union[List[Callable], Callable]]] = ...,
**properties: Dict[str, Any],
Expand All @@ -551,9 +549,9 @@ class Config:
Returns:
`ScenarioConfig^`: The new scenario configuration.
"""
@classmethod
@staticmethod
def configure_default_scenario(
pipeline_configs: List[PipelineConfig],
pipeline_configs: List[PipelineConfig], # type: ignore
frequency: Optional[Frequency] = ...,
comparators: Optional[Dict[str, Union[List[Callable], Callable]]] = ...,
**properties: Dict[str, Any],
Expand Down Expand Up @@ -581,10 +579,10 @@ class Config:
Returns:
`ScenarioConfig^`: The default scenario configuration.
"""
@classmethod
@staticmethod
def configure_scenario_from_tasks(
id: str,
task_configs: List[TaskConfig],
task_configs: List[TaskConfig], # type: ignore
frequency: Optional[Frequency] = ...,
comparators: Optional[Dict[str, Union[List[Callable], Callable]]] = ...,
pipeline_id: Optional[str] = ...,
Expand Down Expand Up @@ -616,12 +614,12 @@ class Config:
Returns:
`ScenarioConfig^`: The new scenario configuration.
"""
@classmethod
@staticmethod
def configure_task(
id: str,
function: Callable,
input: Optional[Union[DataNodeConfig, List[DataNodeConfig]]] = ...,
output: Optional[Union[DataNodeConfig, List[DataNodeConfig]]] = ...,
input: Optional[Union[DataNodeConfig, List[DataNodeConfig]]] = ..., # type: ignore
output: Optional[Union[DataNodeConfig, List[DataNodeConfig]]] = ..., # type: ignore
**properties: Dict[str, Any],
):
"""Configure a new task configuration.
Expand All @@ -639,11 +637,11 @@ class Config:
Returns:
`TaskConfig^`: The new task configuration.
"""
@classmethod
@staticmethod
def configure_default_task(
function: Callable,
input: Optional[Union[DataNodeConfig, List[DataNodeConfig]]] = ...,
output: Optional[Union[DataNodeConfig, List[DataNodeConfig]]] = ...,
input: Optional[Union[DataNodeConfig, List[DataNodeConfig]]] = ..., # type: ignore
output: Optional[Union[DataNodeConfig, List[DataNodeConfig]]] = ..., # type: ignore
**properties: Dict[str, Any],
):
"""Configure the default values for task configurations.
Expand Down
2 changes: 1 addition & 1 deletion src/taipy/config/global_app/global_app_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class GlobalAppConfig:
_REPOSITORY_TYPE_KEY = "repository_type"
_DEFAULT_REPOSITORY_TYPE = "filesystem"
_REPOSITORY_PROPERTIES_KEY = "repository_properties"
_DEFAULT_REPOSITORY_PROPERTIES = dict()
_DEFAULT_REPOSITORY_PROPERTIES: Dict = dict()

def __init__(
self,
Expand Down
11 changes: 11 additions & 0 deletions tests/config/test_section.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Copyright 2022 Avaiga Private Limited
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
# specific language governing permissions and limitations under the License.

import os
from unittest import mock

Expand Down
14 changes: 6 additions & 8 deletions tests/config/test_section_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_section_registration_and_usage():
assert Config.sections[SectionForTest.name]["default"] is not None
assert Config.sections[SectionForTest.name]["default"].attribute == "default_attribute"
assert Config.sections[SectionForTest.name]["default"].prop == "default_prop"
assert Config.sections[SectionForTest.name]["default"].foo == None
assert Config.sections[SectionForTest.name]["default"].foo is None

myFirstSection = Config.configure_section_for_tests(id="first", attribute="my_attribute", prop="my_prop", foo="bar")
assert Config.sections is not None
Expand All @@ -75,7 +75,7 @@ def test_section_registration_and_usage():
assert Config.sections[SectionForTest.name]["default"] is not None
assert Config.sections[SectionForTest.name]["default"].attribute == "default_attribute"
assert Config.sections[SectionForTest.name]["default"].prop == "default_prop"
assert Config.sections[SectionForTest.name]["default"].foo == None
assert Config.sections[SectionForTest.name]["default"].foo is None
assert Config.sections[SectionForTest.name]["first"] is not None
assert Config.sections[SectionForTest.name]["first"].attribute == "my_attribute"
assert Config.sections[SectionForTest.name]["first"].prop == "my_prop"
Expand All @@ -92,7 +92,7 @@ def test_section_registration_and_usage():
assert Config.sections[SectionForTest.name]["default"] is not None
assert Config.sections[SectionForTest.name]["default"].attribute == "default_attribute"
assert Config.sections[SectionForTest.name]["default"].prop == "default_prop"
assert Config.sections[SectionForTest.name]["default"].foo == None
assert Config.sections[SectionForTest.name]["default"].foo is None
assert Config.sections[SectionForTest.name]["first"] is not None
assert Config.sections[SectionForTest.name]["first"].attribute == "my_attribute"
assert Config.sections[SectionForTest.name]["first"].prop == "my_prop"
Expand All @@ -116,7 +116,7 @@ def test_section_registration_and_usage():
assert Config.sections[SectionForTest.name]["default"] is not None
assert Config.sections[SectionForTest.name]["default"].attribute == "default_attribute"
assert Config.sections[SectionForTest.name]["default"].prop == "default_prop"
assert Config.sections[SectionForTest.name]["default"].foo == None
assert Config.sections[SectionForTest.name]["default"].foo is None
assert Config.sections[SectionForTest.name]["first"] is not None
assert Config.sections[SectionForTest.name]["first"].attribute == "my_attribute"
assert Config.sections[SectionForTest.name]["first"].prop == "my_prop"
Expand All @@ -143,12 +143,10 @@ def test_block_registration():
Config.block_update()

with pytest.raises(ConfigurationUpdateBlocked):
myNewUniqueSection = Config.configure_unique_section_for_tests(
attribute="my_new_unique_attribute", prop="my_new_unique_prop"
)
Config.configure_unique_section_for_tests(attribute="my_new_unique_attribute", prop="my_new_unique_prop")

with pytest.raises(ConfigurationUpdateBlocked):
myNewSection = Config.configure_section_for_tests(id="new", attribute="my_attribute", prop="my_prop", foo="bar")
Config.configure_section_for_tests(id="new", attribute="my_attribute", prop="my_prop", foo="bar")

with pytest.raises(ConfigurationUpdateBlocked):
myUniqueSection.attribute = "foo"
Expand Down

0 comments on commit eeadc40

Please sign in to comment.