Bug: SQSMessageAttribute.binary_value raises KeyError for String attributes

Aberta
#8,712 1 comentário 0 reações 1 responsável Ver no GitHub

Mantenedores costumam responder em até 1 dia

@anishmehta24 já está trabalhando nisso.

Desde 30/10/2026.

  • #8713 de @anishmehta24 — aberto

Avaliação

Esta issue ainda não foi avaliada.

Descrição

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

Linguagem predominante
Python
Estrelas
3.3k
Forks
511
Merge médio
3d 8h
PRs com merge (30d)
92

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de aws-powertools/powertools-lambda-python

Todas as issues de aws-powertools/powertools-lambda-python

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.