diff --git a/src/python/jw/util/Config.py b/src/python/jw/util/Config.py index c71a805..afccf95 100644 --- a/src/python/jw/util/Config.py +++ b/src/python/jw/util/Config.py @@ -5,8 +5,9 @@ import os import re import sys +from collections.abc import Iterator, Mapping from pathlib import Path -from typing import cast +from typing import cast, override from .stree import serdes from .log import DEBUG, ERR, slog, get_caller_pos @@ -16,7 +17,7 @@ from .stree.StringTree import StringTree # world-accessible files that contain password/secret-like content. _REFUSE_MODE_MASK: int = 0o0027 -class Config(): # export +class Config(Mapping): # export def __load( self, @@ -123,12 +124,21 @@ class Config(): # export self.__conf.dump(DEBUG, "superposed configuration") + @override def __getitem__(self, key: str) -> str: ret = self.get(key) if ret is None: raise KeyError(key) return ret + @override + def __iter__(self) -> Iterator[str]: + return iter(self.__conf) + + @override + def __len__(self) -> int: + return len(self.__conf) + def __setitem__(self, key: str, value: str) -> None: self.set(key, value) @@ -145,7 +155,15 @@ class Config(): # export def set(self, key: str, val: str) -> None: self.__conf[key] = val - def get(self, key: str, default: str | None = None) -> str | None: + # Narrowing default and return value from Any to str by overriding + # Mapping.get(). That will work for all callers but isn't accepted by mypy, + # so ignore[override]. + @override + def get( # type: ignore[override] + self, + key: str, + default: str | None = None + ) -> str | None: item = self.__conf.get(key) if item: return item.value()