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

refactor(typing): Update event_handler to pass strict checks

Open BVMiko opened this issue 5 months ago • 2 comments

While testing out moving an APIGatewayRestResolver projects to run mypy in strict mode, I noticed that 4 areas were failing type inference:

router = Router()

throws: error: Call to untyped function "Router" in typed context [no-untyped-call]

and

@router.get('/hello')
def hello_get() -> Response[str]:
    return Response(200, body="Hello World!")

throws: error: Untyped decorator makes function "hello_get" untyped [misc]

and

@app.exception_handler(Exception)
def handle_generic_exception(exception: Exception) -> Response[str]:
    return Response(500, 'error')

throws: error: Untyped decorator makes function "handle_generic_exception" untyped [misc]

and

foo = 42
app.append_context(foo=foo)

throws: error: Call to untyped function "append_context" in typed context [no-untyped-call]

Issue number:

Summary

Changes

Added some simple type definitions to affected areas of these 4 components.

User experience

The above code examples will not raise errors when running mypy --strict

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • [ ] Migration process documented
  • [ ] Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

BVMiko avatar Sep 07 '24 19:09 BVMiko