aws-powertools / powertools-lambda-python

A developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/python/latest/
MIT No Attribution
2.72k stars 378 forks source link

Bug: KafkaEventRecord Incorrect types for offset and partition properties #4504

Closed scampbell5 closed 3 weeks ago

scampbell5 commented 3 weeks ago

Expected Behaviour

The properties for offset and partition are the correct int type on the KafkaEventRecord class.

Current Behaviour

The properties for offset and partition on the KafkaEventRecord are currently defined as str type.

Code snippet

class KafkaEventRecord(DictWrapper):
    @property
    def topic(self) -> str:
        """The Kafka topic."""
        return self["topic"]

    @property
    def partition(self) -> str:
        """The Kafka record parition."""
        return self["partition"]

    @property
    def offset(self) -> str:
        """The Kafka record offset."""
        return self["offset"]

https://github.com/aws-powertools/powertools-lambda-python/blob/develop/aws_lambda_powertools/utilities/data_classes/kafka_event.py#L18

Possible Solution

Change the type to int for the partition and offset properties on the KafkaEventRecord class.

Steps to Reproduce

from aws_lambda_powertools.utilities.data_classes import event_source, KafkaEvent

@event_source(data_class=KafkaEvent)
def lambda_handler(event: KafkaEvent, context):
    assert isinstance(event.record.offset, int)
    assert isinstance(event.record.partition, int)

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.11

Packaging format used

Lambda Layers

Debugging logs

No response

boring-cyborg[bot] commented 3 weeks ago

Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

leandrodamascena commented 3 weeks ago

Hi @scampbell5! Thanks for pointing out this bug. Actually, you're 100% right, these fields should be int instead of str. Do you want to submit a PR to fix this? We can work together to merge it.

References: https://docs.aws.amazon.com/lambda/latest/dg/with-kafka.html https://github.com/aws/aws-lambda-go/blob/main/events/kafka.go

scampbell5 commented 3 weeks ago

@leandrodamascena Appreciate the quick response! I'll work on putting together a PR later tonight to fix this.

leandrodamascena commented 3 weeks ago

Perfect, let me know if you have any questions or blocks, and we can work together on this!

We truly appreciate when the community contributes to the project. 🚀

scampbell5 commented 3 weeks ago

@leandrodamascena I've submitted this PR if you don't mind taking a look when you have time: https://github.com/aws-powertools/powertools-lambda-python/pull/4515

github-actions[bot] commented 3 weeks ago

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.