|
| 1 | +import functools |
| 2 | +import sys |
| 3 | +from typing import TYPE_CHECKING, Annotated, get_args, get_origin, no_type_check |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from aws_lambda_powertools.utilities.idempotency import IdempotencyConfig, idempotent_function |
| 8 | +from aws_lambda_powertools.utilities.idempotency.serialization.base import BaseIdempotencyModelSerializer |
| 9 | +from aws_lambda_powertools.utilities.idempotency.serialization.dataclass import DataclassSerializer |
| 10 | +from aws_lambda_powertools.utilities.idempotency.serialization.pydantic import PydanticSerializer |
| 11 | +from tests.functional.idempotency._pydantic.test_idempotency_with_postponed_annotations import ( |
| 12 | + MOCK_EVENT, |
| 13 | + NoOpPersistenceLayer, |
| 14 | + PaymentOutput, |
| 15 | + PaymentOutputDataclass, |
| 16 | +) |
| 17 | + |
| 18 | +if TYPE_CHECKING: |
| 19 | + from collections.abc import Mapping as InputOnlyAvailableToTypeChecker |
| 20 | + |
| 21 | + |
| 22 | +class MetadataSerializer(BaseIdempotencyModelSerializer): |
| 23 | + @classmethod |
| 24 | + def instantiate(cls, model_type): |
| 25 | + assert get_origin(model_type) is Annotated |
| 26 | + model, metadata = get_args(model_type) |
| 27 | + assert metadata == "v1" |
| 28 | + return PydanticSerializer(model=model) |
| 29 | + |
| 30 | + def to_dict(self, data): ... |
| 31 | + |
| 32 | + def from_dict(self, data): ... |
| 33 | + |
| 34 | + |
| 35 | +def replay(function, serializer, expected): |
| 36 | + config = IdempotencyConfig(use_local_cache=True) |
| 37 | + |
| 38 | + # This fake Lambda context avoids depending on DynamoDB or warning about missing context. |
| 39 | + class Context: |
| 40 | + def get_remaining_time_in_millis(self): |
| 41 | + return 1000 |
| 42 | + |
| 43 | + config.register_lambda_context(Context()) |
| 44 | + decorated = idempotent_function( |
| 45 | + function, |
| 46 | + data_keyword_argument="payment", |
| 47 | + persistence_store=NoOpPersistenceLayer(), |
| 48 | + config=config, |
| 49 | + output_serializer=serializer, |
| 50 | + ) |
| 51 | + assert decorated(payment=MOCK_EVENT) == expected |
| 52 | + assert decorated(payment=MOCK_EVENT) == expected |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("postponed", [False, True]) |
| 56 | +def test_custom_serializer_keeps_annotated_metadata(postponed): |
| 57 | + calls = [] |
| 58 | + |
| 59 | + def collect_payment(payment: dict) -> Annotated[PaymentOutput, "v1"]: |
| 60 | + calls.append(payment) |
| 61 | + return PaymentOutput(**payment) |
| 62 | + |
| 63 | + if postponed: |
| 64 | + collect_payment.__annotations__["return"] = "Annotated[PaymentOutput, 'v1']" |
| 65 | + |
| 66 | + replay(collect_payment, MetadataSerializer, PaymentOutput(**MOCK_EVENT)) |
| 67 | + assert len(calls) == 1 |
| 68 | + |
| 69 | + |
| 70 | +@pytest.mark.parametrize("postponed", [False, True]) |
| 71 | +def test_no_type_check_keeps_return_model(postponed): |
| 72 | + calls = [] |
| 73 | + |
| 74 | + @no_type_check |
| 75 | + def collect_payment(payment: dict) -> PaymentOutput: |
| 76 | + calls.append(payment) |
| 77 | + return PaymentOutput(**payment) |
| 78 | + |
| 79 | + if postponed: |
| 80 | + collect_payment.__annotations__["return"] = "PaymentOutput" |
| 81 | + |
| 82 | + replay(collect_payment, PydanticSerializer, PaymentOutput(**MOCK_EVENT)) |
| 83 | + assert len(calls) == 1 |
| 84 | + |
| 85 | + |
| 86 | +@pytest.mark.parametrize( |
| 87 | + "model,serializer", |
| 88 | + [(PaymentOutput, PydanticSerializer), (PaymentOutputDataclass, DataclassSerializer)], |
| 89 | +) |
| 90 | +def test_unresolved_input_annotation_does_not_block_return_model(model, serializer): |
| 91 | + calls = [] |
| 92 | + |
| 93 | + def collect_payment(payment: "InputOnlyAvailableToTypeChecker"): |
| 94 | + calls.append(payment) |
| 95 | + return model(**payment) |
| 96 | + |
| 97 | + collect_payment.__annotations__["return"] = model.__name__ |
| 98 | + replay(collect_payment, serializer, model(**MOCK_EVENT)) |
| 99 | + assert len(calls) == 1 |
| 100 | + |
| 101 | + |
| 102 | +def test_wrapped_function_resolves_return_in_original_namespace(): |
| 103 | + calls = [] |
| 104 | + |
| 105 | + def collect_payment(payment: dict) -> "PaymentOutput": |
| 106 | + calls.append(payment) |
| 107 | + return PaymentOutput(**payment) |
| 108 | + |
| 109 | + # A wrapper from a different module has no PaymentOutput in its globals. |
| 110 | + namespace = {"functools": functools, "original": collect_payment} |
| 111 | + exec( |
| 112 | + compile( |
| 113 | + "@functools.wraps(original)\ndef wrapper(*args, **kwargs):\n return original(*args, **kwargs)\n", |
| 114 | + "<external_wrapper>", |
| 115 | + "exec", |
| 116 | + dont_inherit=True, |
| 117 | + ), |
| 118 | + namespace, |
| 119 | + ) |
| 120 | + replay(namespace["wrapper"], PydanticSerializer, PaymentOutput(**MOCK_EVENT)) |
| 121 | + assert len(calls) == 1 |
| 122 | + |
| 123 | + |
| 124 | +@pytest.mark.skipif(sys.version_info < (3, 14), reason="Native deferred annotations require Python 3.14") |
| 125 | +def test_native_deferred_annotations_allow_unresolved_input(): |
| 126 | + calls = [] |
| 127 | + namespace = {"PaymentOutput": PaymentOutput, "calls": calls} |
| 128 | + exec( |
| 129 | + compile( |
| 130 | + "def collect_payment(payment: InputOnlyAvailableToTypeChecker) -> PaymentOutput:\n" |
| 131 | + " calls.append(payment)\n" |
| 132 | + " return PaymentOutput(**payment)\n", |
| 133 | + "<native_deferred_annotations>", |
| 134 | + "exec", |
| 135 | + dont_inherit=True, |
| 136 | + ), |
| 137 | + namespace, |
| 138 | + ) |
| 139 | + replay(namespace["collect_payment"], PydanticSerializer, PaymentOutput(**MOCK_EVENT)) |
| 140 | + assert len(calls) == 1 |
0 commit comments