Skip to content

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

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
JakubSosnovec opened this issue Dec 28, 2023 · 11 comments
Closed

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

JakubSosnovec opened this issue Dec 28, 2023 · 11 comments
Labels
rejected 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
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
@manmartgarc
Copy link

Maybe unrelated, but I'm having trouble decorating an instance method, for example:

class MyClass:
    @event_source(MyDataClass)
    def handle(self, event: MyDataClass, context: LambdaContext) -> None:
         return self._handle_inner(event, context)

    def _handle_inner(self, event: Any, context: LambdaContext) -> None:
        return None

Getting this error:

MyClass._handle_inner() takes 2 positional arguments but 3 were given

@heitorlessa
Copy link
Contributor

oh hey @manmartgarc that's because the decorator doesn't support class methods, only functions.

The error but 3 were given is because the self argument of the class is being injected.

@manmartgarc
Copy link

manmartgarc commented Jun 7, 2024

oh hey @manmartgarc that's because the decorator doesn't support class methods, only functions.

The error but 3 were given is because the self argument of the class is being injected.

Thanks. I figured it would support them. Is there a reason they're not supported and if not, is there a plan to support them? If we're just waiting on a PR, I'm happy to submit one.

@manmartgarc
Copy link

hey @heitorlessa, was there a conscious decision to not support class methods? I think we can make the typing work to support both; can I work on this feature?

@leandrodamascena
Copy link
Contributor

Hey @manmartgarc! Please go ahead with this PR and let me know if you need any help!

If you're planning to start working on this right now, please fork a new branch from the v3 branch.

@dreamorosi dreamorosi moved this from Backlog to Closed in Powertools for AWS Lambda (Python) Jan 24, 2025
@dreamorosi dreamorosi added rejected and removed help wanted Could use a second pair of eyes/hands labels Jan 24, 2025
@anafalcao
Copy link
Contributor

Closing this issue as it's been open for a while now, and we didn't have much customer feedback.
Please open a new one if it's still an issue.

@anafalcao anafalcao closed this as not planned Won't fix, can't repro, duplicate, stale Jan 24, 2025
@github-project-automation github-project-automation bot moved this from Closed to Coming soon in Powertools for AWS Lambda (Python) Jan 24, 2025
@dreamorosi dreamorosi moved this from Coming soon to Closed in Powertools for AWS Lambda (Python) Jan 24, 2025
Copy link
Contributor

⚠️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
rejected typing Static typing definition related issues (mypy, pyright, etc.)
Projects
Development

Successfully merging a pull request may close this issue.

7 participants