pola-rs/polars · warning · KeyError

not found in cache

Error message

{key!r} not found in cache

What it means

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.

Solutions

  1. Check membership first: `if key in cache: del cache[key]`
  2. Use `cache.pop(key, None)` semantics instead of delete (pop supports a default)
  3. Recall the key by re-inserting it before deleting if eviction removed it
  4. Reduce trimming surprises by sizing maxsize generously or tracking eviction

Example fix

// before
del cache["plan:abc"]  # KeyError: 'plan:abc' not found in cache
// after
if "plan:abc" in cache:
    del cache["plan:abc"]
Defensive patterns

Strategy: try-catch

Validate before calling

if key in cache:
    del cache[key]

Type guard

def cache_has(cache, key):
    try:
        return key in cache
    except TypeError:
        return False

Try / catch

try:
    del cache[key]
except KeyError:
    pass  # already evicted/removed

Prevention

When it happens

Trigger: 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().

Common situations: 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.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/99ad020a559e49ff. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/cache.py:65

        >>> cache.get("xyz", "not found")
        'not found'
        """
        self._items: OrderedDict[K, V] = OrderedDict()
        self.maxsize = maxsize

    def __bool__(self) -> bool:
        """Returns True if the cache is not empty, False otherwise."""
        return bool(self._items)

    def __contains__(self, key: Any) -> bool:
        """Check if the key is in the cache."""
        return key in self._items

    def __delitem__(self, key: K) -> None:
        """Remove the item with the specified key from the cache."""
        if key not in self._items:
            msg = f"{key!r} not found in cache"
            raise KeyError(msg)
        del self._items[key]

    def __getitem__(self, key: K) -> V:
        """Raises KeyError if the key is not found."""
        if key not in self._items:
            msg = f"{key!r} not found in cache"
            raise KeyError(msg)

        # moving accessed items to the end marks them as recently used
        self._items.move_to_end(key)
        return self._items[key]

    def __iter__(self) -> Iterator[K]:
        """Iterate over the keys in the cache."""
        yield from self._items

    def __len__(self) -> int:
        """Number of items in the cache."""

View on GitHub (pinned to fe841f959e)