aws-powertools / powertools-lambda-python

A developer toolkit to implement Serverless best practices and increase developer velocity.
https://docs.powertools.aws.dev/lambda/python/latest/
MIT No Attribution
2.8k stars 389 forks source link

Bug: Pydantic is being imported when using Event Handler without validation #3370

Closed leandrodamascena closed 9 months ago

leandrodamascena commented 9 months ago

Expected Behaviour

I'm using ApiGatewayResolver without enabling validation and it shouldn't import Pydantic. Even if a customer has Pydantic installed, they should not import Pydantic without explicitly enabling validation: app = ApiGatewayResolver(enable_validation=True)

Current Behaviour

Profiling the application with tuna, I can see Pydantic being imported:

image

Code snippet

from aws_lambda_powertools.event_handler import ApiGatewayResolver

app = ApiGatewayResolver(enable_validation=False)

def lambda_handler(event, context):
    return app.resolve(event, context)

requirements.txt
aws-lambda-powertools==2.27.0
aws-xray-sdk==2.12.1
botocore==1.32.3
fastjsonschema==2.19.0
jmespath==1.0.1
pydantic==1.10.13
python-dateutil==2.8.2
six==1.16.0
tuna==0.5.11
typing_extensions==4.8.0
urllib3==2.0.7
wrapt==1.16.0

Possible Solution

Move these imports to an standalone file or import them only when enable_validation is True.

Steps to Reproduce

1 - Create a file 2 - pip install aws-lambda-powertools[all] tuna 3 - Use the code snippet provided 4 - python -X importtime test.py 2> prof1.log 5 - tuna prof1.log

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.11

Packaging format used

PyPi

Debugging logs

No response

rubenfonseca commented 9 months ago

Working at this now

github-actions[bot] commented 9 months ago

⚠️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[bot] commented 9 months ago

This is now released under 2.28.0 version!