Skip to content
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

Bug: event_parser decorator catches an irrelevant AttributeError and raises InvalidModelTypeError exception. #5705

Closed
junkor-1011 opened this issue Dec 7, 2024 · 4 comments · Fixed by #5742
Labels
bug Something isn't working on-hold This item is on-hold and will be revisited in the future

Comments

@junkor-1011
Copy link

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 inExpected Behaviour”.

Possible Solution

Currently (v3.3.0), AttributeError is caught at the error location in powertools as follows:

https://github.com/aws-powertools/powertools-lambda-python/blob/v3.3.0/aws_lambda_powertools/utilities/parser/parser.py#L114

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

@junkor-1011 junkor-1011 added bug Something isn't working triage Pending triage from maintainers labels Dec 7, 2024
Copy link

boring-cyborg bot commented 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
Copy link
Collaborator

anafalcao commented Dec 12, 2024

Hey @junkor-1011 ! Thank you so much for opening this issue. We'll evaluate and get back to you soon. 🚀

@anafalcao anafalcao added on-hold This item is on-hold and will be revisited in the future and removed triage Pending triage from maintainers labels Dec 16, 2024
@github-project-automation github-project-automation bot moved this from Triage to Coming soon in Powertools for AWS Lambda (Python) Dec 20, 2024
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.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Dec 20, 2024
Copy link
Contributor

This is now released under 3.4.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working on-hold This item is on-hold and will be revisited in the future
Projects
Status: Coming soon
2 participants