Skip to content
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

Static typing: Untyped event_source decorator with mypy --strict #3568

Open
JakubSosnovec opened this issue Dec 28, 2023 · 4 comments · May be fixed by #4165
Open

Static typing: Untyped event_source decorator with mypy --strict #3568

JakubSosnovec opened this issue Dec 28, 2023 · 4 comments · May be fixed by #4165
Labels
help wanted Could use a second pair of eyes/hands typing Static typing definition related issues (mypy, pyright, etc.)

Comments

@JakubSosnovec
Copy link

Static type checker used

mypy (project's standard)

AWS Lambda function runtime

3.12

Powertools for AWS Lambda (Python) version

2.30.2

Static type checker info

Just mypy --strict, with mypy version 1.8.0.
Output: error: Untyped decorator makes function "handler" untyped [misc]

Code snippet

$ pip freeze
aws-lambda-powertools==2.30.2
mypy==1.8.0
mypy-extensions==1.0.0
typing_extensions==4.9.0

$ cat main.py  --plain
from typing import Any
from aws_lambda_powertools.utilities.data_classes import SQSEvent, event_source
from aws_lambda_powertools.utilities.typing import LambdaContext
@event_source(data_class=SQSEvent)
def handler(event: SQSEvent, _context: LambdaContext) -> dict[str, Any]:
    return {}

$ mypy --strict main.py
main.py:7: error: Untyped decorator makes function "handler" untyped  [misc]
Found 1 error in 1 file (checked 1 source file)

Possible Solution

Very similar to #1060 which is claimed to be solved, but this issue appears in the latest release, mypy in strict mode still doesnt understand the return type. Maybe it came back?

@JakubSosnovec JakubSosnovec added triage Pending triage from maintainers typing Static typing definition related issues (mypy, pyright, etc.) labels Dec 28, 2023
Copy link

boring-cyborg bot commented Dec 28, 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

@leandrodamascena
Copy link
Contributor

Hi @JakubSosnovec! Thanks for bringing this question! Strict typing has been on our radar for a long time and we are correcting what is possible every time we release a new version, but this project is not 100% compatible with strict typing and we are investigating how to include it in our roadmap, but we do not have an ETA for that. I'll add this issue to our backlog and see if we can do something specifically for this case.

@heitorlessa would love to hear your thoughts here.

BTW, I'm trying here with mypy 1.4+ and Powertoos 1.25.7 and I get the same error. It seems like something is still unresolved.

❯ pip freeze
aws-lambda-powertools==1.25.7
aws-xray-sdk==2.12.1
boto3==1.34.10
botocore==1.34.10
fastjsonschema==2.19.1
jmespath==0.10.0
mypy==1.4.1
mypy-extensions==1.0.0
python-dateutil==2.8.2
s3transfer==0.10.0
six==1.16.0
tomli==2.0.1
typing-extensions==4.9.0
urllib3==1.26.18
wrapt==1.16.0
from typing import Any
from aws_lambda_powertools.utilities.data_classes import SQSEvent, event_source
from aws_lambda_powertools.utilities.typing import LambdaContext
@event_source(data_class=SQSEvent)
def handler(event: SQSEvent, _context: LambdaContext) -> dict[str, Any]:
    return {}
❯ mypy --strict main.py       
main.py:4: error: Untyped decorator makes function "handler" untyped  [misc]
Found 1 error in 1 file (checked 1 source file)

@leandrodamascena leandrodamascena added help wanted Could use a second pair of eyes/hands and removed triage Pending triage from maintainers labels Dec 30, 2023
@robmoss2k
Copy link

The simple fix for this to remove the error for event_source would be to add -> Any to def event_source, is it not? You already have the fact that the callable returns that type in the definition: handler: Callable[[Any, LambdaContext], Any],, so it is, in fact, also the correct fix.

@robmoss2k robmoss2k linked a pull request Apr 19, 2024 that will close this issue
7 tasks
@robmoss2k
Copy link

@leandrodamascena - PR with fix submitted. Quite possibly the smallest one you'll ever see...

@leandrodamascena leandrodamascena linked a pull request Apr 19, 2024 that will close this issue
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Could use a second pair of eyes/hands typing Static typing definition related issues (mypy, pyright, etc.)
Projects
Status: Backlog
Development

Successfully merging a pull request may close this issue.

3 participants