Skip to content

Commit 6eb8443

Browse files
committed
Use generics for logger.inject_lambda_context.
This enables static-type checking correctly with mypy when using pydantic models for the lambda event argument rather than a mapping type. Currently using this decorator results in an error and requires a typing ignore directive on the line using the decorator.
1 parent 11b6afd commit 6eb8443

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

aws_lambda_powertools/logging/logger.py

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
import os
55
import random
66
import sys
7-
from typing import IO, Any, Callable, Dict, Iterable, Optional, TypeVar, Union
7+
from typing import IO, Any, Callable, Iterable, Optional, TypeVar, Union, overload
88

99
import jmespath
1010

1111
from ..shared import constants
1212
from ..shared.functions import resolve_env_var_choice, resolve_truthy_env_var_choice
13+
from ..shared.types import AnyCallableT
1314
from .exceptions import InvalidLoggerSamplingRateError
1415
from .filters import SuppressFilter
1516
from .formatter import (
@@ -270,13 +271,33 @@ def _configure_sampling(self):
270271
f"Please review POWERTOOLS_LOGGER_SAMPLE_RATE environment variable."
271272
)
272273

274+
@overload
273275
def inject_lambda_context(
274276
self,
275-
lambda_handler: Optional[Callable[[Dict, Any], Any]] = None,
277+
lambda_handler: AnyCallableT,
276278
log_event: Optional[bool] = None,
277279
correlation_id_path: Optional[str] = None,
278280
clear_state: Optional[bool] = False,
279-
):
281+
) -> AnyCallableT:
282+
...
283+
284+
@overload
285+
def inject_lambda_context(
286+
self,
287+
lambda_handler: None = None,
288+
log_event: Optional[bool] = None,
289+
correlation_id_path: Optional[str] = None,
290+
clear_state: Optional[bool] = False,
291+
) -> Callable[[AnyCallableT], AnyCallableT]:
292+
...
293+
294+
def inject_lambda_context(
295+
self,
296+
lambda_handler: Optional[AnyCallableT] = None,
297+
log_event: Optional[bool] = None,
298+
correlation_id_path: Optional[str] = None,
299+
clear_state: Optional[bool] = False,
300+
) -> Any:
280301
"""Decorator to capture Lambda contextual info and inject into logger
281302
282303
Parameters

0 commit comments

Comments
 (0)