Skip to content

Bug: Response class does not honour docstrings on optional parameters #1559

Closed
@heitorlessa

Description

@heitorlessa

Expected Behaviour

According to docstrings, I should be able to only provide status_code as a parameter and have a valid response -- e.g., return Response(status_code=204)

Current Behaviour

Response signature has content_type and body as positional arguments which is incompatible with the docstrings.

{
  "errorMessage": "__init__() missing 2 required positional arguments: 'content_type' and 'body'",
  "errorType": "TypeError",
  "requestId": "b59862a6-b905-4497-9b33-cb7f9314c5b9",
  "stackTrace": [
    "  File \"/opt/python/aws_lambda_powertools/logging/logger.py\", line 354, in decorate\n    return lambda_handler(event, context, *args, **kwargs)\n",
    "  File \"/var/task/lambda_function.py\", line 19, in lambda_handler\n    return app.resolve(event, context)\n",
    "  File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 509, in resolve\n    return self._resolve().build(self.current_event, self._cors)\n",
    "  File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 571, in _resolve\n    return self._call_route(route, match_results.groupdict())  # pass fn args\n",
    "  File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 625, in _call_route\n    return ResponseBuilder(self._to_response(route.func(**args)), route)\n",
    "  File \"/var/task/lambda_function.py\", line 13, in get_todos\n    return Response(status_code=204)\n"
  ]
}

Code snippet

from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler import APIGatewayRestResolver, Response
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger()
app = APIGatewayRestResolver()


@app.get("/todos")
def get_todos():
    return Response(status_code=204)


@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
def lambda_handler(event: dict, context: LambdaContext) -> dict:
    return app.resolve(event, context)

Possible Solution

Keep status_code as positional while everything else with a default None value as the docstring suggests.

Steps to Reproduce

Either run a local unit test or deploy a Lambda function with the code snippet provided

AWS Lambda Powertools for Python version

latest

AWS Lambda function runtime

3.9

Packaging format used

Lambda Layers

Debugging logs

Test Event Name
apigwv1

Response
{
  "errorMessage": "__init__() missing 2 required positional arguments: 'content_type' and 'body'",
  "errorType": "TypeError",
  "requestId": "b59862a6-b905-4497-9b33-cb7f9314c5b9",
  "stackTrace": [
    "  File \"/opt/python/aws_lambda_powertools/logging/logger.py\", line 354, in decorate\n    return lambda_handler(event, context, *args, **kwargs)\n",
    "  File \"/var/task/lambda_function.py\", line 19, in lambda_handler\n    return app.resolve(event, context)\n",
    "  File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 509, in resolve\n    return self._resolve().build(self.current_event, self._cors)\n",
    "  File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 571, in _resolve\n    return self._call_route(route, match_results.groupdict())  # pass fn args\n",
    "  File \"/opt/python/aws_lambda_powertools/event_handler/api_gateway.py\", line 625, in _call_route\n    return ResponseBuilder(self._to_response(route.func(**args)), route)\n",
    "  File \"/var/task/lambda_function.py\", line 13, in get_todos\n    return Response(status_code=204)\n"
  ]
}

Function Logs
START RequestId: b59862a6-b905-4497-9b33-cb7f9314c5b9 Version: $LATEST
[ERROR] TypeError: __init__() missing 2 required positional arguments: 'content_type' and 'body'
Traceback (most recent call last):
  File "/opt/python/aws_lambda_powertools/logging/logger.py", line 354, in decorate
    return lambda_handler(event, context, *args, **kwargs)
  File "/var/task/lambda_function.py", line 19, in lambda_handler
    return app.resolve(event, context)
  File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 509, in resolve
    return self._resolve().build(self.current_event, self._cors)
  File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 571, in _resolve
    return self._call_route(route, match_results.groupdict())  # pass fn args
  File "/opt/python/aws_lambda_powertools/event_handler/api_gateway.py", line 625, in _call_route
    return ResponseBuilder(self._to_response(route.func(**args)), route)
  File "/var/task/lambda_function.py", line 13, in get_todos
    return Response(status_code=204)END RequestId: b59862a6-b905-4497-9b33-cb7f9314c5b9
REPORT RequestId: b59862a6-b905-4497-9b33-cb7f9314c5b9	Duration: 20.79 ms	Billed Duration: 21 ms	Memory Size: 128 MB	Max Memory Used: 58 MB	Init Duration: 705.25 ms

Request ID
b59862a6-b905-4497-9b33-cb7f9314c5b9

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions