From eeadc40137f4979d44ccd74a35ed05a88d912824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=90=E1=BB=97=20Tr=C6=B0=E1=BB=9Dng=20Giang?= Date: Wed, 11 Jan 2023 09:55:57 +0700 Subject: [PATCH] Refactor/#33 Address mypy issues (#34) * 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 --- src/taipy/config/_base_serializer.py | 2 +- src/taipy/config/checker/_checker.py | 5 +- src/taipy/config/config.py | 4 +- src/taipy/config/config.pyi | 52 +++++++++---------- .../config/global_app/global_app_config.py | 2 +- tests/config/test_section.py | 11 ++++ tests/config/test_section_registration.py | 14 +++-- 7 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/taipy/config/_base_serializer.py b/src/taipy/config/_base_serializer.py index d66a506cf..a931c6502 100644 --- a/src/taipy/config/_base_serializer.py +++ b/src/taipy/config/_base_serializer.py @@ -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): diff --git a/src/taipy/config/checker/_checker.py b/src/taipy/config/checker/_checker.py index 83a1eb227..2c700783d 100644 --- a/src/taipy/config/checker/_checker.py +++ b/src/taipy/config/checker/_checker.py @@ -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): diff --git a/src/taipy/config/config.py b/src/taipy/config/config.py index 1d1a4363c..b15302a9d 100644 --- a/src/taipy/config/config.py +++ b/src/taipy/config/config.py @@ -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 diff --git a/src/taipy/config/config.pyi b/src/taipy/config/config.pyi index 11df2cabc..bbfe92a28 100644 --- a/src/taipy/config/config.pyi +++ b/src/taipy/config/config.pyi @@ -25,15 +25,12 @@ 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): @@ -41,7 +38,6 @@ class Config: Parameters: filename (Union[str, Path]): The path of the toml configuration file to load. """ - @classmethod def export(cls, filename): """Export a configuration. @@ -55,7 +51,6 @@ class Config: Note: If _filename_ already exists, it is overwritten. """ - @classmethod def backup(cls, filename): """Backup a configuration. @@ -71,7 +66,6 @@ class Config: Note: If _filename_ already exists, it is overwritten. """ - @classmethod @_ConfigBlocker._check() def restore(cls, filename): @@ -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): @@ -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, @@ -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] = ..., @@ -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. @@ -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 @@ -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], @@ -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], @@ -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] = ..., @@ -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. @@ -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. diff --git a/src/taipy/config/global_app/global_app_config.py b/src/taipy/config/global_app/global_app_config.py index c0146b7ed..77da07370 100644 --- a/src/taipy/config/global_app/global_app_config.py +++ b/src/taipy/config/global_app/global_app_config.py @@ -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, diff --git a/tests/config/test_section.py b/tests/config/test_section.py index ecb27bf7f..ebc3567c4 100644 --- a/tests/config/test_section.py +++ b/tests/config/test_section.py @@ -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 diff --git a/tests/config/test_section_registration.py b/tests/config/test_section_registration.py index 3283fa7a4..27022fefc 100644 --- a/tests/config/test_section_registration.py +++ b/tests/config/test_section_registration.py @@ -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 @@ -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" @@ -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" @@ -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" @@ -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"