Skip to content

Commit 39b3136

Browse files
Michael Brewerheitorlessa
Michael Brewer
andauthored
chore: minor housekeeping before release (#912)
Co-authored-by: heitorlessa <[email protected]>
1 parent 849e003 commit 39b3136

File tree

7 files changed

+38
-25
lines changed

7 files changed

+38
-25
lines changed

aws_lambda_powertools/utilities/batch/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
BasePartialProcessor,
99
BatchProcessor,
1010
EventType,
11-
ExceptionInfo,
1211
FailureResponse,
1312
SuccessResponse,
1413
batch_processor,
1514
)
15+
from aws_lambda_powertools.utilities.batch.exceptions import ExceptionInfo
1616
from aws_lambda_powertools.utilities.batch.sqs import PartialSQSProcessor, sqs_batch_processor
1717

1818
__all__ = (

aws_lambda_powertools/utilities/batch/base.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88
import sys
99
from abc import ABC, abstractmethod
1010
from enum import Enum
11-
from types import TracebackType
1211
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union, overload
1312

1413
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
15-
from aws_lambda_powertools.utilities.batch.exceptions import BatchProcessingError
14+
from aws_lambda_powertools.utilities.batch.exceptions import BatchProcessingError, ExceptionInfo
1615
from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import DynamoDBRecord
1716
from aws_lambda_powertools.utilities.data_classes.kinesis_stream_event import KinesisStreamRecord
1817
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSRecord
@@ -30,8 +29,6 @@ class EventType(Enum):
3029
# type specifics
3130
#
3231
has_pydantic = "pydantic" in sys.modules
33-
ExceptionInfo = Tuple[Type[BaseException], BaseException, TracebackType]
34-
OptExcInfo = Union[ExceptionInfo, Tuple[None, None, None]]
3532

3633
# For IntelliSense and Mypy to work, we need to account for possible SQS, Kinesis and DynamoDB subclasses
3734
# We need them as subclasses as we must access their message ID or sequence number metadata via dot notation
@@ -61,7 +58,7 @@ class BasePartialProcessor(ABC):
6158
def __init__(self):
6259
self.success_messages: List[BatchEventTypes] = []
6360
self.fail_messages: List[BatchEventTypes] = []
64-
self.exceptions: List = []
61+
self.exceptions: List[ExceptionInfo] = []
6562

6663
@abstractmethod
6764
def _prepare(self):
@@ -132,15 +129,15 @@ def success_handler(self, record, result: Any) -> SuccessResponse:
132129
self.success_messages.append(record)
133130
return entry
134131

135-
def failure_handler(self, record, exception: OptExcInfo) -> FailureResponse:
132+
def failure_handler(self, record, exception: ExceptionInfo) -> FailureResponse:
136133
"""
137134
Keeps track of batch records that failed processing
138135
139136
Parameters
140137
----------
141138
record: Any
142139
record that failed processing
143-
exception: OptExcInfo
140+
exception: ExceptionInfo
144141
Exception information containing type, value, and traceback (sys.exc_info())
145142
146143
Returns
@@ -411,32 +408,28 @@ def _get_messages_to_report(self) -> Dict[str, str]:
411408
def _collect_sqs_failures(self):
412409
if self.model:
413410
return {"itemIdentifier": msg.messageId for msg in self.fail_messages}
414-
else:
415-
return {"itemIdentifier": msg.message_id for msg in self.fail_messages}
411+
return {"itemIdentifier": msg.message_id for msg in self.fail_messages}
416412

417413
def _collect_kinesis_failures(self):
418414
if self.model:
419415
# Pydantic model uses int but Lambda poller expects str
420416
return {"itemIdentifier": msg.kinesis.sequenceNumber for msg in self.fail_messages}
421-
else:
422-
return {"itemIdentifier": msg.kinesis.sequence_number for msg in self.fail_messages}
417+
return {"itemIdentifier": msg.kinesis.sequence_number for msg in self.fail_messages}
423418

424419
def _collect_dynamodb_failures(self):
425420
if self.model:
426421
return {"itemIdentifier": msg.dynamodb.SequenceNumber for msg in self.fail_messages}
427-
else:
428-
return {"itemIdentifier": msg.dynamodb.sequence_number for msg in self.fail_messages}
422+
return {"itemIdentifier": msg.dynamodb.sequence_number for msg in self.fail_messages}
429423

430424
@overload
431425
def _to_batch_type(self, record: dict, event_type: EventType, model: "BatchTypeModels") -> "BatchTypeModels":
432-
...
426+
... # pragma: no cover
433427

434428
@overload
435429
def _to_batch_type(self, record: dict, event_type: EventType) -> EventSourceDataClassTypes:
436-
...
430+
... # pragma: no cover
437431

438432
def _to_batch_type(self, record: dict, event_type: EventType, model: Optional["BatchTypeModels"] = None):
439433
if model is not None:
440434
return model.parse_obj(record)
441-
else:
442-
return self._DATA_CLASS_MAPPING[event_type](record)
435+
return self._DATA_CLASS_MAPPING[event_type](record)

aws_lambda_powertools/utilities/batch/exceptions.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
Batch processing exceptions
33
"""
44
import traceback
5-
from typing import Optional, Tuple
5+
from types import TracebackType
6+
from typing import List, Optional, Tuple, Type
7+
8+
ExceptionInfo = Tuple[Type[BaseException], BaseException, TracebackType]
69

710

811
class BaseBatchProcessingError(Exception):
9-
def __init__(self, msg="", child_exceptions=()):
12+
def __init__(self, msg="", child_exceptions: Optional[List[ExceptionInfo]] = None):
1013
super().__init__(msg)
1114
self.msg = msg
1215
self.child_exceptions = child_exceptions
@@ -24,7 +27,7 @@ def format_exceptions(self, parent_exception_str):
2427
class SQSBatchProcessingError(BaseBatchProcessingError):
2528
"""When at least one message within a batch could not be processed"""
2629

27-
def __init__(self, msg="", child_exceptions: Optional[Tuple[Exception]] = None):
30+
def __init__(self, msg="", child_exceptions: Optional[List[ExceptionInfo]] = None):
2831
super().__init__(msg, child_exceptions)
2932

3033
# Overriding this method so we can output all child exception tracebacks when we raise this exception to prevent
@@ -37,7 +40,7 @@ def __str__(self):
3740
class BatchProcessingError(BaseBatchProcessingError):
3841
"""When all batch records failed to be processed"""
3942

40-
def __init__(self, msg="", child_exceptions: Optional[Tuple[Exception]] = None):
43+
def __init__(self, msg="", child_exceptions: Optional[List[ExceptionInfo]] = None):
4144
super().__init__(msg, child_exceptions)
4245

4346
def __str__(self):

aws_lambda_powertools/utilities/parser/parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,12 @@ def handler(event: Order, context: LambdaContext):
8686

8787
@overload
8888
def parse(event: Dict[str, Any], model: Type[Model]) -> Model:
89-
...
89+
... # pragma: no cover
9090

9191

9292
@overload
9393
def parse(event: Dict[str, Any], model: Type[Model], envelope: Type[Envelope]) -> EnvelopeModel:
94-
...
94+
... # pragma: no cover
9595

9696

9797
def parse(event: Dict[str, Any], model: Type[Model], envelope: Optional[Type[Envelope]] = None):

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ exclude_lines = [
8585
# Don't complain if non-runnable code isn't run:
8686
"if 0:",
8787
"if __name__ == .__main__.:",
88+
89+
# Ignore type function overload
90+
"@overload",
8891
]
8992

9093
[tool.isort]

tests/functional/idempotency/test_idempotency.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,3 +1057,15 @@ def two(data):
10571057
assert one(data=mock_event) == "one"
10581058
assert two(data=mock_event) == "two"
10591059
assert len(persistence_store.table.method_calls) == 4
1060+
1061+
1062+
def test_invalid_dynamodb_persistence_layer():
1063+
# Scenario constructing a DynamoDBPersistenceLayer with a key_attr matching sort_key_attr should fail
1064+
with pytest.raises(ValueError) as ve:
1065+
DynamoDBPersistenceLayer(
1066+
table_name="Foo",
1067+
key_attr="id",
1068+
sort_key_attr="id",
1069+
)
1070+
# and raise a ValueError
1071+
assert str(ve.value) == "key_attr [id] and sort_key_attr [id] cannot be the same!"

tests/functional/test_utilities_batch.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -832,5 +832,7 @@ def lambda_handler(event, context):
832832
return processor.response()
833833

834834
# WHEN/THEN
835-
with pytest.raises(BatchProcessingError):
835+
with pytest.raises(BatchProcessingError) as e:
836836
lambda_handler(event, {})
837+
ret = str(e)
838+
assert ret is not None

0 commit comments

Comments
 (0)