Type Annotations: core.config

- Resolves Issue #694
- Related to PR #628
This commit is contained in:
BJ Dierkes 2024-06-23 20:30:26 -05:00
parent 0376695cd8
commit 3c750a16ce
2 changed files with 17 additions and 12 deletions

View File

@ -25,6 +25,7 @@ Refactoring:
- Type Annotations (related: [PR #628](https://github.com/datafolklabs/cement/pull/628))
- `[core.arg]` [Issue #692](https://github.com/datafolklabs/cement/issues/692)
- `[core.cache]` [Issue #693](https://github.com/datafolklabs/cement/issues/693)
- `[core.config]` [Issue #694](https://github.com/datafolklabs/cement/issues/694)
- `[core.exc]` [Issue #697](https://github.com/datafolklabs/cement/issues/697)
- `[core.interface]` [Issue #702](https://github.com/datafolklabs/cement/issues/702)
- `[core.meta]` [Issue #705](https://github.com/datafolklabs/cement/issues/705)

View File

@ -2,6 +2,7 @@
import os
from abc import abstractmethod
from typing import Any, Dict, List
from ..core.interface import Interface
from ..core.handler import Handler
from ..utils.fs import abspath
@ -27,7 +28,7 @@ class ConfigInterface(Interface):
interface = 'config'
@abstractmethod
def parse_file(self, file_path):
def parse_file(self, file_path: str) -> bool:
"""
Parse config file settings from ``file_path``. Returns True if the
file existed, and was parsed successfully. Returns False otherwise.
@ -42,7 +43,7 @@ class ConfigInterface(Interface):
pass # pragma: nocover
@abstractmethod
def keys(self, section):
def keys(self, section: str) -> List[str]:
"""
Return a list of configuration keys from ``section``.
@ -56,7 +57,7 @@ class ConfigInterface(Interface):
pass # pragma: nocover
@abstractmethod
def get_sections(self):
def get_sections(self) -> List[str]:
"""
Return a list of configuration sections.
@ -67,7 +68,7 @@ class ConfigInterface(Interface):
pass # pragma: nocover
@abstractmethod
def get_dict(self):
def get_dict(self) -> Dict[str, Any]:
"""
Return a dict of the entire configuration.
@ -77,7 +78,7 @@ class ConfigInterface(Interface):
"""
@abstractmethod
def get_section_dict(self, section):
def get_section_dict(self, section: str) -> Dict[str, Any]:
"""
Return a dict of configuration parameters for ``section``.
@ -92,7 +93,7 @@ class ConfigInterface(Interface):
pass # pragma: nocover
@abstractmethod
def add_section(self, section):
def add_section(self, section: str) -> None:
"""
Add a new section if it doesn't already exist.
@ -106,7 +107,7 @@ class ConfigInterface(Interface):
pass # pragma: nocover
@abstractmethod
def get(self, section, key):
def get(self, section: str, key: str) -> Any:
"""
Return a configuration value based on ``section.key``. Must honor
environment variables if they exist to override the config... for
@ -129,7 +130,7 @@ class ConfigInterface(Interface):
pass # pragma: nocover
@abstractmethod
def set(self, section, key, value):
def set(self, section: str, key: str, value: Any) -> None:
"""
Set a configuration value based at ``section.key``.
@ -146,7 +147,7 @@ class ConfigInterface(Interface):
pass # pragma: nocover
@abstractmethod
def merge(self, dict_obj, override=True):
def merge(self, dict_obj: dict, override: bool = True) -> None:
"""
Merges a dict object into the configuration.
@ -161,7 +162,7 @@ class ConfigInterface(Interface):
pass # pragma: nocover
@abstractmethod
def has_section(self, section):
def has_section(self, section: str) -> bool:
"""
Returns whether or not the section exists.
@ -183,8 +184,11 @@ class ConfigHandler(ConfigInterface, Handler):
"""
class Meta:
pass # pragma: nocover
@abstractmethod
def _parse_file(self, file_path):
def _parse_file(self, file_path: str) -> bool:
"""
Parse a configuration file at ``file_path`` and store it. This
function must be provided by the handler implementation (that is
@ -199,7 +203,7 @@ class ConfigHandler(ConfigInterface, Handler):
"""
pass # pragma: nocover
def parse_file(self, file_path):
def parse_file(self, file_path: str) -> bool:
"""
Ensure we are using the absolute/expanded path to ``file_path``, and
then call ``self._parse_file`` to parse config file settings from it,