Skip to content

refactor(tests): use standard collections for types + refactor code #6497

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@ runtime-evaluated-base-classes = ["pydantic.BaseModel"]
# Maintenance: we're keeping EphemeralMetrics code in case of Hyrum's law so we can quickly revert it
"aws_lambda_powertools/metrics/metrics.py" = ["ERA001"]
"examples/*" = ["FA100", "TCH"]
"tests/*" = ["FA100", "TCH"]
"tests/*" = ["FA100"]
"aws_lambda_powertools/utilities/parser/models/*" = ["FA100"]
10 changes: 6 additions & 4 deletions tests/e2e/utils/data_fetcher/traces.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import json
from datetime import datetime, timedelta
from typing import Any, Dict, Generator, List, Optional
from typing import TYPE_CHECKING, Any, Dict, Generator, List, Optional

import boto3
from botocore.paginate import PageIterator
from mypy_boto3_xray.client import XRayClient
from mypy_boto3_xray.type_defs import TraceSummaryTypeDef
from pydantic import BaseModel
from retry import retry

if TYPE_CHECKING:
from mypy_boto3_xray.type_defs import TraceSummaryTypeDef


class TraceSubsegment(BaseModel):
id: str # noqa: A003 VNE003 # id is a field we can't change
id: str # noqa: A003 # id is a field we can't change
name: str
start_time: float
end_time: float
Expand All @@ -22,7 +24,7 @@ class TraceSubsegment(BaseModel):


class TraceDocument(BaseModel):
id: str # noqa: A003 VNE003 # id is a field we can't change
id: str # noqa: A003 # id is a field we can't change
name: str
start_time: float
end_time: float
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
process_partial_response,
)
from aws_lambda_powertools.utilities.batch.exceptions import BatchProcessingError, UnexpectedBatchTypeError
from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import (
DynamoDBRecord,
)
from aws_lambda_powertools.utilities.data_classes.kinesis_stream_event import (
KinesisStreamRecord,
)
Expand All @@ -31,6 +28,10 @@
if TYPE_CHECKING:
from collections.abc import Awaitable, Callable

from aws_lambda_powertools.utilities.data_classes.dynamo_db_stream_event import (
DynamoDBRecord,
)


@pytest.fixture(scope="module")
def sqs_event_fifo_factory() -> Callable:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

from pydantic import BaseModel, Field
from typing_extensions import Annotated
from typing_extensions import Annotated # noqa: TC002

from aws_lambda_powertools.event_handler.api_gateway import APIGatewayRestResolver
from aws_lambda_powertools.event_handler.openapi.models import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from __future__ import annotations

import datetime
from typing import Any
from typing import TYPE_CHECKING, Any

from botocore.config import Config
from dateutil.tz import gettz
Expand All @@ -20,7 +20,9 @@
TimeKeys,
TimeValues,
)
from aws_lambda_powertools.utilities.feature_flags.types import JSONType

if TYPE_CHECKING:
from aws_lambda_powertools.utilities.feature_flags.types import JSONType


def evaluate_mocked_schema(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import string
import sys
from collections import namedtuple
from collections.abc import Callable, Iterable
from datetime import datetime, timezone
from typing import Any
from typing import TYPE_CHECKING, Any

import pytest

Expand All @@ -27,6 +26,9 @@
from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.utilities.data_classes import S3Event, event_source

if TYPE_CHECKING:
from collections.abc import Callable, Iterable


@pytest.fixture
def stdout():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
import warnings
from collections import namedtuple
from typing import Any
from typing import TYPE_CHECKING, Any

import pytest

Expand All @@ -25,9 +25,11 @@
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.constants import (
MAX_DIMENSIONS,
)
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.types import (
CloudWatchEMFOutput,
)

if TYPE_CHECKING:
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.types import (
CloudWatchEMFOutput,
)


def serialize_metrics(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import annotations

import json
from typing import Any
from typing import TYPE_CHECKING, Any

from aws_lambda_powertools.metrics import (
SchemaValidationError,
)
from aws_lambda_powertools.metrics.metrics import Metrics
from aws_lambda_powertools.metrics.provider import BaseProvider
from aws_lambda_powertools.utilities.typing import LambdaContext

if TYPE_CHECKING:
from aws_lambda_powertools.utilities.typing import LambdaContext


def capture_metrics_output(capsys):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import json
import zipfile
from io import StringIO
from typing import TYPE_CHECKING

import pytest
from botocore.response import StreamingBody
from pytest_mock import MockerFixture

from aws_lambda_powertools.utilities.data_classes import CodePipelineJobEvent
from aws_lambda_powertools.utilities.data_classes.code_pipeline_job_event import (
CodePipelineData,
)
from tests.functional.utils import load_event

if TYPE_CHECKING:
from pytest_mock import MockerFixture


def test_code_pipeline_event():
raw_event = load_event("codePipelineEvent.json")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
from __future__ import annotations

import json
from typing import TYPE_CHECKING

from aws_lambda_powertools.utilities.data_classes import S3Event, SQSEvent
from aws_lambda_powertools.utilities.data_classes.sns_event import SNSMessage
from aws_lambda_powertools.utilities.data_classes.sqs_event import SQSMessageAttributes
from tests.functional.utils import load_event

if TYPE_CHECKING:
from aws_lambda_powertools.utilities.data_classes.sns_event import SNSMessage


def test_seq_trigger_event():
raw_event = load_event("sqsEvent.json")
Expand Down
6 changes: 4 additions & 2 deletions tests/unit/test_tracing.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import annotations

import contextlib
from typing import NamedTuple
from typing import TYPE_CHECKING, NamedTuple
from unittest import mock
from unittest.mock import MagicMock

import pytest

from aws_lambda_powertools import Tracer

if TYPE_CHECKING:
from unittest.mock import MagicMock

# Maintenance: This should move to Functional tests and use Fake over mocks.

MODULE_PREFIX = "tests.unit.test_tracing"
Expand Down
Loading