-
Notifications
You must be signed in to change notification settings - Fork 464
Description
Use case
As mentioned in BatchProcessor's documentation (https://docs.powertools.aws.dev/lambda/python/latest/utilities/batch/#create-your-own-partial-processor), BatchProcessor can be extended by overriding success_handler and failure_handler methods:
class MyProcessor(BatchProcessor):
def failure_handler(self, record: SQSRecord, exception: ExceptionInfo) -> FailureResponse:
metrics.add_metric(name="BatchRecordFailures", unit=MetricUnit.Count, value=1)
return super().failure_handler(record, exception)
Reading this, I see and understand that the record is passed to the function as a Pydantic model. I would thus expect success_handler to take the same type of record as failure_handler. That is not currently the case, as record is passed as raw dict data in BatchProcessor's _process_record function:
powertools-lambda-python/aws_lambda_powertools/utilities/batch/base.py
Lines 485 to 502 in 021076e
| def _process_record(self, record: dict) -> Union[SuccessResponse, FailureResponse]: | |
| """ | |
| Process a record with instance's handler | |
| Parameters | |
| ---------- | |
| record: dict | |
| A batch record to be processed. | |
| """ | |
| data: Optional["BatchTypeModels"] = None | |
| try: | |
| data = self._to_batch_type(record=record, event_type=self.event_type, model=self.model) | |
| if self._handler_accepts_lambda_context: | |
| result = self.handler(record=data, lambda_context=self.lambda_context) | |
| else: | |
| result = self.handler(record=data) | |
| return self.success_handler(record=record, result=result) |
In my opinion, record args should be homogeneous between the two functions, or otherwise devs wanting to override success_handler will encounter errors at runtime, have to dig in this tool's source code and understand the subtlety (like I did).
Solution/User Experience
First solution would be to add an example for success_handler in the documentation (https://docs.powertools.aws.dev/lambda/python/latest/utilities/batch/#extending-batchprocessor). This would notify developers of the differences between the functions' signature.
Second solution would be to pass the Pydantic model as record to success_handler. This should be a pretty straightforward change in the source code since this structure is already instantiated in _process_record. This would make the tool more homogeneous as well as improve experience in using the record's attributes in success_handler.
Alternative solutions
No response
Acknowledgment
- This feature request meets Powertools for AWS Lambda (Python) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Java, TypeScript, and .NET
Metadata
Metadata
Assignees
Labels
Type
Projects
Status