Skip to content

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

Merged
leandrodamascena merged 2 commits into
aws-powertools:developfrom
Vl4dC:codex/fix-lru-get-default-and-recency
Oct 29, 2026
Merged

leandrodamascena merged 2 commits into
aws-powertools:developfrom
Vl4dC:codex/fix-lru-get-default-and-recency

Conversation

@Vl4dC

@Vl4dC Vl4dC commented Oct 26, 2026

Copy link
Copy Markdown
Contributor

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

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Vl4dC, and welcome. Confirmed both halves on develop: a missing key with a truthy default raised KeyError, and a falsy cached value lost its recency and got evicted right after being read. key in self is the right check, and the docstring fix is welcome too. Our current callers don't hit either case, but the contract was wrong and your tests pin it. Approving, this goes out in 3.39.0, no rebase needed, and you'll be credited in the release notes.

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.99%. Comparing base (486e484) to head (9b29655).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8683   +/-   ##
========================================
  Coverage    96.99%   96.99%           
========================================
  Files          321      321           
  Lines        16332    16332           
  Branches      1473     1473           
========================================
  Hits         15841    15841           
  Misses         350      350           
  Partials       141      141           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
leandrodamascena merged commit 523504d into aws-powertools:develop Oct 29, 2026
14 checks passed
@boring-cyborg

boring-cyborg Bot commented Oct 29, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@powertools-for-aws-oss-automation

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

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

2 participants