From 6d2492c1cff293929b29683a249f25cf4bc2a9ca Mon Sep 17 00:00:00 2001 From: Sven Heidemann Date: Wed, 7 Dec 2022 13:06:50 +0100 Subject: [PATCH] Added generic typing to configuration #141 --- src/cpl_core/configuration/configuration.py | 6 +++--- src/cpl_core/configuration/configuration_abc.py | 8 ++++---- src/cpl_core/typing.py | 3 +++ 3 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 src/cpl_core/typing.py diff --git a/src/cpl_core/configuration/configuration.py b/src/cpl_core/configuration/configuration.py index dbe2574f..b395a826 100644 --- a/src/cpl_core/configuration/configuration.py +++ b/src/cpl_core/configuration/configuration.py @@ -22,6 +22,7 @@ from cpl_core.dependency_injection.service_provider_abc import ServiceProviderAB from cpl_core.environment.application_environment import ApplicationEnvironment from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC from cpl_core.environment.environment_name_enum import EnvironmentNameEnum +from cpl_core.typing import T class Configuration(ConfigurationABC): @@ -267,7 +268,7 @@ class Configuration(ConfigurationABC): configuration.from_dict(value) self.add_configuration(sub, configuration) - def add_configuration(self, key_type: Union[str, type], value: Union[str, ConfigurationModelABC]): + def add_configuration(self, key_type: Union[str, type], value: any): self._config[key_type] = value def create_console_argument(self, arg_type: ArgumentTypeEnum, token: str, name: str, aliases: list[str], @@ -280,8 +281,7 @@ class Configuration(ConfigurationABC): for arg in self._argument_types: call(arg) - def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> \ - Optional[Union[str, ConfigurationModelABC]]: + def get_configuration(self, search_type: Type[T]) -> Optional[T]: if type(search_type) is str: if search_type == ConfigurationVariableNameEnum.environment.value: return self._application_environment.environment_name diff --git a/src/cpl_core/configuration/configuration_abc.py b/src/cpl_core/configuration/configuration_abc.py index caadc15f..4007a1ad 100644 --- a/src/cpl_core/configuration/configuration_abc.py +++ b/src/cpl_core/configuration/configuration_abc.py @@ -6,6 +6,7 @@ from cpl_core.configuration.configuration_model_abc import ConfigurationModelABC from cpl_core.configuration.argument_abc import ArgumentABC from cpl_core.configuration.argument_type_enum import ArgumentTypeEnum from cpl_core.environment.application_environment_abc import ApplicationEnvironmentABC +from cpl_core.typing import T class ConfigurationABC(ABC): @@ -75,14 +76,14 @@ class ConfigurationABC(ABC): pass @abstractmethod - def add_configuration(self, key_type: Union[str, type], value: Union[str, ConfigurationModelABC]): + def add_configuration(self, key_type: Union[str, type], value: any): r"""Add configuration object Parameter --------- key_type: Union[:class:`str`, :class:`type`] Type of the value - value: Union[:class:`str`, :class:`cpl_core.configuration.configuration_model_abc.ConfigurationModelABC`] + value: any Object of the value """ pass @@ -125,8 +126,7 @@ class ConfigurationABC(ABC): pass @abstractmethod - def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Union[ - str, ConfigurationModelABC]: + def get_configuration(self, search_type: Union[str, Type[ConfigurationModelABC]]) -> Optional[T]: r"""Returns value from configuration by given type Parameter diff --git a/src/cpl_core/typing.py b/src/cpl_core/typing.py new file mode 100644 index 00000000..853073b5 --- /dev/null +++ b/src/cpl_core/typing.py @@ -0,0 +1,3 @@ +from typing import TypeVar + +T = TypeVar('T') -- 2.45.2