stree: Switch StringTree from Mapping to MutableMapping
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 <jan@janware.com>
This commit is contained in:
parent
4d49b508bc
commit
b4f053c892
1 changed files with 9 additions and 5 deletions
|
|
@ -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'
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue