Skip to content

Bug: Using @metrics.log_metrics and @event_parser on the same handler causes a type error. #3447

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
brdskggs opened this issue Dec 4, 2023 · 5 comments · Fixed by #3460
Closed
Assignees
Labels
metrics typing Static typing definition related issues (mypy, pyright, etc.)

Comments

@brdskggs
Copy link

brdskggs commented Dec 4, 2023

Expected Behaviour

I expect to be able to use @metrics.log_metrics and @event_parser on the same Lambda handler. It doesn't work, regardless of order they are used to annotate the handler.

Current Behaviour

I get the following error in VS Code, according to Pylance:

Argument of type "(event: AgentQuery, context: LambdaContext) -> str" 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 "__call__"
  Type "(event: AgentQuery, context: LambdaContext) -> str" cannot be assigned to type "((Dict[Unknown, Unknown], Any) -> Any) | ((Dict[Unknown, Unknown], Any, Dict[Unknown, Unknown] | None) -> Any) | None"
    Type "(event: AgentQuery, context: LambdaContext) -> str" cannot be assigned to type "(Dict[Unknown, Unknown], Any) -> Any"
      Parameter 1: type "Dict[Unknown, Unknown]" cannot be assigned to type "AgentQuery"
        "Dict[Unknown, Unknown]" is incompatible with "AgentQuery"
    Type "(event: AgentQuery, context: LambdaContext) -> str" cannot be assigned to 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]" cannot be assigned to type "AgentQuery"
          "Dict[Unknown, Unknown]" is incompatible with "AgentQuery"
Argument missing for parameter "context"

Code snippet

import json
from typing import List

from aws_lambda_powertools import Logger, Metrics
from aws_lambda_powertools.utilities.parser import BaseModel, Field, event_parser
from aws_lambda_powertools.utilities.typing import LambdaContext


logger = Logger()
metrics = Metrics()


class AgentQuery(BaseModel):
    username: str
    user_query: str


@logger.inject_lambda_context
@metrics.log_metrics(capture_cold_start_metric=True)
@event_parser(model=AgentQuery)
def lambda_handler(event: AgentQuery, context: LambdaContext):
    logger.info("hi")


### Possible Solution

Parser and Event Sources mutate the event, but `log_metrics` expect a dict.

Potential fix is to update type annotations for handler passed to `log_metrics` to be `AnyCallableT`.

### Steps to Reproduce

Paste the code in VS Code, and look at the Problems tab highlighted by Pylance.

### Powertools for AWS Lambda (Python) version

latest

### AWS Lambda function runtime

3.9

### Packaging format used

PyPi

### Debugging logs

_No response_
@brdskggs brdskggs added bug Something isn't working triage Pending triage from maintainers labels Dec 4, 2023
Copy link

boring-cyborg bot commented Dec 4, 2023

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

@heitorlessa heitorlessa added typing Static typing definition related issues (mypy, pyright, etc.) metrics and removed bug Something isn't working labels Dec 4, 2023
@heitorlessa heitorlessa self-assigned this Dec 6, 2023
@heitorlessa heitorlessa moved this from Triage to Working on it in Powertools for AWS Lambda (Python) Dec 6, 2023
@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Dec 6, 2023
@heitorlessa
Copy link
Contributor

looking into it now.

@heitorlessa
Copy link
Contributor

PR is up with the fix. That said, Pylance complains about typing being partially unknown. While I'd love to find a solution for it, mypy (our preferred static checker) is happy with it. I suspect is due to now using ParamSpec but that doesn't work well with partial to support decorators with @metrics.log_metrics, @metrics.log_metrics(), @metrics.log_metrics(params...).

We should maybe do a campaign next year to identify what Pylance issues are worth resolving.

image

Copy link
Contributor

github-actions bot commented Dec 6, 2023

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

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Dec 6, 2023
Copy link
Contributor

github-actions bot commented Dec 6, 2023

This is now released under 2.29.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Dec 6, 2023
@heitorlessa heitorlessa moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
metrics typing Static typing definition related issues (mypy, pyright, etc.)
Projects
Status: Shipped
2 participants