Skip to content

Docs: Invalid return type in middleware example #3556

@kamilturek

Description

@kamilturek

What were you searching in the docs?

All examples on the middleware factory page present a middleware function with return type of Callable which seems incorrect.

image

The actual return type of the middleware_before function is the same as the one of the lambda handler - dict this case. Only after the function is decorated, it'll be a Callable but I believe the function definition should anyway have the return type of dict.

A small reproducible example:

from typing import Callable

from aws_lambda_powertools.middleware_factory import lambda_handler_decorator

LambdaHandler = Callable[[dict, dict], dict]


@lambda_handler_decorator
def middleware_add_headers(
    handler: LambdaHandler, event: dict, context: dict
) -> Callable:
    return handler(event, context)


@middleware_add_headers
def lambda_handler(event: dict, context: dict) -> dict:
    return {
        "statusCode": 200,
        "body": "hello",
    }

Mypy output:

$ mypy main.py
handler.py:12: error: Incompatible return value type (got "dict[Any, Any]", expected "Callable[..., Any]")  [return-value]
Found 1 error in 1 file (checked 1 source file)

Is this related to an existing documentation section?

https://docs.powertools.aws.dev/lambda/python/latest/utilities/middleware_factory/#middleware-with-before-logic

How can we improve?

All invalid examples should be updated with correct return types (seems like dict in all cases).

Got a suggestion in mind?

I'm happy to help with resolving this issue.

Acknowledgment

  • I understand the final update might be different from my proposed suggestion, or refused.

Metadata

Metadata

Labels

documentationImprovements or additions to documentationmiddleware_factoryMiddleware factory utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions