Bug: SQSMessageAttribute.binary_value raises KeyError for String attributes

Offen
#8,712 1 Kommentar 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@anishmehta24 arbeitet bereits daran.

Seit 30.10.2026.

  • #8713 von @anishmehta24 — offen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Beschreibung

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

Vorherrschende Sprache
Python
Sterne
3.3k
Forks
511
Ø Merge
3 T. 8 Std.
Gemergte PRs (30 T.)
92

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus aws-powertools/powertools-lambda-python

Alle Issues in aws-powertools/powertools-lambda-python

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.