Skip to content

Commit 2a4e1ec

Browse files
refactor(e2e-tests): use standard collections for types + refactor code (#6505)
* Using generics types + enabling ruff * Using generics types + enabling ruff
1 parent ef84244 commit 2a4e1ec

File tree

79 files changed

+281
-77
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+281
-77
lines changed

tests/e2e/conftest.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING, Any
4+
15
import pytest
26

37
from tests.e2e.utils.infrastructure import call_once
48
from tests.e2e.utils.lambda_layer.powertools_layer import LocalLambdaPowertoolsLayer
59

10+
if TYPE_CHECKING:
11+
from collections.abc import Generator
12+
613

714
@pytest.fixture(scope="session", autouse=True)
8-
def lambda_layer_build(tmp_path_factory: pytest.TempPathFactory, worker_id: str) -> str:
15+
def lambda_layer_build(tmp_path_factory: pytest.TempPathFactory, worker_id: str) -> Generator[Any, Any, Any]:
916
"""Build Lambda Layer once before stacks are created
1017
1118
Parameters

tests/e2e/data_masking/conftest.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24

35
from tests.e2e.data_masking.infrastructure import DataMaskingStack

tests/e2e/data_masking/handlers/basic_handler.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools import Logger
24
from aws_lambda_powertools.utilities.data_masking import DataMasking
35
from aws_lambda_powertools.utilities.data_masking.provider.kms.aws_encryption_sdk import AWSEncryptionSDKProvider
@@ -17,7 +19,4 @@ def lambda_handler(event, context):
1719
data_masker = DataMasking(provider=AWSEncryptionSDKProvider(keys=[kms_key]))
1820
value = [1, 2, "string", 4.5]
1921
encrypted_data = data_masker.encrypt(value)
20-
response = {}
21-
response["encrypted_data"] = encrypted_data
22-
23-
return response
22+
return {"encrypted_data": encrypted_data}

tests/e2e/data_masking/infrastructure.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import aws_cdk.aws_kms as kms
24
from aws_cdk import CfnOutput, Duration
35
from aws_cdk import aws_iam as iam

tests/e2e/data_masking/test_e2e_data_masking.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24
from uuid import uuid4
35

tests/e2e/event_handler/conftest.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24

35
from tests.e2e.event_handler.infrastructure import EventHandlerStack

tests/e2e/event_handler/handlers/alb_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools.event_handler import (
24
ALBResolver,
35
CORSConfig,

tests/e2e/event_handler/handlers/alb_handler_with_body_none.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools.event_handler import (
24
ALBResolver,
35
Response,

tests/e2e/event_handler/handlers/api_gateway_http_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools.event_handler import (
24
APIGatewayHttpResolver,
35
CORSConfig,

tests/e2e/event_handler/handlers/api_gateway_rest_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools.event_handler import (
24
APIGatewayRestResolver,
35
CORSConfig,

tests/e2e/event_handler/handlers/lambda_function_url_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools.event_handler import (
24
CORSConfig,
35
LambdaFunctionUrlResolver,

tests/e2e/event_handler/handlers/openapi_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools.event_handler import (
24
APIGatewayRestResolver,
35
)

tests/e2e/event_handler/infrastructure.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Dict, List, Optional
1+
from __future__ import annotations
22

33
from aws_cdk import CfnOutput, Duration
44
from aws_cdk import aws_apigateway as apigwv1
@@ -28,7 +28,7 @@ def create_resources(self):
2828
self._create_api_gateway_http(function=functions["ApiGatewayHttpHandler"])
2929
self._create_lambda_function_url(function=functions["LambdaFunctionUrlHandler"])
3030

31-
def _create_alb(self, function: List[Function]):
31+
def _create_alb(self, function: list[Function]):
3232
vpc = ec2.Vpc.from_lookup(
3333
self.stack,
3434
"VPC",
@@ -58,7 +58,7 @@ def _create_alb_listener(
5858
name: str,
5959
port: int,
6060
function: Function,
61-
attributes: Optional[Dict[str, str]] = None,
61+
attributes: dict[str, str] | None = None,
6262
):
6363
listener = alb.add_listener(name, port=port, protocol=elbv2.ApplicationProtocol.HTTP)
6464
target = listener.add_targets(f"ALB{name}Target", targets=[targets.LambdaTarget(function)])
@@ -82,7 +82,7 @@ def _create_api_gateway_http(self, function: Function):
8282

8383
CfnOutput(self.stack, "APIGatewayHTTPUrl", value=(apigw.url or ""))
8484

85-
def _create_api_gateway_rest(self, function: List[Function]):
85+
def _create_api_gateway_rest(self, function: list[Function]):
8686
apigw = apigwv1.RestApi(
8787
self.stack,
8888
"APIGatewayRest",

tests/e2e/event_handler/test_cors.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24
from requests import Request
35

tests/e2e/event_handler/test_header_serializer.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from uuid import uuid4
24

35
import pytest

tests/e2e/event_handler/test_openapi.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24
from requests import Request
35

tests/e2e/event_handler/test_paths_ending_with_slash.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24
from requests import HTTPError, Request
35

tests/e2e/event_handler/test_response_code.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24
from requests import Request
35

tests/e2e/event_handler_appsync/conftest.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24

35
from tests.e2e.event_handler_appsync.infrastructure import EventHandlerAppSyncStack

tests/e2e/event_handler_appsync/handlers/appsync_resolver_handler.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from typing import List, Optional
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
24

35
from pydantic import BaseModel
46

57
from aws_lambda_powertools.event_handler import AppSyncResolver
6-
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
7-
from aws_lambda_powertools.utilities.typing import LambdaContext
8+
9+
if TYPE_CHECKING:
10+
from aws_lambda_powertools.utilities.data_classes import AppSyncResolverEvent
11+
from aws_lambda_powertools.utilities.typing import LambdaContext
812

913
app = AppSyncResolver()
1014

@@ -84,29 +88,29 @@ def get_post(post_id: str = "") -> dict:
8488

8589

8690
@app.resolver(type_name="Query", field_name="allPosts")
87-
def all_posts() -> List[dict]:
91+
def all_posts() -> list[dict]:
8892
return list(posts.values())
8993

9094

9195
# PROCESSING BATCH WITHOUT AGGREGATION
9296
@app.batch_resolver(type_name="Post", field_name="relatedPosts", aggregate=False)
93-
def related_posts(event: AppSyncResolverEvent) -> Optional[list]:
97+
def related_posts(event: AppSyncResolverEvent) -> list | None:
9498
return posts_related[event.source["post_id"]] if event.source else None
9599

96100

97101
@app.async_batch_resolver(type_name="Post", field_name="relatedPostsAsync", aggregate=False)
98-
async def related_posts_async(event: AppSyncResolverEvent) -> Optional[list]:
102+
async def related_posts_async(event: AppSyncResolverEvent) -> list | None:
99103
return posts_related[event.source["post_id"]] if event.source else None
100104

101105

102106
# PROCESSING BATCH WITH AGGREGATION
103107
@app.batch_resolver(type_name="Post", field_name="relatedPostsAggregate")
104-
def related_posts_aggregate(event: List[AppSyncResolverEvent]) -> Optional[list]:
108+
def related_posts_aggregate(event: list[AppSyncResolverEvent]) -> list | None:
105109
return [posts_related[record.source.get("post_id")] for record in event]
106110

107111

108112
@app.async_batch_resolver(type_name="Post", field_name="relatedPostsAsyncAggregate")
109-
async def related_posts_async_aggregate(event: List[AppSyncResolverEvent]) -> Optional[list]:
113+
async def related_posts_async_aggregate(event: list[AppSyncResolverEvent]) -> list | None:
110114
return [posts_related[record.source.get("post_id")] for record in event]
111115

112116

tests/e2e/event_handler_appsync/infrastructure.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
from __future__ import annotations
2+
13
from pathlib import Path
4+
from typing import TYPE_CHECKING
25

36
from aws_cdk import CfnOutput, Duration, Expiration
47
from aws_cdk import aws_appsync_alpha as appsync
5-
from aws_cdk.aws_lambda import Function
68

79
from tests.e2e.utils.data_builder import build_random_value
810
from tests.e2e.utils.infrastructure import BaseInfrastructure
911

12+
if TYPE_CHECKING:
13+
from aws_cdk.aws_lambda import Function
14+
1015

1116
class EventHandlerAppSyncStack(BaseInfrastructure):
1217
def create_resources(self):

tests/e2e/event_handler_appsync/test_appsync_resolvers.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24

35
import pytest

tests/e2e/idempotency/conftest.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24

35
from tests.e2e.idempotency.infrastructure import IdempotencyDynamoDBStack

tests/e2e/idempotency/handlers/function_thread_safety_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import time
35
from concurrent.futures import ThreadPoolExecutor, as_completed

tests/e2e/idempotency/handlers/optional_idempotency_key_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import uuid
35

tests/e2e/idempotency/handlers/parallel_execution_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import time
35

tests/e2e/idempotency/handlers/payload_tampering_validation_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import uuid
35

tests/e2e/idempotency/handlers/response_hook.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1+
from __future__ import annotations
2+
13
import os
4+
from typing import TYPE_CHECKING
25

36
from aws_lambda_powertools.utilities.idempotency import (
47
DynamoDBPersistenceLayer,
58
IdempotencyConfig,
69
idempotent,
710
)
8-
from aws_lambda_powertools.utilities.idempotency.persistence.datarecord import (
9-
DataRecord,
10-
)
11+
12+
if TYPE_CHECKING:
13+
from aws_lambda_powertools.utilities.idempotency.persistence.datarecord import (
14+
DataRecord,
15+
)
1116

1217
TABLE_NAME = os.getenv("IdempotencyTable", "")
1318
persistence_layer = DynamoDBPersistenceLayer(table_name=TABLE_NAME)

tests/e2e/idempotency/handlers/ttl_cache_expiration_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import time
35

tests/e2e/idempotency/handlers/ttl_cache_timeout_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import time
35

tests/e2e/idempotency/infrastructure.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
from __future__ import annotations
2+
3+
from typing import TYPE_CHECKING
4+
15
from aws_cdk import CfnOutput, Duration, RemovalPolicy
26
from aws_cdk import aws_dynamodb as dynamodb
3-
from aws_cdk.aws_dynamodb import Table
7+
8+
if TYPE_CHECKING:
9+
from aws_cdk.aws_dynamodb import Table
410

511
from tests.e2e.utils.infrastructure import BaseInfrastructure
612

tests/e2e/idempotency/test_idempotency_dynamodb.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24
from copy import deepcopy
35
from time import sleep

tests/e2e/logger/conftest.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24

35
from tests.e2e.logger.infrastructure import LoggerStack

tests/e2e/logger/handlers/basic_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools import Logger
24

35
logger = Logger()

tests/e2e/logger/handlers/buffer_logs_with_flush.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools import Logger
24
from aws_lambda_powertools.logging.buffer import LoggerBufferConfig
35

tests/e2e/logger/handlers/buffer_logs_without_flush.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools import Logger
24
from aws_lambda_powertools.logging.buffer import LoggerBufferConfig
35

tests/e2e/logger/handlers/multiple_logger_instances.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools import Logger
24

35
# Instance 1

tests/e2e/logger/handlers/tz_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import os
24
import time
35

tests/e2e/logger/infrastructure.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from tests.e2e.utils.infrastructure import BaseInfrastructure
24

35

tests/e2e/logger/test_logger.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import json
24
import os
35
import time

tests/e2e/metrics/conftest.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import pytest
24

35
from tests.e2e.metrics.infrastructure import MetricsStack

tests/e2e/metrics/handlers/basic_handler.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools import Metrics
24

35
my_metrics = Metrics()

tests/e2e/metrics/handlers/cold_start.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from aws_lambda_powertools import Metrics
24

35
my_metrics = Metrics()

tests/e2e/metrics/infrastructure.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
from tests.e2e.utils.infrastructure import BaseInfrastructure
24

35

0 commit comments

Comments
 (0)