powertools-lambda-python icon indicating copy to clipboard operation
powertools-lambda-python copied to clipboard

Static typing: APIGatewayRestResolver `.post()` decorator return type Unknown

Open rafrafek opened this issue 1 year ago • 3 comments

Static type checker used

pyright/pylance

AWS Lambda function runtime

3.12

Powertools for AWS Lambda (Python) version

latest

Static type checker info

Latest Pylance, latest VS Code, type checking mode strict.

Untyped function decorator obscures type of function

Screenshot:

image

Code snippet

from aws_lambda_powertools.event_handler import APIGatewayRestResolver

app = APIGatewayRestResolver()

@app.post("/v1/demo")
def demo() -> str:
    return "demo"

Possible Solution

return Callable[..., T] or something like that

rafrafek avatar Mar 20 '24 15:03 rafrafek

I think the solution may look similar to this:

from typing import Any, Callable, TypeVar


T_route = TypeVar("T_route", bound=Callable[..., Any])


class Resolver:

    def route(self, rule: str, method: str) -> Callable[[T_route], T_route]:

        def register_resolver(func: T_route) -> T_route:
            print(rule, method)
            return func

        return register_resolver

    def post(self, rule: str) -> Callable[[T_route], T_route]:
        return self.route(rule=rule, method="POST")


app = Resolver()


@app.route("/v1/demo", "GET")
def demo() -> str:
    return "demo"


@app.post("/v1/demo_post")
def demo_post() -> str:
    return "demo post"

rafrafek avatar Mar 21 '24 12:03 rafrafek

Hello @rafrafek! Thanks for opening this issue! I'm planning to review this next week.

leandrodamascena avatar Mar 21 '24 21:03 leandrodamascena

Adding help wanted label as we were unable to prioritize it. Any PR welcome <3

heitorlessa avatar Jun 10 '24 08:06 heitorlessa