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: PydanticSerializer for idempotency utility does not support Optional return types #5589

Closed
TonySherman opened this issue Nov 19, 2024 · 2 comments · Fixed by #5590
Closed
Assignees
Labels
bug Something isn't working idempotency Idempotency utility pending-release Fix or implementation already in dev waiting to be released

Comments

@TonySherman
Copy link
Contributor

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__
@leandrodamascena
Copy link
Contributor

Hey @TonySherman! Thanks for opening this issue!

I'm sending a PR to fix this.

@leandrodamascena leandrodamascena moved this from Triage to Working on it in Powertools for AWS Lambda (Python) Nov 19, 2024
@leandrodamascena leandrodamascena self-assigned this Nov 19, 2024
@leandrodamascena leandrodamascena added idempotency Idempotency utility and removed triage Pending triage from maintainers labels Nov 19, 2024
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in Powertools for AWS Lambda (Python) Dec 23, 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 23, 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 idempotency Idempotency utility pending-release Fix or implementation already in dev waiting to be released
Projects
Status: Coming soon
2 participants