Skip to content

Commit acd3734

Browse files
author
Sujit
committed
fix: satisfy batch logger mypy checks
1 parent 368cbbc commit acd3734

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

aws_lambda_powertools/utilities/batch/base.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import sys
1515
from abc import ABC, abstractmethod
1616
from enum import Enum
17-
from typing import TYPE_CHECKING, Any, Tuple, Union, overload
17+
from typing import TYPE_CHECKING, Any, Tuple, Union, cast, overload
1818

1919
from aws_lambda_powertools.shared import constants
2020
from aws_lambda_powertools.utilities.batch.exceptions import (
@@ -35,6 +35,7 @@
3535

3636
if TYPE_CHECKING:
3737
from collections.abc import Callable
38+
from types import TracebackType
3839

3940
from aws_lambda_powertools.logging import Logger
4041
from aws_lambda_powertools.utilities.batch.types import (
@@ -240,10 +241,19 @@ def failure_handler(self, record, exception: ExceptionInfo) -> FailureResponse:
240241
entry = ("fail", exception_string, record)
241242
logger.debug(f"Record processing exception: {exception_string}")
242243

243-
if getattr(self, "logger", None) and exception[2] is not None:
244-
self.logger.warning(
244+
# Log with full traceback when a customer-provided logger is present
245+
# and the exception carries a real traceback (e.g. not a synthetic FIFO circuit-breaker)
246+
batch_logger = self.logger
247+
if batch_logger is not None and exception[2] is not None:
248+
# ExceptionInfo allows None on every slot, but logging.warning's exc_info
249+
# requires a fully populated tuple. We already excluded synthetic exceptions
250+
# (no traceback) above, so the type and value are guaranteed to be set.
251+
assert exception[0] is not None
252+
assert exception[1] is not None
253+
exc_info = cast("tuple[type[BaseException], BaseException, TracebackType]", exception)
254+
batch_logger.warning(
245255
"Record processing exception; skipping this record",
246-
exc_info=exception,
256+
exc_info=exc_info,
247257
)
248258

249259
self.exceptions.append(exception)

tests/functional/batch/required_dependencies/test_utilities_batch.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ async def simple_async_handler(record: SQSRecord):
865865

866866
def test_batch_processor_logs_exception_with_injected_logger(sqs_event_factory, caplog):
867867
import logging
868+
868869
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
869870

870871
fail_record = sqs_event_factory("fail")
@@ -894,6 +895,7 @@ def handler(record):
894895

895896
def test_batch_processor_does_not_log_without_injected_logger(sqs_event_factory, caplog):
896897
import logging
898+
897899
from aws_lambda_powertools.utilities.batch import BatchProcessor, EventType, process_partial_response
898900

899901
fail_record = sqs_event_factory("fail")
@@ -916,6 +918,7 @@ def handler(record):
916918

917919
def test_sqs_fifo_circuit_breaker_does_not_log(sqs_event_fifo_factory, caplog):
918920
import logging
921+
919922
from aws_lambda_powertools.utilities.batch import SqsFifoPartialProcessor, process_partial_response
920923

921924
failing_record = sqs_event_fifo_factory("fail", "group-1")

0 commit comments

Comments
 (0)