Skip to content

mypy fails with @tracer.capture_lambda_handler #360

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

Closed
joyofhex opened this issue Mar 25, 2021 · 5 comments
Closed

mypy fails with @tracer.capture_lambda_handler #360

joyofhex opened this issue Mar 25, 2021 · 5 comments
Labels
bug Something isn't working
Milestone

Comments

@joyofhex
Copy link

Using mypy to typecheck code decorated with the @tracer.capture_lambda_handler fails since v1.10.

Expected Behavior

Mypy should not fail with code decorated with the capture_lambda_handler.

Current Behavior

❯ poetry run mypy src
src/handler.py:5: error: Argument 1 to "capture_lambda_handler" of "Tracer" has incompatible type "Callable[[Any, Any], Any]"; expected "Optional[Callable[[Dict[Any, Any], Any, Optional[Dict[Any, Any]]], Any]]"
Found 1 error in 1 file (checked 1 source file)

Possible Solution

First of all, thanks for the incredibly useful library. It has been a pleasure to work with it.

It seems that in commit 11ebcf9 the following change was introduced:

@@ -238,7 +242,7 @@ class Tracer:

     def capture_lambda_handler(
         self,
-        lambda_handler: Callable[[Dict, Any], Any] = None,
+        lambda_handler: Callable[[Dict, Any, Optional[Dict]], Any] = None,
         capture_response: Optional[bool] = None,
         capture_error: Optional[bool] = None,
     ):

This seems to have changed the expected handler signature to have three parameters instead of two, which is causing the sample code to fail. I've worked around this for now by adding a _ignored: Optional[Any] = None parameter to my lambda handler.

Steps to Reproduce (for bugs)

I created a small repo repo (uh) here: https://github.com/joyofhex/repocase-for-powertools

  1. clone above repo
  2. cd into repo
  3. poetry run mypy

Environment

  • Powertools version used: 1.13.0
  • Packaging format (Layers, PyPi): pypi
  • AWS Lambda function runtime: python 3.8
@joyofhex joyofhex added bug Something isn't working triage Pending triage from maintainers labels Mar 25, 2021
@heitorlessa
Copy link
Contributor

Thanks a lot for raising it @joyofhex, and it's quite timely because we're working to make MyPy compliant for the next release ;-)

Let us know if you find anything else and we keep you posted!

@heitorlessa heitorlessa added area/tracer and removed triage Pending triage from maintainers labels Mar 25, 2021
michaelbrewer referenced this issue in gyft/aws-lambda-powertools-python Mar 25, 2021
michaelbrewer referenced this issue in gyft/aws-lambda-powertools-python Mar 25, 2021
@michaelbrewer
Copy link
Contributor

@joyofhex @heitorlessa i have a fix in for this on #365

So this example will work via MyPy

from aws_lambda_powertools import Tracer

tracer = Tracer()


@tracer.capture_lambda_handler
def handler(event, context):
    return None

@joyofhex
Copy link
Author

Oh, excellent. Thanks for taking the time to address this.

@heitorlessa heitorlessa added this to the 1.14.0 milestone Mar 29, 2021
@heitorlessa
Copy link
Contributor

heitorlessa commented Mar 29, 2021

Reopening as we will include this in the next minor release, then we can ping you once it's available :)

@heitorlessa heitorlessa reopened this Mar 29, 2021
@heitorlessa heitorlessa added the pending-release Fix or implementation already in dev waiting to be released label Mar 29, 2021
@michaelbrewer
Copy link
Contributor

@heitorlessa I think we need to something else to mark the modules as PEP 561

@heitorlessa heitorlessa removed the pending-release Fix or implementation already in dev waiting to be released label Jul 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

3 participants