{"record":{"id":"99ad020a559e49ff","repo":"pola-rs/polars","slug":"key-r-not-found-in-cache","errorCode":null,"errorMessage":"{key!r} not found in cache","messagePattern":"(.+?) not found in cache","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"warning","filePath":"py-polars/src/polars/_utils/cache.py","lineNumber":65,"sourceCode":"        >>> cache.get(\"xyz\", \"not found\")\n        'not found'\n        \"\"\"\n        self._items: OrderedDict[K, V] = OrderedDict()\n        self.maxsize = maxsize\n\n    def __bool__(self) -> bool:\n        \"\"\"Returns True if the cache is not empty, False otherwise.\"\"\"\n        return bool(self._items)\n\n    def __contains__(self, key: Any) -> bool:\n        \"\"\"Check if the key is in the cache.\"\"\"\n        return key in self._items\n\n    def __delitem__(self, key: K) -> None:\n        \"\"\"Remove the item with the specified key from the cache.\"\"\"\n        if key not in self._items:\n            msg = f\"{key!r} not found in cache\"\n            raise KeyError(msg)\n        del self._items[key]\n\n    def __getitem__(self, key: K) -> V:\n        \"\"\"Raises KeyError if the key is not found.\"\"\"\n        if key not in self._items:\n            msg = f\"{key!r} not found in cache\"\n            raise KeyError(msg)\n\n        # moving accessed items to the end marks them as recently used\n        self._items.move_to_end(key)\n        return self._items[key]\n\n    def __iter__(self) -> Iterator[K]:\n        \"\"\"Iterate over the keys in the cache.\"\"\"\n        yield from self._items\n\n    def __len__(self) -> int:\n        \"\"\"Number of items in the cache.\"\"\"","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/_utils/cache.py#L47-L83","documentation":"Cache.__delitem__ raises KeyError with a '<key>' not found in cache message when asked to delete a key that is absent. Python's `del cache[key]` protocol conventionally raises KeyError, but Polars adds a clearer message identifying the missing key.","triggerScenarios":"Executing `del cache[key]` where key was never inserted or was already evicted/removed by the LRU trimming done in the maxsize setter or popitem().","commonSituations":"Deleting a computed key twice in retry logic; assuming a key still exists after the cache was trimmed by a smaller maxsize; race between check and delete.","solutions":["Check membership first: `if key in cache: del cache[key]`","Use `cache.pop(key, None)` semantics instead of delete (pop supports a default)","Recall the key by re-inserting it before deleting if eviction removed it","Reduce trimming surprises by sizing maxsize generously or tracking eviction"],"exampleFix":"// before\ndel cache[\"plan:abc\"]  # KeyError: 'plan:abc' not found in cache\n// after\nif \"plan:abc\" in cache:\n    del cache[\"plan:abc\"]","handlingStrategy":"try-catch","validationCode":"if key in cache:\n    del cache[key]","typeGuard":"def cache_has(cache, key):\n    try:\n        return key in cache\n    except TypeError:\n        return False","tryCatchPattern":"try:\n    del cache[key]\nexcept KeyError:\n    pass  # already evicted/removed","preventionTips":["Use cache.pop(key, None) instead of membership-then-delete for idempotency","Remember LRU trimming can silently remove keys when maxsize shrinks","Make delete paths idempotent in retry logic","Keep an owning reference to keys you intend to delete later"],"tags":["python","cache","keyerror","lru"],"backgroundTag":"record-not-found","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}