Skip to content

Code using the event_source decorator fails pyright checks #1085

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
ericbn opened this issue Mar 29, 2022 · 7 comments
Closed

Code using the event_source decorator fails pyright checks #1085

ericbn opened this issue Mar 29, 2022 · 7 comments
Assignees
Labels
typing Static typing definition related issues (mypy, pyright, etc.)

Comments

@ericbn
Copy link
Contributor

ericbn commented Mar 29, 2022

What were you trying to accomplish?
Check code that uses aws_lambda_powertools using pyright.

Expected Behavior

Checking code that correctly uses aws_lambda_powertools should be successful.

Current Behavior

Checking code that uses the event_source decorator as documented fails with:

lambda_function.py
  lambda_function.py:6:15 - error: No parameter named "data_class" (reportGeneralTypeIssues)
  lambda_function.py:6:2 - error: Argument missing for parameter "context" (reportGeneralTypeIssues)

Here's the code used:

import json

from aws_lambda_powertools.utilities.data_classes import event_source, APIGatewayProxyEventV2


@event_source(data_class=APIGatewayProxyEventV2)
def lambda_handler(event: APIGatewayProxyEventV2, context):
    return {
        "statusCode": 200,
        "body": json.dumps({"message": f"hello {event.json_body['name']}!"})
    }

Other types might be affected too. Here's the output when pyright with the special option "--verifytypes" is called on aws_lambda_powertools:

Symbols exported by "aws_lambda_powertools": 2095
  With known type: 1733
  With ambiguous type: 77
  With unknown type: 285

Type completeness score: 82.7%

This means about 17% of the symbols exposed are not properly annotated.

See this comment here.

Possible Solution

Complete the type annotations.

Steps to Reproduce (for bugs)

❯ python3 -m venv venv
❯ . venv/bin/activate
❯ pip install aws_lambda_powertools pyright
❯ mkdir src
❯ >>! src/lambda_function.py <<\EOF
import json

from aws_lambda_powertools.utilities.data_classes import event_source, APIGatewayProxyEventV2


@event_source(data_class=APIGatewayProxyEventV2)
def lambda_handler(event: APIGatewayProxyEventV2, context):
    return {
        "statusCode": 200,
        "body": json.dumps({"message": f"hello {event.json_body['name']}!"})
    }
EOF
❯ >>! pyproject.toml <<\EOF
[tool.pyright]
pythonPlatform = "Linux"
pythonVersion = "3.9"
stubPath = ""
executionEnvironments = [
    {root = "src"},
]
EOF
❯ pyright
No configuration file found.
pyproject.toml file found at /tmp/test.
Loading pyproject.toml file at /tmp/test/pyproject.toml
No include entries specified; assuming /tmp/test
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
Searching for source files
Auto-excluding /tmp/test/venv
Found 1 source file
/tmp/test/src/lambda_function.py
  /tmp/test/src/lambda_function.py:6:15 - error: No parameter named "data_class" (reportGeneralTypeIssues)
  /tmp/test/src/lambda_function.py:6:2 - error: Argument missing for parameter "context" (reportGeneralTypeIssues)
2 errors, 0 warnings, 0 informations
Completed in 0.767sec

Environment

  • Powertools version used: 1.25.5
  • Packaging format (Layers, PyPi): N/A
  • AWS Lambda function runtime: N/A
  • Debugging logs
    N/A
@ericbn ericbn added bug Something isn't working triage Pending triage from maintainers labels Mar 29, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Mar 29, 2022

Thanks for opening your first issue here! We'll come back to you as soon as we can.

@michaelbrewer
Copy link
Contributor

Related to #1060

@heitorlessa heitorlessa added typing Static typing definition related issues (mypy, pyright, etc.) and removed bug Something isn't working triage Pending triage from maintainers labels Apr 12, 2022
@heitorlessa
Copy link
Contributor

Hi @ericbn, thank you for opening your first issue and flagging a type def to us!

As of now, we don't support Pyright but Mypy (non-strict mode) - we had mixed results last year that. When trying so, we found it quite difficult to keep the code maintainable and ensure 100% accuracy due to heavy use of metaprogramming (plus supporting older versions of Python that don't yet have certain mechanisms).

I'm changing the label from bug to static_typing. We welcome any targeted solution to this specific type def that also works for 3.6+ as well as Mypy. We had tried some solutions in #1060 but wasn't enough to cover different Python versions and satisfy either Mypy in strict mode as well as Pyright - We do want to revisit it in the future, tho.

Thank you!

@heitorlessa heitorlessa added the need-customer-feedback Requires more customers feedback before making or revisiting a decision label Apr 12, 2022
@heitorlessa
Copy link
Contributor

Forgot to also thank you for checking upstream. That comment was helpful to understand how much Pyright progressed since we tried early last year, and how we could use that info to improve our existing type def in the future.

@heitorlessa heitorlessa added the help wanted Could use a second pair of eyes/hands label Mar 10, 2023
@heitorlessa heitorlessa self-assigned this Apr 21, 2023
@heitorlessa
Copy link
Contributor

Checking this a year later... apparently Pyright is happy with us now. Nevertheless, I'll go try type the decorator factory as this will help us towards mypy strict which is related.

> pyright --verbose
No configuration file found.
pyproject.toml file found at /private/tmp/pyright-test.
Loading pyproject.toml file at /private/tmp/pyright-test/pyproject.toml
No include entries specified; assuming /private/tmp/pyright-test
Auto-excluding **/node_modules
Auto-excluding **/__pycache__
Auto-excluding **/.*
Search paths for /private/tmp/pyright-test/src
  /Users/lessa/.cache/pyright-python/1.1.304/node_modules/pyright/dist/typeshed-fallback/stdlib
  /private/tmp/pyright-test/src
  /private/tmp/pyright-test
  /Users/lessa/.cache/pyright-python/1.1.304/node_modules/pyright/dist/typeshed-fallback/stubs/...
  /Users/lessa/.pyenv/versions/3.10.11/lib/python3.10
  /Users/lessa/.pyenv/versions/3.10.11/lib/python3.10/lib-dynload
  /Users/lessa/.pyenv/versions/3.10.11/lib/python3.10/site-packages
Searching for source files
Found 1 source file
pyright 1.1.304
0 errors, 0 warnings, 0 informations 
Completed in 0.485sec

pyproject.toml

[tool.pyright]
pythonPlatform = "Linux"
pythonVersion = "3.9"
stubPath = ""
executionEnvironments = [
{root = "src"},
]

src/lambda_function.py

import json

from aws_lambda_powertools.utilities.data_classes import event_source, APIGatewayProxyEventV2


@event_source(data_class=APIGatewayProxyEventV2)
def lambda_handler(event: APIGatewayProxyEventV2, context):
    return {
        "statusCode": 200,
        "body": json.dumps({"message": f"hello {event.json_body['name']}!"})
    }

@heitorlessa heitorlessa removed help wanted Could use a second pair of eyes/hands need-customer-feedback Requires more customers feedback before making or revisiting a decision labels Apr 21, 2023
@heitorlessa
Copy link
Contributor

Closing as narrowing types further hits a bug in ParamSpec when using Concatenate in a decorator factory logic: python/mypy#12595.

As of now, both mypy and pyright are working (please let us know otherwise).

@github-actions
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
typing Static typing definition related issues (mypy, pyright, etc.)
Projects
None yet
Development

No branches or pull requests

3 participants