Config: Derive from Mapping and add @override
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 <jan@janware.com>
This commit is contained in:
parent
ffbb070076
commit
af185c60f9
1 changed files with 21 additions and 3 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in a new issue