Skip to content

Tech debt: Fix falsy response handling, inconsistencies, and duplicated code in idempotency utility #8090

Description

@leandrodamascena

Why is this needed?

Found a few issues during code review of the idempotency utility:

1. Falsy responses are silently discarded (base.py:299, 273-274)

if response else None treats valid falsy return values (0, False, {}, [], "") as None. This means a function returning 0 or False won't have its response stored, and on replay it will re-execute instead of returning the cached value.

# Current
serialized_response: dict = self.output_serializer.to_dict(response) if response else None

# Should be
serialized_response: dict = self.output_serializer.to_dict(response) if response is not None else None

Same issue on line 273-274 when reading cached responses.

2. Inconsistent status constant usage (base.py:170)

Uses string literal "INPROGRESS" instead of STATUS_CONSTANTS["INPROGRESS"].

3. str(None) produces "None" string in Redis persistence (redis.py:335-336)

response_data=str(item.get(self.data_attr)),          # str(None) = "None"
payload_hash=str(item.get(self.validation_key_attr)),  # str(None) = "None"

When the value is missing from Redis, this creates the string "None" instead of actual None.

4. Duplicated null-check for idempotency key (persistence/base.py)

The same 4-line block is copy-pasted in save_success, save_inprogress, delete_record, and get_record:

idempotency_key = self._get_hashed_idempotency_key(data=data)
if idempotency_key is None:
    # If the idempotency key is None, no data will be saved in the Persistence Layer.
    # See: https://github.com/aws-powertools/powertools-lambda-python/issues/2465
    return None

Can be extracted into a single helper method to reduce duplication.

Which area does this relate to?

Idempotency

Suggestion

Fix this.

Acknowledgment

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    tech-debtTechnical Debt tasks

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions