Skip to content

mypy fails with @tracer.capture_lambda_handler #360

Closed

Description

@joyofhex

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

Activity

  1. added
    bug
    triage
    on Mar 25, 2021
  2. heitorlessa commented on Mar 25, 2021

    @heitorlessa
    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!

  3. added and removed
    triage
    on Mar 25, 2021
  4. michaelbrewer commented on Mar 25, 2021

    @michaelbrewer
    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
  5. joyofhex commented on Mar 26, 2021

    @joyofhex
    Author

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

  6. added this to the 1.14.0 milestone on Mar 29, 2021
  7. heitorlessa commented on Mar 29, 2021

    @heitorlessa
    Contributor

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

  8. added
    pending-release
    on Mar 29, 2021
  9. michaelbrewer commented on Mar 29, 2021

    @michaelbrewer
    Contributor

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

  10. removed
    pending-release
    on Jul 2, 2021
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

    bug

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions