Closed
Description
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
- install aws-lambda-powertools with no optional dependecies
- Take code snippet from BatchProcessor page for SQS
- Try and run it using Python
- 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):