Description
Expected Behaviour
The expected behavior is that exceptions related to Powertools do not arise from the event_parser decorator in code like the following:
from aws_lambda_powertools.utilities.parser import event_parser
from pydantic import BaseModel
class EventModel(BaseModel):
"""It's just a sample, so the content can be anything."""
val: str
@event_parser(model=EventModel)
def lambda_handler(event: EventModel, context):
print(event) # correctly parsed
a = "aaa"
a.property # raise AttributeError (**NOT related to event_parser**)
# The following is intended for simple reproduction in a local environment:
if __name__ == "__main__":
lambda_handler({"val": "hello, lambda"}, None)
Assume that the event
is in a format that matches the EventModel
by pydantic and is correctly parsed.
Therefore, the exception happening in the above code has nothing to do with powertools event_parser.
Current Behaviour
InvalidModelTypeError occurs within powertools, as shown below:
raise InvalidModelTypeError(f"Error: {str(exc)}. Please ensure the type you're trying to parse into is correct") aws_lambda_powertools.utilities.parser.exceptions.InvalidModelTypeError: Error: 'str' object has no attribute 'property'. Please ensure the type you're trying to parse into is correct
I consider it very confusing because even if the event validation in the event_parser decorator succeeds, the above exception is raised.
Code snippet
Same as described in “Expected Behaviour”.
Possible Solution
Currently (v3.3.0), AttributeError is caught at the error location in powertools as follows:
Is it problematic to rewrite this part to catch Pydantic-derived Exceptions (e.g. pydantic.ValidationError) instead of AttributeError?
Steps to Reproduce
- Python version: 3.12
- architecture: x86_64
- The libraries used are the same versions as those installed by the following requirements.txt file:
# requirements.txt
annotated-types==0.7.0
aws-lambda-powertools==3.3.0
jmespath==1.0.1
pydantic==2.10.3
pydantic-core==2.27.1
typing-extensions==4.12.2
Powertools for AWS Lambda (Python) version
3.3.0
AWS Lambda function runtime
3.12
Packaging format used
PyPi
Debugging logs
Metadata
Metadata
Assignees
Type
Projects
Status
Activity
boring-cyborg commentedon Dec 7, 2024
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
anafalcao commentedon Dec 12, 2024
Hey @junkor-1011 ! Thank you so much for opening this issue. We'll evaluate and get back to you soon. 🚀
github-actions commentedon Dec 20, 2024
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 commentedon Dec 20, 2024
This is now released under 3.4.0 version!