Bug: SQSMessageAttribute.binary_value raises KeyError for String attributes

Đang mở
#8,712 1 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@anishmehta24 đang làm issue này rồi.

Từ ngày 30/10/2026.

  • #8713 của @anishmehta24 — đang mở

Đánh giá

Issue này chưa được đánh giá.

Mô tả

bug event_sources

Expected Behaviour

SQSMessageAttribute.string_value and binary_value are typed str | None, so reading the value an attribute doesn't carry should return None.

Current Behaviour

Both properties index the dict directly (self["stringValue"], self["binaryValue"]). Lambda only sends the field that matches the attribute's type; the SQS example event in the Lambda docs has {"stringValue": "myValue", "stringListValues": [], "binaryListValues": [], "dataType": "String"} with no binaryValue. So binary_value on any String or Number attribute, and string_value on any Binary attribute, raise KeyError.

The test event tests/events/sqsEvent.json puts both stringValue and binaryValue on one attribute, which is why the existing tests don't hit this. The Pydantic SqsMsgAttributeModel already declares both fields optional.

Code snippet

from aws_lambda_powertools.utilities.data_classes import SQSEvent

event = {"Records": [{
    "messageId": "1", "receiptHandle": "r", "body": "b", "attributes": {}, "md5OfBody": "m",
    "eventSource": "aws:sqs", "eventSourceARN": "arn:aws:sqs:us-east-2:123456789012:q", "awsRegion": "us-east-2",
    "messageAttributes": {
        "text": {"stringValue": "myValue", "stringListValues": [], "binaryListValues": [], "dataType": "String"},
    },
}]}
record = next(SQSEvent(event).records)
record.message_attributes["text"].binary_value  # KeyError: 'binaryValue'

Steps to Reproduce

  1. Send an SQS message with a String message attribute to a Lambda function.
  2. Read binary_value on that attribute (or string_value on a Binary one).
  3. KeyError is raised instead of None.

Possible Solution

Use self.get(...) in both properties. I can open a PR.

Powertools for AWS Lambda (Python) version

latest (develop, 3.38.0)

AWS Lambda function runtime

3.12

Packaging format used

PyPi

Debugging logs

No response

Ngôn ngữ chính
Python
Star
3.3k
Fork
511
Merge trung bình
3 ngày 12 giờ
Pull request đã merge (30 ngày)
93

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của aws-powertools/powertools-lambda-python

Tất cả issue của aws-powertools/powertools-lambda-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.