Skip to content

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

Closed

Description

@Vl4dC

Expected Behaviour

LRUDict.get("missing", "fallback") returns "fallback", matching dict.get and OrderedDict.get. Reading an existing entry should make it recently used even when its value is falsey.

Current Behaviour

A missing key with a truthy default raises KeyError because get calls move_to_end on the missing key. An existing entry containing False, 0, or None is returned without updating its recency, so it can be evicted immediately after being read.

Code snippet

from aws_lambda_powertools.shared.cache_dict import LRUDict

cache = LRUDict(max_items=2)
cache.get("missing", "fallback")  # Raises KeyError: 'missing'

cache["false_value"] = False
cache["other"] = 1
cache.get("false_value")
cache["new"] = 2
assert "false_value" in cache  # Fails: the recently read key was evicted

Steps to Reproduce

Run the snippet against the current develop branch (5785377) on Python 3.12. The first expression raises KeyError; if skipped, the final assertion fails.

Possible Solution

Check whether the key exists before moving it to the end. Preserve the normal get return behavior for a missing key and its optional default.

Environment

  • Powertools for AWS Lambda (Python): source checkout at 5785377 (develop)
  • Python: 3.12
  • Packaging: source checkout; no Lambda runtime or AWS service is needed

LRUDict is an internal shared cache, so the current impact depends on how callers use get. This report concerns its mapping and recency behavior.

Activity

  1. boring-cyborg commented on Oct 26, 2026

    @boring-cyborg

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

  2. powertools-for-aws-oss-automation commented on Oct 26, 2026

    @powertools-for-aws-oss-automation

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out our GitHub Discussions: Join the conversation

  3. added this to the 3.39.0 milestone on Oct 28, 2026
  4. powertools-for-aws-oss-automation commented on Oct 29, 2026

    @powertools-for-aws-oss-automation

    Warning

    This issue is now closed. Please be mindful that future comments are hard for our team to see.
    If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

  5. github-actions commented on Nov 3, 2026

    @github-actions
    Contributor

    This is now released under 3.39.0 version!

  6. removed
    pending-releaseFix or implementation already in dev waiting to be released
    on Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions