Skip to content

fix(cache): preserve LRUDict.get defaults and LRU order - #8683

Open
Vl4dC wants to merge 1 commit into
aws-powertools:developfrom
Vl4dC:codex/fix-lru-get-default-and-recency
Open

Vl4dC wants to merge 1 commit into
aws-powertools:developfrom
Vl4dC:codex/fix-lru-get-default-and-recency

Conversation

@Vl4dC

@Vl4dC Vl4dC commented Oct 26, 2026

Copy link
Copy Markdown

Issue number: closes #8682

Summary

Changes

  • Check whether the key exists before updating LRUDict recency. This preserves get defaults for missing keys and updates recency for cached falsey values.
  • Add regression tests for both cases.
  • Correct the cache docstring to describe least-recently-used eviction.

User experience

LRUDict.get("missing", "fallback") now returns "fallback" instead of raising KeyError. Reading a cached False, 0, None, or empty string now protects that entry from being evicted as the least recently used item. Existing callers using truthy cached values retain their behavior.

Verification

  • uv run --locked --no-default-groups --with pytest --with ruff --python 3.12 pytest tests/unit/test_lru_cache.py -q — 11 passed, 1 pre-existing skip.
  • Ruff lint and format checks passed for both changed files.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@Vl4dC
Vl4dC requested a review from a team as a code owner October 26, 2026 23:57
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Oct 26, 2026
@boring-cyborg

boring-cyborg Bot commented Oct 26, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

commons size/S Denotes a PR that changes 10-29 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: LRUDict.get raises for missing keys with truthy defaults

1 participant