Skip to content

Commit a344816

Browse files
committed
chore: fix child exceptions type; remove casting
1 parent 2a4c56b commit a344816

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

aws_lambda_powertools/utilities/batch/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ class EventType(Enum):
3131
#
3232
has_pydantic = "pydantic" in sys.modules
3333
ExceptionInfo = Tuple[Type[BaseException], BaseException, TracebackType]
34-
OptExcInfo = Union[ExceptionInfo, Tuple[None, None, None]]
3534

3635
# For IntelliSense and Mypy to work, we need to account for possible SQS, Kinesis and DynamoDB subclasses
3736
# We need them as subclasses as we must access their message ID or sequence number metadata via dot notation
@@ -61,7 +60,7 @@ class BasePartialProcessor(ABC):
6160
def __init__(self):
6261
self.success_messages: List[BatchEventTypes] = []
6362
self.fail_messages: List[BatchEventTypes] = []
64-
self.exceptions: List = []
63+
self.exceptions: List[ExceptionInfo] = []
6564

6665
@abstractmethod
6766
def _prepare(self):
@@ -132,15 +131,15 @@ def success_handler(self, record, result: Any) -> SuccessResponse:
132131
self.success_messages.append(record)
133132
return entry
134133

135-
def failure_handler(self, record, exception: OptExcInfo) -> FailureResponse:
134+
def failure_handler(self, record, exception: ExceptionInfo) -> FailureResponse:
136135
"""
137136
Keeps track of batch records that failed processing
138137
139138
Parameters
140139
----------
141140
record: Any
142141
record that failed processing
143-
exception: OptExcInfo
142+
exception: ExceptionInfo
144143
Exception information containing type, value, and traceback (sys.exc_info())
145144
146145
Returns
@@ -384,7 +383,7 @@ def _clean(self):
384383
raise BatchProcessingError(
385384
msg=f"All records failed processing. {len(self.exceptions)} individual errors logged"
386385
f"separately below.",
387-
child_exceptions=tuple(self.exceptions),
386+
child_exceptions=self.exceptions,
388387
)
389388

390389
messages = self._get_messages_to_report()

aws_lambda_powertools/utilities/batch/exceptions.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
Batch processing exceptions
33
"""
44
import traceback
5-
from typing import Optional, Tuple
5+
from typing import List, Optional
6+
7+
from aws_lambda_powertools.utilities.batch import ExceptionInfo
68

79

810
class BaseBatchProcessingError(Exception):
9-
def __init__(self, msg="", child_exceptions=()):
11+
def __init__(self, msg="", child_exceptions: Optional[List[ExceptionInfo]] = None):
1012
super().__init__(msg)
1113
self.msg = msg
1214
self.child_exceptions = child_exceptions
@@ -24,7 +26,7 @@ def format_exceptions(self, parent_exception_str):
2426
class SQSBatchProcessingError(BaseBatchProcessingError):
2527
"""When at least one message within a batch could not be processed"""
2628

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

3032
# Overriding this method so we can output all child exception tracebacks when we raise this exception to prevent
@@ -37,7 +39,7 @@ def __str__(self):
3739
class BatchProcessingError(BaseBatchProcessingError):
3840
"""When all batch records failed to be processed"""
3941

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

4345
def __str__(self):

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)