Skip to content

Commit 39b27a0

Browse files
committed
fix: reading improvements
1 parent ebacef8 commit 39b27a0

File tree

5 files changed

+21
-17
lines changed

5 files changed

+21
-17
lines changed

Diff for: aws_lambda_powertools/utilities/idempotency/exceptions.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class IdempotencyKeyError(BaseError):
7575

7676
class IdempotencyModelTypeError(BaseError):
7777
"""
78-
Model type does not match expected type
78+
Model type does not match expected payload output
7979
"""
8080

8181

Diff for: aws_lambda_powertools/utilities/idempotency/idempotency.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import functools
55
import logging
66
import os
7-
from inspect import isclass
87
from typing import Any, Callable, Dict, Optional, Type, Union, cast
98

109
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
@@ -108,8 +107,8 @@ def idempotent_function(
108107
output_serializer: Optional[Union[BaseIdempotencySerializer, Type[BaseIdempotencyModelSerializer]]]
109108
Serializer to transform the data to and from a dictionary.
110109
If not supplied, no serialization is done via the NoOpSerializer.
111-
In case a serializer of type inheriting BaseIdempotencyModelSerializer] is given,
112-
the serializer is deduced from the function return type.
110+
In case a serializer of type inheriting BaseIdempotencyModelSerializer is given,
111+
the serializer is derived from the function return type.
113112
Examples
114113
--------
115114
**Processes an order in an idempotent manner**
@@ -137,7 +136,8 @@ def process_order(customer_id: str, order: dict, **kwargs):
137136
output_serializer=output_serializer,
138137
),
139138
)
140-
if isclass(output_serializer) and issubclass(output_serializer, BaseIdempotencyModelSerializer):
139+
140+
if issubclass(output_serializer, BaseIdempotencyModelSerializer):
141141
# instantiate an instance of the serializer class
142142
output_serializer = output_serializer.instantiate(function.__annotations__.get("return", None))
143143

Diff for: aws_lambda_powertools/utilities/idempotency/serialization/base.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ class BaseIdempotencyModelSerializer(BaseIdempotencySerializer):
2828
@abstractmethod
2929
def instantiate(cls, model_type: Any) -> BaseIdempotencySerializer:
3030
"""
31-
Instantiate the serializer from the given model type.
32-
In case there is the model_type is unknown, None will be sent to the method.
31+
Creates an instance of a serializer based on a provided model type.
32+
In case the model_type is unknown, None will be sent as `model_type`.
3333
It's on the implementer to verify that:
34-
- None is handled
34+
- None is handled correctly
3535
- A model type not matching the expected types is handled
3636
3737
Parameters

Diff for: aws_lambda_powertools/utilities/idempotency/serialization/dataclass.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,16 @@
1414

1515

1616
class DataclassSerializer(BaseIdempotencyModelSerializer):
17+
"""
18+
A serializer class for transforming data between dataclass objects and dictionaries.
19+
"""
20+
1721
def __init__(self, model: Type[DataClass]):
1822
"""
1923
Parameters
2024
----------
21-
model: Model
22-
A Pydantic model of the type to transform
25+
model: Type[DataClass]
26+
A dataclass type to be used for serialization and deserialization
2327
"""
2428
self.__model: Type[DataClass] = model
2529

Diff for: aws_lambda_powertools/utilities/idempotency/serialization/pydantic.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@
1111
BaseIdempotencySerializer,
1212
)
1313

14-
Model = BaseModel
15-
1614

1715
class PydanticSerializer(BaseIdempotencyModelSerializer):
18-
def __init__(self, model: Type[Model]):
16+
"""Pydantic serializer for idempotency models"""
17+
18+
def __init__(self, model: Type[BaseModel]):
1919
"""
2020
Parameters
2121
----------
2222
model: Model
23-
A Pydantic model of the type to transform
23+
Pydantic model to be used for serialization
2424
"""
25-
self.__model: Type[Model] = model
25+
self.__model: Type[BaseModel] = model
2626

27-
def to_dict(self, data: Model) -> Dict:
27+
def to_dict(self, data: BaseModel) -> Dict:
2828
if callable(getattr(data, "model_dump", None)):
2929
# Support for pydantic V2
3030
return data.model_dump() # type: ignore[unused-ignore,attr-defined]
3131
return data.dict()
3232

33-
def from_dict(self, data: Dict) -> Model:
33+
def from_dict(self, data: Dict) -> BaseModel:
3434
if callable(getattr(self.__model, "model_validate", None)):
3535
# Support for pydantic V2
3636
return self.__model.model_validate(data) # type: ignore[unused-ignore,attr-defined]

0 commit comments

Comments
 (0)