Skip to content

Pydantic required when using BatchProcessor utiltiies #2156

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

Closed
walmsles opened this issue Apr 21, 2023 · 4 comments
Closed

Pydantic required when using BatchProcessor utiltiies #2156

walmsles opened this issue Apr 21, 2023 · 4 comments
Assignees
Labels
bug Something isn't working

Comments

@walmsles
Copy link
Contributor

walmsles commented Apr 21, 2023

Expected Behaviour

I have installed aws-lambda-powertools without any optional dependencies and am not using any parser features. I expect to be able to use Batch Processor without any errors.

When including BatchProcessor to process SQS Records (without Models) batch/base.py imports "ValidationError" from parser which requires the "pydantic" dependency and breaks the optional constraint.

Current Behaviour

ModuleNotFoundError: No module named 'pydantic'

Code snippet

import json

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
from aws_lambda_powertools.utilities.parser.models import SqsRecordModel
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.utilities.parser import BaseModel
from aws_lambda_powertools.utilities.parser.types import Json


class Order(BaseModel):
    item: dict

class OrderSqsRecord(SqsRecordModel):
    body: Json[Order]  # deserialize order data from JSON string

processor = BatchProcessor(event_type=EventType.SQS, model=OrderSqsRecord)
tracer = Tracer()
logger = Logger()


@tracer.capture_method
def record_handler(record: OrderSqsRecord):
    return record.body.item

@logger.inject_lambda_context
@tracer.capture_lambda_handler
def lambda_handler(event, context: LambdaContext):
    return process_partial_response(event=event, record_handler=record_handler, processor=processor, context=context)

Possible Solution

remove line 29. Change Exception handling to use String Comparison instead to detect ValidationError so works whether Pydantic is available or not?

e.g.
Instead of:

        except ValidationError:
            return self._register_model_validation_error_record(record)
        except Exception:
            return self.failure_handler(record=data, exception=sys.exc_info())

Perhaps:

        except Exception as exc:
            if exc.__class__.__name__ == "ValidationError":
                return self._register_model_validation_error_record(record)
            return self.failure_handler(record=data, exception=sys.exc_info())

Steps to Reproduce

  1. install aws-lambda-powertools with no optional dependecies
  2. Take code snippet from BatchProcessor page for SQS
  3. Try and run it using Python
  4. GET Module IMport Error

AWS Lambda Powertools for Python version

latest

AWS Lambda function runtime

3.9

Packaging format used

PyPi

Debugging logs

2023-04-21T21:36:02.153+10:00

Copy
[ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'pydantic'
Traceback (most recent call last):
[ERROR] Runtime.ImportModuleError: Unable to import module 'app': No module named 'pydantic' Traceback (most recent call last):
@walmsles walmsles added bug Something isn't working triage Pending triage from maintainers labels Apr 21, 2023
@walmsles
Copy link
Contributor Author

Fixing is quite trivial - remove the import of ValidationError (which has parser requirement) and use a string class name check instead.

What is needed is a run of tests that check aws-lambda-powertools without optional dependencies to catch this kind of thing in the future 😬

@heitorlessa
Copy link
Contributor

looking

@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Apr 21, 2023
@heitorlessa heitorlessa self-assigned this Apr 21, 2023
@heitorlessa
Copy link
Contributor

Now released in PyPi 2.14.1 - Layers coming soon. Closing as I know you're using PyPi <3 THANK YOU one more time.

The snippet is wrong but the intent and bug were real ;)

@github-actions
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants