aws-powertools / powertools-lambda-python

A developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/python/latest/
MIT No Attribution
2.81k stars 389 forks source link

Using `event_source` decorator with `tracer.capture_lambda_handler` decorator #4795

Closed aminalaee closed 1 month ago

aminalaee commented 1 month ago

Static type checker used

pyright/pylance

AWS Lambda function runtime

3.12

Powertools for AWS Lambda (Python) version

latest

Static type checker info

Argument of type "(event: SNSEvent, context: Unknown) -> dict[str, Unknown]" cannot be assigned to parameter "lambda_handler" of type "((Dict[Unknown, Unknown], Any) -> Any) | ((Dict[Unknown, Unknown], Any, Dict[Unknown, Unknown] | None) -> Any) | None" in function "capture_lambda_handler"
  Type "(event: SNSEvent, context: Unknown) -> dict[str, Unknown]" is incompatible with type "((Dict[Unknown, Unknown], Any) -> Any) | ((Dict[Unknown, Unknown], Any, Dict[Unknown, Unknown] | None) -> Any) | None"
    Type "(event: SNSEvent, context: Unknown) -> dict[str, Unknown]" is incompatible with type "(Dict[Unknown, Unknown], Any) -> Any"
      Parameter 1: type "Dict[Unknown, Unknown]" is incompatible with type "SNSEvent"
        "Dict[Unknown, Unknown]" is incompatible with "SNSEvent"
    Type "(event: SNSEvent, context: Unknown) -> dict[str, Unknown]" is incompatible with type "(Dict[Unknown, Unknown], Any, Dict[Unknown, Unknown] | None) -> Any"
      Function accepts too many positional parameters; expected 2 but received 3
        Parameter 1: type "Dict[Unknown, Unknown]" is incompatible with type "SNSEvent"
          "Dict[Unknown, Unknown]" is incompatible with "SNSEvent"

Code snippet

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.utilities.data_classes import SNSEvent, event_source

tracer = Tracer()
logger = Logger()

@logger.inject_lambda_context
@tracer.capture_lambda_handler
@event_source(data_class=SNSEvent)
def lambda_handler(event: SNSEvent, context: LambdaContext): ...

Possible Solution

When using the tracer.capture_lambda_handler decorator with event_source decorator Pylance is complaining about type mismatch. Checking the logger.inject_lambda_context I think we could use AnyCallableT .

Does that make sense to create a PR for this? Or is too broad of a type? I will create a small PR anyway.

boring-cyborg[bot] commented 1 month ago

Thanks for opening your first issue here! We'll come back to you as soon as we can. In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

leandrodamascena commented 1 month ago

Does that make sense to create a PR for this? Or is too broad of a type? I will create a small PR anyway.

Hello @aminalaee! I don't think that's too broad of a type. By definition lambda_handler is an event and a context, but you can add more parameters or even *args, **kwargs because you need to inject some dependencies into your tests or any other use case. So I think it's ok to change the function signature.

github-actions[bot] commented 1 month ago

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.