From ee3e224e396494cd0d69bb6167087a071a20349c Mon Sep 17 00:00:00 2001 From: Maxim Devaev Date: Sun, 4 Sep 2022 18:08:40 +0300 Subject: new typing style --- kvmd/yamlconf/__init__.py | 20 ++++++++------------ kvmd/yamlconf/loader.py | 8 +++----- 2 files changed, 11 insertions(+), 17 deletions(-) (limited to 'kvmd/yamlconf') diff --git a/kvmd/yamlconf/__init__.py b/kvmd/yamlconf/__init__.py index d32e5fd3..faf8fa63 100644 --- a/kvmd/yamlconf/__init__.py +++ b/kvmd/yamlconf/__init__.py @@ -23,12 +23,8 @@ import contextlib import json -from typing import Tuple -from typing import List -from typing import Dict from typing import Generator from typing import Callable -from typing import Optional from typing import Any @@ -38,8 +34,8 @@ class ConfigError(ValueError): # ===== -def build_raw_from_options(options: List[str]) -> Dict[str, Any]: - raw: Dict[str, Any] = {} +def build_raw_from_options(options: list[str]) -> dict[str, Any]: + raw: dict[str, Any] = {} for option in options: (key, value) = (option.split("=", 1) + [None])[:2] # type: ignore if len(key.strip()) == 0: @@ -71,12 +67,12 @@ def _parse_value(value: str) -> Any: class Section(dict): def __init__(self) -> None: dict.__init__(self) - self.__meta: Dict[str, Dict[str, Any]] = {} + self.__meta: dict[str, dict[str, Any]] = {} - def _unpack(self, ignore: Optional[List[str]]=None) -> Dict[str, Any]: + def _unpack(self, ignore: (list[str] | None)=None) -> dict[str, Any]: if ignore is None: ignore = [] - unpacked: Dict[str, Any] = {} + unpacked: dict[str, Any] = {} for (key, value) in self.items(): if key not in ignore: if isinstance(value, Section): @@ -118,7 +114,7 @@ class Option: def __init__( self, default: Any, - type: Optional[Callable[[Any], Any]]=None, # pylint: disable=redefined-builtin + type: (Callable[[Any], Any] | None)=None, # pylint: disable=redefined-builtin if_none: Any=Stub, if_empty: Any=Stub, only_if: str="", @@ -150,13 +146,13 @@ def manual_validated(value: Any, *path: str) -> Generator[None, None, None]: raise ConfigError(f"Invalid value {value!r} for key {'/'.join(path)!r}: {err}") -def make_config(raw: Dict[str, Any], scheme: Dict[str, Any], _keys: Tuple[str, ...]=()) -> Section: +def make_config(raw: dict[str, Any], scheme: dict[str, Any], _keys: tuple[str, ...]=()) -> Section: if not isinstance(raw, dict): raise ConfigError(f"The node {('/'.join(_keys) or '/')!r} must be a dictionary") config = Section() - def make_full_key(key: str) -> Tuple[str, ...]: + def make_full_key(key: str) -> tuple[str, ...]: return _keys + (key,) def make_full_name(key: str) -> str: diff --git a/kvmd/yamlconf/loader.py b/kvmd/yamlconf/loader.py index 682d5979..2dedc017 100644 --- a/kvmd/yamlconf/loader.py +++ b/kvmd/yamlconf/loader.py @@ -22,8 +22,6 @@ import os -from typing import List -from typing import Dict from typing import IO from typing import Any @@ -52,7 +50,7 @@ class _YamlLoader(yaml.SafeLoader): self.__root = os.path.dirname(yaml_file.name) def include(self, node: yaml.nodes.Node) -> Any: - incs: List[str] + incs: list[str] if isinstance(node, yaml.nodes.SequenceNode): incs = [ str(child) @@ -63,8 +61,8 @@ class _YamlLoader(yaml.SafeLoader): incs = [str(self.construct_scalar(node))] # type: ignore return self.__inner_include(list(filter(None, incs))) - def __inner_include(self, incs: List[str]) -> Any: - tree: Dict = {} + def __inner_include(self, incs: list[str]) -> Any: + tree: dict = {} for inc in filter(None, incs): assert inc, inc inc_path = os.path.join(self.__root, inc) -- cgit v1.2.3