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 1 year 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

Hi @BVMiko! Thanks for submitting this PR! Can I ask you to open an issue telling us what problem you want to solve here? We love explaining the challenges customers face and how they can contribute to solving them.

Also, I see mypy complaining about types, can you please check?

leandrodamascena avatar Sep 09 '24 15:09 leandrodamascena

Hi @BVMiko! I'll close this PR and create a new one targeting the develop branch. I'll give you credit in the release notes.

leandrodamascena avatar Dec 23 '24 15:12 leandrodamascena

Actually it was resolved here: https://github.com/aws-powertools/powertools-lambda-python/pull/5601

leandrodamascena avatar Dec 23 '24 18:12 leandrodamascena