From b4f053c892ddef1a30d35d8458d5ef2dedf98dfb Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Mon, 13 Jul 2026 13:20:52 +0200 Subject: [PATCH] stree: Switch StringTree from Mapping to MutableMapping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit StringTree declares itself as a Mapping but implements __setitem__ which returns StringTree — neither correct for Mapping nor for MutableMapping. This commit changes the base class to MutableMapping and fixes the signatures to match the ABC contract: __setitem__ now returns None, and __delitem__ is implemented by delegating to the underlying OrderedDict. The __setitem__ return value change is an API break for callers that rely on its StringTree return. Those callers should use add() instead. This is acceptable because the previous type signature was silently incorrect — it violated the Mapping abstract base class contract. Assisted-by: unsloth/Qwen3.6-35B-A3B-GGUF:IQ4_NL with pi.dev v0.80.2 Signed-off-by: Jan Lindemann --- src/python/jw/util/stree/StringTree.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/python/jw/util/stree/StringTree.py b/src/python/jw/util/stree/StringTree.py index d196a28..df7e188 100644 --- a/src/python/jw/util/stree/StringTree.py +++ b/src/python/jw/util/stree/StringTree.py @@ -4,7 +4,7 @@ import fnmatch import re from collections import OrderedDict -from collections.abc import Mapping +from collections.abc import MutableMapping from enum import Enum, auto from typing import TYPE_CHECKING @@ -55,7 +55,7 @@ def cleanup_string(s: str | StringTree) -> str | StringTree: return s[1:-1].replace('\\' + s[0], s[0]) return s -class StringTree(Mapping): # export +class StringTree(MutableMapping): # export class Error(Exception): pass @@ -190,12 +190,16 @@ class StringTree(Mapping): # export raise self.Error(path) return ret - def __setitem__(self, key: str, value: str) -> StringTree: - return self.__set(key, value) + def __setitem__(self, key: str, value: str) -> None: + self.__set(key, value) def __iter__(self) -> Iterator[str]: return iter(self.children) + def __delitem__(self, key: str) -> None: + if key in self.children: + del self.children[key] + def __len__(self) -> int: return len(self.children) @@ -282,7 +286,7 @@ class StringTree(Mapping): # export def child_list(self, depth_first: bool = True) -> list[StringTree]: if not depth_first: - raise Exception( + raise self.Error( 'Tried to retrieve child list with breadth-first ' 'search, not yet implemented' )