Skip to content

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

Closed
@rafrafek

Description

@rafrafek
Contributor

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

Activity

added
triagePending triage from maintainers
typingStatic typing definition related issues (mypy, pyright, etc.)
on Mar 20, 2024
rafrafek

rafrafek commented on Mar 21, 2024

@rafrafek
ContributorAuthor

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"
leandrodamascena

leandrodamascena commented on Mar 21, 2024

@leandrodamascena
Contributor

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

moved this from Triage to Pending review in Powertools for AWS Lambda (Python)on Mar 21, 2024
added
help wantedCould use a second pair of eyes/hands
triagePending triage from maintainers
on Jun 9, 2024
heitorlessa

heitorlessa commented on Jun 10, 2024

@heitorlessa
Contributor

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

moved this from Pending review to Backlog in Powertools for AWS Lambda (Python)on Jun 10, 2024
rafrafek

rafrafek commented on Nov 20, 2024

@rafrafek
ContributorAuthor

Created PR for this, appreciate reviewing it: #5601

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedCould use a second pair of eyes/handsrevisit-in-3-monthsRequires more customers feedback before making or revisiting a decisiontypingStatic typing definition related issues (mypy, pyright, etc.)

    Type

    No type

    Projects

    Status

    Shipped

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @heitorlessa@leandrodamascena@dreamorosi@rafrafek

      Issue actions

        Static typing: APIGatewayRestResolver `.post()` decorator return type Unknown · Issue #3990 · aws-powertools/powertools-lambda-python