Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rafrafek opened this issue Mar 20, 2024 · 2 comments
Open
Labels
typing Static typing definition related issues (mypy, pyright, etc.)

Comments

@rafrafek
Copy link
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

@rafrafek rafrafek added triage Pending triage from maintainers typing Static typing definition related issues (mypy, pyright, etc.) labels Mar 20, 2024
@rafrafek
Copy link
Contributor Author

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
Copy link
Contributor

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

@leandrodamascena leandrodamascena removed the triage Pending triage from maintainers label Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typing Static typing definition related issues (mypy, pyright, etc.)
Projects
Status: Pending review
Development

No branches or pull requests

2 participants