Open tobocop2 opened 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.
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!
Why is this needed?
Problem Statement
The AWS Lambda Powertools for Python currently provides several options for managing HTTP responses, such as:
Response
object: Offers flexibility but lacks strong typing and adds additional boilerplate for status codes and headers management.Tuple[Dict, int]
(Dict + status code): Common but can lead to juggling multiple return values without type safety.Dict
: Simplest option yet does not handle status codes or headers well when managed manually.These methods pose several challenges:
Proposed Solution
I propose a
JsonResponse
generic that inherits from theResponse
, allowing developers to return their responses as typed, JSON-serialized Pydantic models. This offers:Type safety: Developers can return
JsonResponse[MyModel]
directly tied to a Pydantic model, ensuring valid responses and reducing errors.Consistency: Standardizing the return type across the codebase leads to more maintainable and predictable API behavior.
Improved developer experience: With
JsonResponse
, there’s no need for manual handling of status codes or serialization—it's done automatically.Example
JsonResponse
implementation:Here's an example of its usage:
Benefits
This approach addresses several key issues:
Enhanced type safety: By using generics tied to Pydantic models, the system ensures that API responses match the expected structure.
Reduced boilerplate: No need for manual response serialization or status code management.
Increased consistency: All API responses follow a consistent pattern, making the codebase easier to maintain and less error-prone.
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