Skip to content

fix(data_classes): return None for the SQS attribute value an attribute does not carry - #8713

Open
anishmehta24 wants to merge 1 commit into
aws-powertools:developfrom
anishmehta24:fix/sqs-message-attribute-missing-value
Open

anishmehta24 wants to merge 1 commit into
aws-powertools:developfrom
anishmehta24:fix/sqs-message-attribute-missing-value

Conversation

@anishmehta24

Copy link
Copy Markdown

Issue number: closes #8712

Summary

Changes

SQSMessageAttribute.string_value and binary_value are typed str | None but indexed the dict directly. Lambda only includes the field matching the attribute's type (a String attribute has no binaryValue, a Binary one no stringValue), so reading the other one raised KeyError. Both now use self.get(...) and return None, which matches their type hints and the Pydantic SqsMsgAttributeModel; the docstrings say when the value is None.

Added test_sqs_message_attribute_with_only_one_value in tests/unit/data_classes/required_dependencies/test_sqs_event.py with a String and a Binary attribute shaped like the Lambda docs example; it fails on develop with KeyError: 'binaryValue'. The data classes and batch tests pass.

User experience

Before: record.message_attributes["text"].binary_value on a String attribute raises KeyError. After: it returns None.


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

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@anishmehta24
anishmehta24 requested a review from a team as a code owner October 30, 2026 16:58
@anishmehta24
anishmehta24 requested a review from hjgraca October 30, 2026 16:58
@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 30, 2026
@boring-cyborg boring-cyborg Bot added the tests label Oct 30, 2026
@sonarqubecloud

Copy link
Copy Markdown

@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 @anishmehta24, confirmed with a real event: I sent a message with String, Number and Binary attributes through a queue to a function and Lambda delivers only the field matching the type, so binary_value on the String one raises KeyError on develop and returns None with your change, same as the Pydantic model already does.

I'm approving now. This goes out in 3.40.0, so it will sit here for a while before I merge it. No rebase needed in the meantime, and you'll be credited in the release notes.

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

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: SQSMessageAttribute.binary_value raises KeyError for String attributes

2 participants