Repository: aws-powertools/powertools-lambda-python

Issue:
mypy fails with @tracer.capture_lambda_handler
<!--- Provide a general summary of the issue in the Title above -->
<!--- How has this issue affected you? What are you trying to accomplish? -->

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 11ebcf9f216e00a2b2d842154121a569bc430a23 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






Discussion:
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! 

Resolving patch:
diff --git a/aws_lambda_powertools/tracing/tracer.py b/aws_lambda_powertools/tracing/tracer.py
--- a/aws_lambda_powertools/tracing/tracer.py
+++ b/aws_lambda_powertools/tracing/tracer.py
@@ -244,7 +244,7 @@ def patch(self, modules: Tuple[str] = None):
 
     def capture_lambda_handler(
         self,
-        lambda_handler: Callable[[Dict, Any, Optional[Dict]], Any] = None,
+        lambda_handler: Union[Callable[[Dict, Any], Any], Callable[[Dict, Any, Optional[Dict]], Any]] = None,
         capture_response: Optional[bool] = None,
         capture_error: Optional[bool] = None,
     ):
@@ -517,7 +517,7 @@ async def async_tasks():
 
     def _decorate_async_function(
         self,
-        method: Callable = None,
+        method: Callable,
         capture_response: Optional[Union[bool, str]] = None,
         capture_error: Optional[Union[bool, str]] = None,
         method_name: str = None,
@@ -544,7 +544,7 @@ async def decorate(*args, **kwargs):
 
     def _decorate_generator_function(
         self,
-        method: Callable = None,
+        method: Callable,
         capture_response: Optional[Union[bool, str]] = None,
         capture_error: Optional[Union[bool, str]] = None,
         method_name: str = None,
@@ -571,7 +571,7 @@ def decorate(*args, **kwargs):
 
     def _decorate_generator_function_with_context_manager(
         self,
-        method: Callable = None,
+        method: Callable,
         capture_response: Optional[Union[bool, str]] = None,
         capture_error: Optional[Union[bool, str]] = None,
         method_name: str = None,
@@ -599,7 +599,7 @@ def decorate(*args, **kwargs):
 
     def _decorate_sync_function(
         self,
-        method: Callable = None,
+        method: Callable,
         capture_response: Optional[Union[bool, str]] = None,
         capture_error: Optional[Union[bool, str]] = None,
         method_name: str = None,
@@ -654,20 +654,20 @@ def _add_response_as_metadata(
 
     def _add_full_exception_as_metadata(
         self,
-        method_name: str = None,
-        error: Exception = None,
-        subsegment: BaseSegment = None,
+        method_name: str,
+        error: Exception,
+        subsegment: BaseSegment,
         capture_error: Optional[bool] = None,
     ):
         """Add full exception object as metadata for given subsegment
 
         Parameters
         ----------
-        method_name : str, optional
+        method_name : str
             method name to add as metadata key, by default None
-        error : Exception, optional
+        error : Exception
             error to add as subsegment metadata, by default None
-        subsegment : BaseSegment, optional
+        subsegment : BaseSegment
             existing subsegment to add metadata on, by default None
         capture_error : bool, optional
             Do not include error as metadata, by default True
@@ -717,7 +717,7 @@ def __build_config(
         service: str = None,
         disabled: bool = None,
         auto_patch: bool = None,
-        patch_modules: List = None,
+        patch_modules: Union[List, Tuple] = None,
         provider: BaseProvider = None,
     ):
         """ Populates Tracer config for new and existing initializations """


Test patch: