From af185c60f9e57672c08b356e687a464e99168b54 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Tue, 14 Jul 2026 15:26:23 +0200 Subject: [PATCH] Config: Derive from Mapping and add @override MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Derive Config from collections.abc.Mapping to formally declare it as a read-only mapping interface. Implement the three abstract methods: - __getitem__(): delegates to get(), raises KeyError if missing - __iter__(): iterates over config keys (delegated to __conf) - __len__(): returns number of top-level keys Add @override to __getitem__(), __iter__(), and __len__(). The get() method does not get @override — Mapping.get() uses generic overloads with Any-typed parameters that are incompatible with Config.get()’s specific str-typed signature. While Config.get() is Liskov-safe (contravariant args, covariant return), mypy flags the mismatch on the def line. A targeted "# type: ignore[override]" suppresses only this one false positive without disabling override checking for the rest of the module. Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v Signed-off-by: Jan Lindemann --- src/python/jw/util/Config.py | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) 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()