Bug: SQSMessageAttribute.binary_value raises KeyError for String attributes

オープン
#8,712 コメント 1 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@anishmehta24 がすでに取り組んでいます。

2026年10月30日 から。

  • #8713 @anishmehta24 による — オープン

評価

この issue はまだ評価されていません。

説明

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

主要言語
Python
スター
3.3k
フォーク
511
平均マージ
3日 8時間
マージ済み PR(30日)
92

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

aws-powertools/powertools-lambda-python のほかの issue

aws-powertools/powertools-lambda-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。