Skip to content

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

Closed
@junkor-1011

Description

@junkor-1011

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

Activity

added
bugSomething isn't working
triagePending triage from maintainers
on Dec 7, 2024
boring-cyborg

boring-cyborg commented on Dec 7, 2024

@boring-cyborg

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

anafalcao commented on Dec 12, 2024

@anafalcao
Contributor

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

added
on-holdThis item is on-hold and will be revisited in the future
and removed
triagePending triage from maintainers
on Dec 16, 2024
github-actions

github-actions commented on Dec 20, 2024

@github-actions
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 commented on Dec 20, 2024

@github-actions
Contributor

This is now released under 3.4.0 version!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingon-holdThis item is on-hold and will be revisited in the future

    Type

    No type

    Projects

    Status

    Shipped

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @anafalcao@junkor-1011

      Issue actions

        Bug: event_parser decorator catches an irrelevant AttributeError and raises InvalidModelTypeError exception. · Issue #5705 · aws-powertools/powertools-lambda-python