Coverage for src/gentrie/trie/mutation.py: 100%
9 statements
« prev ^ index » next coverage.py v7.6.10, created at 2025-08-20 09:30 -0700
« prev ^ index » next coverage.py v7.6.10, created at 2025-08-20 09:30 -0700
1# -*- coding: utf-8 -*-
2"""Mutation operations for the trie."""
4from typing import Any
6from ..protocols import GeneralizedKey
7from ..types import TrieId
9from .trie_mixins import TrieMixinsInterface
12class TrieMutationMixin:
13 """Mixin providing mutation operations."""
15 def __setitem__(self: TrieMixinsInterface, key: GeneralizedKey, value: Any) -> None:
16 """Adds or updates an entry in the trie using subscript notation.
18 This is a convenience wrapper for the :meth:`update` method.
20 Example:
21 trie[['ape', 'green', 'apple']] = "A green apple"
23 Args:
24 key (GeneralizedKey): The key for the entry.
25 value (Any): The value to associate with the key.
27 Raises:
28 InvalidGeneralizedKeyError: If key is not a valid :class:`GeneralizedKey`.
29 """
30 self.update(key, value)
32 def __delitem__(self: TrieMixinsInterface, key: TrieId | GeneralizedKey) -> None:
33 """Removes an entry from the trie using subscript notation.
35 This is a convenience wrapper for the :meth:`remove` method.
37 Example:
38 del trie[['ape', 'green', 'apple']]
40 Args:
41 key (TrieId | GeneralizedKey): The identifier for the entry to remove.
43 Raises:
44 KeyError: if the key does not exist in the trie.
45 TypeError: if the key is not a :class:`TrieId` or a valid :class:`GeneralizedKey`.
46 """
47 self.remove(key)