Skip to content

Using event_source decorator with tracer.capture_lambda_handler decorator #4795

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
aminalaee opened this issue Jul 22, 2024 · 3 comments · Fixed by #4796
Closed

Using event_source decorator with tracer.capture_lambda_handler decorator #4795

aminalaee opened this issue Jul 22, 2024 · 3 comments · Fixed by #4796
Assignees
Labels
typing Static typing definition related issues (mypy, pyright, etc.)

Comments

@aminalaee
Copy link
Contributor

aminalaee commented Jul 22, 2024

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.

@aminalaee aminalaee added triage Pending triage from maintainers typing Static typing definition related issues (mypy, pyright, etc.) labels Jul 22, 2024
Copy link

boring-cyborg bot commented Jul 22, 2024

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
Copy link
Contributor

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.

@dreamorosi dreamorosi moved this from Triage to Pending review in Powertools for AWS Lambda (Python) Aug 5, 2024
@dreamorosi dreamorosi removed the triage Pending triage from maintainers label Aug 5, 2024
@github-project-automation github-project-automation bot moved this from Pending review to Coming soon in Powertools for AWS Lambda (Python) Aug 6, 2024
Copy link
Contributor

github-actions bot commented Aug 6, 2024

⚠️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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
typing Static typing definition related issues (mypy, pyright, etc.)
Projects
Development

Successfully merging a pull request may close this issue.

3 participants