Skip to content

Bug: PydanticSerializer for idempotency utility does not support Optional return types #5589

Closed
@TonySherman

Description

@TonySherman

Expected Behaviour

The PydanticSerializer with the idempotent_function decorator should support an optional return or raise a clear error message (along with doc updates)

Current Behaviour

When using the PydanticSerializer with the idempotent_function decorator, if the return can be None( Optional or MyModel | None), the utility will throw an error.

Code snippet

@idempotent_function(
    data_keyword_argument="order",
    config=config,
    persistence_store=dynamodb,
    output_serializer=PydanticSerializer(model=OrderOutput),
)
def process_order(order: Order) -> OrderOutput | None:
    ...
    if order:
        return OrderOutput(order_id=order.order_id)

    return None

Possible Solution

Allow for optional returns (maybe check for this scenario) or raise an error message that explains this limitation (update docs too)

Steps to Reproduce

Use idempotent decorator with PydanticSerializer. Set return type to Optional or MyModel | None.

Powertools for AWS Lambda (Python) version

2.43

AWS Lambda function runtime

3.11

Packaging format used

PyPi

Debugging logs

ERROR] TypeError: issubclass() arg 1 must be a class
Traceback (most recent call last):
  File "/var/lang/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/var/task/lambda_function.py", line 1, in <module>
    from ***.lambda_function import handler as handler
  File "/var/task/***/lambda_function.py", line 24, in <module>
    @idempotent_function(
  File "/var/task/aws_lambda_powertools/utilities/idempotency/idempotency.py", line 158, in idempotent_function
    output_serializer = output_serializer.instantiate(function.__annotations__.get("return", None))
  File "/var/task/aws_lambda_powertools/utilities/idempotency/serialization/pydantic.py", line 44, in instantiate
    if not issubclass(model_type, BaseModel):
  File "<frozen abc>", line 123, in __subclasscheck__

Metadata

Metadata

Labels

bugSomething isn't workingidempotencyIdempotency utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions