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.87k stars 395 forks source link

Tech debt: limited response type handling #5326

Open tobocop2 opened 3 weeks ago

tobocop2 commented 3 weeks ago

Why is this needed?

Problem Statement

The AWS Lambda Powertools for Python currently provides several options for managing HTTP responses, such as:

These methods pose several challenges:

  1. Lack of type safety: Inconsistency in response types increases the risk of malformed responses and potential errors.
  2. Inconsistent response handling: Switching between return types can make the codebase harder to maintain, leading to more errors or inconsistent patterns.
  3. Developer friction: Developers must manually manage status codes and serialize responses, increasing cognitive load and reducing productivity.

Proposed Solution

I propose a JsonResponse generic that inherits from the Response, allowing developers to return their responses as typed, JSON-serialized Pydantic models. This offers:

Example JsonResponse implementation:

from aws_lambda_powertools.event_handler.api_gateway import Response
from typing import Generic, TypeVar
from pydantic import BaseModel

T = TypeVar("T", bound=BaseModel)

class JsonResponse(Response, Generic[T]):
    def __init__(self, content: T, status_code: int = 200):
        super().__init__(
            status_code=status_code,
            content_type="application/json",
            body=content.model_dump_json(),
        )

Here's an example of its usage:

@app.get("/thing")
def handler() -> JsonResponse[PaginatedThingResponse]:
    response: PaginatedThingResponse = get_things()
    return JsonResponse(response, status_code=HTTPStatus.OK)

Benefits

This approach addresses several key issues:

Summary

The introduction of JsonResponse addresses technical debt around response handling by enforcing consistent, type-safe responses across Lambda functions. This improvement reduces the risk of errors, enhances the developer experience, and makes the system more maintainable.

I'd be happy to make an open source contribution if this makes sense.

Which area does this relate to?

No response

Suggestion

No response

Acknowledgment

leandrodamascena commented 3 weeks ago

Hi @tobocop2! Thanks for opening this issue too, it seems like you have good arguments here. I need to test with some code and mypy to see how this works. Have you run tools like mypy and pyright with strict typing on your side? What did they report?

I'll put this in the backlog to look at next week and bring you more accurate insights on this.

tobocop2 commented 3 weeks ago

I have not ran either of those tools but I'd be happy to work on this if it makes sense. Thank you for the quick response!