Skip to content

Commit 2d59b7a

Browse files
refactor(batch_processing): mark batch_processor and async_batch_processor as deprecated (#4910)
Deprecating decorators
1 parent a4fdec0 commit 2d59b7a

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

aws_lambda_powertools/utilities/batch/decorators.py

+28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
from __future__ import annotations
22

3+
import warnings
34
from typing import Any, Awaitable, Callable, Dict, List
45

6+
from typing_extensions import deprecated
7+
58
from aws_lambda_powertools.middleware_factory import lambda_handler_decorator
69
from aws_lambda_powertools.utilities.batch import (
710
AsyncBatchProcessor,
@@ -11,9 +14,14 @@
1114
)
1215
from aws_lambda_powertools.utilities.batch.types import PartialItemFailureResponse
1316
from aws_lambda_powertools.utilities.typing import LambdaContext
17+
from aws_lambda_powertools.warnings import PowertoolsDeprecationWarning
1418

1519

1620
@lambda_handler_decorator
21+
@deprecated(
22+
"`async_batch_processor` decorator is deprecated; use `async_process_partial_response` function instead.",
23+
category=None,
24+
)
1725
def async_batch_processor(
1826
handler: Callable,
1927
event: Dict,
@@ -61,6 +69,14 @@ def async_batch_processor(
6169
-----------
6270
* Sync batch processors. Use `batch_processor` instead.
6371
"""
72+
73+
warnings.warn(
74+
"The `async_batch_processor` decorator is deprecated in V3 "
75+
"and will be removed in the next major version. Use `async_process_partial_response` function instead.",
76+
category=PowertoolsDeprecationWarning,
77+
stacklevel=2,
78+
)
79+
6480
records = event["Records"]
6581

6682
with processor(records, record_handler, lambda_context=context):
@@ -70,6 +86,10 @@ def async_batch_processor(
7086

7187

7288
@lambda_handler_decorator
89+
@deprecated(
90+
"`batch_processor` decorator is deprecated; use `process_partial_response` function instead.",
91+
category=None,
92+
)
7393
def batch_processor(
7494
handler: Callable,
7595
event: Dict,
@@ -117,6 +137,14 @@ def batch_processor(
117137
-----------
118138
* Async batch processors. Use `async_batch_processor` instead.
119139
"""
140+
141+
warnings.warn(
142+
"The `batch_processor` decorator is deprecated in V3 "
143+
"and will be removed in the next major version. Use `process_partial_response` function instead.",
144+
category=PowertoolsDeprecationWarning,
145+
stacklevel=2,
146+
)
147+
120148
records = event["Records"]
121149

122150
with processor(records, record_handler, lambda_context=context):

docs/utilities/batch.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ Processing batches from SQS works in three stages:
119119
--8<-- "examples/batch_processing/src/getting_started_sqs_context_manager.py"
120120
```
121121

122-
=== "As a decorator (legacy)"
122+
=== "As a decorator (deprecated)"
123123

124124
```python hl_lines="4-9 12 18 27 29"
125125
--8<-- "examples/batch_processing/src/getting_started_sqs_decorator.py"
@@ -161,7 +161,7 @@ Enable the `skip_group_on_error` option for seamless processing of messages from
161161
--8<-- "examples/batch_processing/src/getting_started_sqs_fifo_context_manager.py"
162162
```
163163

164-
=== "As a decorator (legacy)"
164+
=== "As a decorator (deprecated)"
165165

166166
```python hl_lines="5-6 11 26"
167167
--8<-- "examples/batch_processing/src/getting_started_sqs_fifo_decorator.py"
@@ -197,7 +197,7 @@ Processing batches from Kinesis works in three stages:
197197
--8<-- "examples/batch_processing/src/getting_started_kinesis_context_manager.py"
198198
```
199199

200-
=== "As a decorator (legacy)"
200+
=== "As a decorator (deprecated)"
201201

202202
```python hl_lines="2-9 12 18 26"
203203
--8<-- "examples/batch_processing/src/getting_started_kinesis_decorator.py"
@@ -241,7 +241,7 @@ Processing batches from DynamoDB Streams works in three stages:
241241
--8<-- "examples/batch_processing/src/getting_started_dynamodb_context_manager.py"
242242
```
243243

244-
=== "As a decorator (legacy)"
244+
=== "As a decorator (deprecated)"
245245

246246
```python hl_lines="4-11 14 20 31"
247247
--8<-- "examples/batch_processing/src/getting_started_dynamodb_decorator.py"
@@ -538,7 +538,7 @@ We can automatically inject the [Lambda context](https://docs.aws.amazon.com/lam
538538
--8<-- "examples/batch_processing/src/advanced_accessing_lambda_context.py"
539539
```
540540

541-
=== "As a decorator (legacy)"
541+
=== "As a decorator (deprecated)"
542542

543543
```python hl_lines="18 26"
544544
--8<-- "examples/batch_processing/src/advanced_accessing_lambda_context_decorator.py"
@@ -673,7 +673,7 @@ Use context manager when you want access to the processed messages or handle `Ba
673673

674674
### What's the difference between the decorator and process_partial_response functions?
675675

676-
`batch_processor` and `async_batch_processor` decorators are now considered legacy. Historically, they were kept due to backwards compatibility and to minimize code changes between V1 and V2.
676+
`batch_processor` and `async_batch_processor` decorators are now marked as deprecated. Historically, they were kept due to backwards compatibility and to minimize code changes between V2 and V3. We will remove both in the next major release.
677677

678678
As 2.12.0, `process_partial_response` and `async_process_partial_response` are the recommended instead. It reduces boilerplate, smaller memory/CPU cycles, and it makes it less error prone - e.g., decorators required an additional return.
679679

tests/functional/test_utilities_batch.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
SqsRecordModel,
3333
)
3434
from aws_lambda_powertools.utilities.parser.types import Literal
35+
from aws_lambda_powertools.warnings import PowertoolsDeprecationWarning
3536
from tests.functional.batch.sample_models import (
3637
OrderDynamoDBRecord,
3738
OrderKinesisRecord,
@@ -857,10 +858,11 @@ def lambda_handler(event, context):
857858
return processor.response()
858859

859860
# WHEN
860-
result = lambda_handler(event, {})
861+
with pytest.warns(PowertoolsDeprecationWarning, match="The `async_batch_processor` decorator is deprecated in V3*"):
862+
result = lambda_handler(event, {})
861863

862-
# THEN
863-
assert result["batchItemFailures"] == []
864+
# THEN
865+
assert result["batchItemFailures"] == []
864866

865867

866868
def test_async_batch_processor_middleware_with_failure(sqs_event_factory, async_record_handler):
@@ -877,10 +879,11 @@ def lambda_handler(event, context):
877879
return processor.response()
878880

879881
# WHEN
880-
result = lambda_handler(event, {})
882+
with pytest.warns(PowertoolsDeprecationWarning, match="The `async_batch_processor` decorator is deprecated in V3*"):
883+
result = lambda_handler(event, {})
881884

882-
# THEN
883-
assert len(result["batchItemFailures"]) == 2
885+
# THEN
886+
assert len(result["batchItemFailures"]) == 2
884887

885888

886889
def test_async_batch_processor_context_success_only(sqs_event_factory, async_record_handler):

0 commit comments

Comments
 (0)