Skip to content

Commit

Permalink
refactor(typing): reduce aws_lambda_powertools.shared.types usage
Browse files Browse the repository at this point in the history
As discussed in aws-powertools#4607. This simplifies linting and refactoring so we can
introduce

    from __future__ import annotations

to all files, which is the plan as the next step.
  • Loading branch information
ericbn committed Aug 6, 2024
1 parent 26cfe7f commit 3ed41f8
Show file tree
Hide file tree
Showing 46 changed files with 62 additions and 96 deletions.
2 changes: 1 addition & 1 deletion aws_lambda_powertools/event_handler/api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
Dict,
Generic,
List,
Literal,
Mapping,
Match,
Optional,
Expand Down Expand Up @@ -47,7 +48,6 @@
from aws_lambda_powertools.shared.cookies import Cookie
from aws_lambda_powertools.shared.functions import powertools_dev_is_set
from aws_lambda_powertools.shared.json_encoder import Encoder
from aws_lambda_powertools.shared.types import Literal
from aws_lambda_powertools.utilities.data_classes import (
ALBEvent,
APIGatewayProxyEvent,
Expand Down
3 changes: 1 addition & 2 deletions aws_lambda_powertools/event_handler/middlewares/base.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from abc import ABC, abstractmethod
from typing import Generic
from typing import Generic, Protocol

from aws_lambda_powertools.event_handler.api_gateway import Response
from aws_lambda_powertools.event_handler.types import EventHandlerInstance
from aws_lambda_powertools.shared.types import Protocol


class NextMiddleware(Protocol):
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_powertools/event_handler/openapi/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from enum import Enum
from typing import Any, Dict, List, Optional, Set, Union
from typing import Any, Dict, List, Literal, Optional, Set, Union

from pydantic import AnyUrl, BaseModel, Field
from typing_extensions import Annotated

from aws_lambda_powertools.event_handler.openapi.compat import model_rebuild
from aws_lambda_powertools.event_handler.openapi.constants import (
MODEL_CONFIG_ALLOW,
MODEL_CONFIG_IGNORE,
)
from aws_lambda_powertools.shared.types import Annotated, Literal

"""
The code defines Pydantic models for the various OpenAPI objects like OpenAPI, PathItem, Operation, Parameter etc.
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_powertools/event_handler/openapi/params.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import inspect
from enum import Enum
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Type, Union

from pydantic import BaseConfig
from pydantic.fields import FieldInfo
from typing_extensions import Annotated, get_args, get_origin

from aws_lambda_powertools.event_handler import Response
from aws_lambda_powertools.event_handler.openapi.compat import (
Expand All @@ -16,7 +17,6 @@
get_annotation_from_field_info,
)
from aws_lambda_powertools.event_handler.openapi.types import CacheKey
from aws_lambda_powertools.shared.types import Annotated, Literal, get_args, get_origin

"""
This turns the low-level function signature into typed, validated Pydantic models for consumption.
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_powertools/event_handler/openapi/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import types
from enum import Enum
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Set, Type, Union
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Set, Type, TypedDict, Union

from aws_lambda_powertools.shared.types import NotRequired, TypedDict
from typing_extensions import NotRequired

if TYPE_CHECKING:
from pydantic import BaseModel # noqa: F401
Expand Down
4 changes: 2 additions & 2 deletions aws_lambda_powertools/logging/types.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from __future__ import annotations

from typing import Any, Dict, List, Union
from typing import Any, Dict, List, TypedDict, Union

from aws_lambda_powertools.shared.types import NotRequired, TypeAlias, TypedDict
from typing_extensions import NotRequired, TypeAlias

LogRecord: TypeAlias = Union[Dict[str, Any], "PowertoolsLogRecord"]
LogStackTrace: TypeAlias = Union[Dict[str, Any], "PowertoolsStackTrace"]
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/metrics/functions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from __future__ import annotations

from datetime import datetime
from typing import List

from aws_lambda_powertools.metrics.provider.cloudwatch_emf.exceptions import (
MetricResolutionError,
MetricUnitError,
)
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.metric_properties import MetricResolution, MetricUnit
from aws_lambda_powertools.shared import constants
from aws_lambda_powertools.shared.types import List


def extract_cloudwatch_metric_resolution_value(metric_resolutions: List, resolution: int | MetricResolution) -> int:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from aws_lambda_powertools.shared.types import List, NotRequired, TypedDict
from typing import List, TypedDict

from typing_extensions import NotRequired


class CloudWatchEMFMetric(TypedDict):
Expand Down
4 changes: 3 additions & 1 deletion aws_lambda_powertools/metrics/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from aws_lambda_powertools.shared.types import NotRequired, TypedDict
from typing import TypedDict

from typing_extensions import NotRequired


class MetricNameUnitResolution(TypedDict):
Expand Down
4 changes: 1 addition & 3 deletions aws_lambda_powertools/shared/cookies.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from datetime import datetime
from enum import Enum
from io import StringIO
from typing import Optional

from aws_lambda_powertools.shared.types import List
from typing import List, Optional


class SameSite(Enum):
Expand Down
22 changes: 1 addition & 21 deletions aws_lambda_powertools/shared/types.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,5 @@
import sys
from typing import Any, Callable, Dict, List, Literal, Protocol, TypedDict, TypeVar, Union

if sys.version_info >= (3, 9):
from typing import Annotated
else:
from typing_extensions import Annotated

if sys.version_info >= (3, 11):
from typing import NotRequired
else:
from typing_extensions import NotRequired

# Even though `get_args` and `get_origin` were added in Python 3.8, they only handle Annotated correctly on 3.10.
# So for python < 3.10 we use the backport from typing_extensions.
if sys.version_info >= (3, 10):
from typing import TypeAlias, get_args, get_origin
else:
from typing_extensions import TypeAlias, get_args, get_origin
from typing import Any, Callable, Dict, List, TypeVar, Union

AnyCallableT = TypeVar("AnyCallableT", bound=Callable[..., Any]) # noqa: VNE001
# JSON primitives only, mypy doesn't support recursive tho
JSONType = Union[str, int, float, bool, None, Dict[str, Any], List[Any]]

__all__ = ["get_args", "get_origin", "Annotated", "Protocol", "TypedDict", "Literal", "NotRequired", "TypeAlias"]
4 changes: 1 addition & 3 deletions aws_lambda_powertools/utilities/batch/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import sys
from typing import Optional, Type, Union

from aws_lambda_powertools.shared.types import List, TypedDict
from typing import List, Optional, Type, TypedDict, Union

has_pydantic = "pydantic" in sys.modules

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import warnings
from dataclasses import dataclass, field
from typing import Any, Dict, Iterator, List, Optional, Tuple
from typing import Any, Dict, Iterator, List, Literal, Optional, Tuple
from urllib.parse import unquote_plus

from aws_lambda_powertools.shared.types import Literal
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper

# list of valid result code. Used both in S3BatchOperationResponse and S3BatchOperationResponseRecord
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from aws_lambda_powertools.shared.types import Literal
from typing import Literal

from aws_lambda_powertools.utilities.data_classes.common import DictWrapper


Expand Down
3 changes: 1 addition & 2 deletions aws_lambda_powertools/utilities/idempotency/hook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from typing import Any
from typing import Any, Protocol

from aws_lambda_powertools.shared.types import Protocol
from aws_lambda_powertools.utilities.idempotency.persistence.datarecord import DataRecord


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
import logging
from contextlib import contextmanager
from datetime import timedelta
from typing import Any, Dict
from typing import Any, Dict, Literal, Protocol

import redis

from aws_lambda_powertools.shared.types import Literal, Protocol
from aws_lambda_powertools.utilities.idempotency import BasePersistenceLayer
from aws_lambda_powertools.utilities.idempotency.exceptions import (
IdempotencyItemAlreadyExistsError,
Expand Down
3 changes: 1 addition & 2 deletions aws_lambda_powertools/utilities/parameters/ssm.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import logging
import os
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, overload
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Tuple, Union, overload

import boto3
from botocore.config import Config
Expand All @@ -17,7 +17,6 @@
resolve_truthy_env_var_choice,
slice_dictionary,
)
from aws_lambda_powertools.shared.types import Literal
from aws_lambda_powertools.utilities.parameters.base import (
DEFAULT_MAX_AGE_SECS,
DEFAULT_PROVIDERS,
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/utilities/parameters/types.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from aws_lambda_powertools.shared.types import Literal
from typing import Literal

TransformOptions = Literal["json", "binary", "auto", None]
4 changes: 1 addition & 3 deletions aws_lambda_powertools/utilities/parser/models/cloudwatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import logging
import zlib
from datetime import datetime
from typing import Optional, Type, Union
from typing import List, Optional, Type, Union

from pydantic import BaseModel, Field, field_validator

from aws_lambda_powertools.shared.types import List

logger = logging.getLogger(__name__)


Expand Down
4 changes: 1 addition & 3 deletions aws_lambda_powertools/utilities/parser/types.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
"""Generics and other shared types used across parser"""

from typing import Any, Dict, Type, TypeVar, Union
from typing import Any, Dict, Literal, Type, TypeVar, Union

from pydantic import BaseModel, Json

from aws_lambda_powertools.shared.types import Literal

Model = TypeVar("Model", bound=BaseModel)
EnvelopeModel = TypeVar("EnvelopeModel")
EventParserReturnType = TypeVar("EventParserReturnType")
Expand Down
2 changes: 1 addition & 1 deletion aws_lambda_powertools/utilities/streaming/s3_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Any,
Iterable,
List,
Literal,
Optional,
Sequence,
TypeVar,
Expand All @@ -15,7 +16,6 @@
overload,
)

from aws_lambda_powertools.shared.types import Literal
from aws_lambda_powertools.utilities.streaming._s3_seekable_io import _S3SeekableIO
from aws_lambda_powertools.utilities.streaming.transformations import (
CsvTransform,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import time

from typing_extensions import Annotated

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import BedrockAgentResolver
from aws_lambda_powertools.event_handler.openapi.params import Body
from aws_lambda_powertools.shared.types import Annotated
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import asyncio
from typing import List
from typing import List, TypedDict

import aiohttp

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.types import TypedDict
from aws_lambda_powertools.tracing import aiohttp_trace_config
from aws_lambda_powertools.utilities.typing import LambdaContext

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import List
from typing import List, TypedDict

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.types import TypedDict
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
Expand Down
3 changes: 1 addition & 2 deletions examples/event_handler_graphql/src/async_resolvers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import asyncio
from typing import List
from typing import List, TypedDict

import aiohttp

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.types import TypedDict
from aws_lambda_powertools.tracing import aiohttp_trace_config
from aws_lambda_powertools.utilities.typing import LambdaContext

Expand Down
3 changes: 1 addition & 2 deletions examples/event_handler_graphql/src/custom_models.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import List
from typing import List, TypedDict

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.types import TypedDict
from aws_lambda_powertools.utilities.data_classes.appsync import scalar_types_utils
from aws_lambda_powertools.utilities.data_classes.appsync_resolver_event import (
AppSyncResolverEvent,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from typing import List
from typing import List, TypedDict

import requests
from requests import Response

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.types import TypedDict
from aws_lambda_powertools.utilities.data_classes.appsync import scalar_types_utils
from aws_lambda_powertools.utilities.typing import LambdaContext

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import List
from typing import List, TypedDict

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.types import TypedDict
from aws_lambda_powertools.utilities.data_classes.appsync import scalar_types_utils
from aws_lambda_powertools.utilities.typing import LambdaContext

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import List
from typing import List, TypedDict

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.types import TypedDict
from aws_lambda_powertools.utilities.data_classes.appsync import scalar_types_utils
from aws_lambda_powertools.utilities.typing import LambdaContext

Expand Down
3 changes: 1 addition & 2 deletions examples/event_handler_graphql/src/nested_mappings.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from typing import List
from typing import List, TypedDict

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import AppSyncResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.shared.types import TypedDict
from aws_lambda_powertools.utilities.typing import LambdaContext

tracer = Tracer()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import List
from typing import List, TypedDict

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler.appsync import Router
from aws_lambda_powertools.shared.types import TypedDict

tracer = Tracer()
logger = Logger()
Expand Down
3 changes: 1 addition & 2 deletions examples/event_handler_graphql/src/split_operation_module.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from typing import List
from typing import List, TypedDict

from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler.appsync import Router
from aws_lambda_powertools.shared.types import TypedDict

tracer = Tracer()
logger = Logger()
Expand Down
Loading

0 comments on commit 3ed41f8

Please sign in to comment.