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