Skip to content

Commit 3ed41f8

Browse files
committed
refactor(typing): reduce aws_lambda_powertools.shared.types usage
As discussed in #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.
1 parent 26cfe7f commit 3ed41f8

Some content is hidden

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

46 files changed

+62
-96
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
Dict,
1818
Generic,
1919
List,
20+
Literal,
2021
Mapping,
2122
Match,
2223
Optional,
@@ -47,7 +48,6 @@
4748
from aws_lambda_powertools.shared.cookies import Cookie
4849
from aws_lambda_powertools.shared.functions import powertools_dev_is_set
4950
from aws_lambda_powertools.shared.json_encoder import Encoder
50-
from aws_lambda_powertools.shared.types import Literal
5151
from aws_lambda_powertools.utilities.data_classes import (
5252
ALBEvent,
5353
APIGatewayProxyEvent,

aws_lambda_powertools/event_handler/middlewares/base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from abc import ABC, abstractmethod
2-
from typing import Generic
2+
from typing import Generic, Protocol
33

44
from aws_lambda_powertools.event_handler.api_gateway import Response
55
from aws_lambda_powertools.event_handler.types import EventHandlerInstance
6-
from aws_lambda_powertools.shared.types import Protocol
76

87

98
class NextMiddleware(Protocol):

aws_lambda_powertools/event_handler/openapi/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from enum import Enum
2-
from typing import Any, Dict, List, Optional, Set, Union
2+
from typing import Any, Dict, List, Literal, Optional, Set, Union
33

44
from pydantic import AnyUrl, BaseModel, Field
5+
from typing_extensions import Annotated
56

67
from aws_lambda_powertools.event_handler.openapi.compat import model_rebuild
78
from aws_lambda_powertools.event_handler.openapi.constants import (
89
MODEL_CONFIG_ALLOW,
910
MODEL_CONFIG_IGNORE,
1011
)
11-
from aws_lambda_powertools.shared.types import Annotated, Literal
1212

1313
"""
1414
The code defines Pydantic models for the various OpenAPI objects like OpenAPI, PathItem, Operation, Parameter etc.

aws_lambda_powertools/event_handler/openapi/params.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import inspect
22
from enum import Enum
3-
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
3+
from typing import Any, Callable, Dict, List, Literal, Optional, Tuple, Type, Union
44

55
from pydantic import BaseConfig
66
from pydantic.fields import FieldInfo
7+
from typing_extensions import Annotated, get_args, get_origin
78

89
from aws_lambda_powertools.event_handler import Response
910
from aws_lambda_powertools.event_handler.openapi.compat import (
@@ -16,7 +17,6 @@
1617
get_annotation_from_field_info,
1718
)
1819
from aws_lambda_powertools.event_handler.openapi.types import CacheKey
19-
from aws_lambda_powertools.shared.types import Annotated, Literal, get_args, get_origin
2020

2121
"""
2222
This turns the low-level function signature into typed, validated Pydantic models for consumption.

aws_lambda_powertools/event_handler/openapi/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import types
22
from enum import Enum
3-
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Set, Type, Union
3+
from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Set, Type, TypedDict, Union
44

5-
from aws_lambda_powertools.shared.types import NotRequired, TypedDict
5+
from typing_extensions import NotRequired
66

77
if TYPE_CHECKING:
88
from pydantic import BaseModel # noqa: F401

aws_lambda_powertools/logging/types.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from __future__ import annotations
22

3-
from typing import Any, Dict, List, Union
3+
from typing import Any, Dict, List, TypedDict, Union
44

5-
from aws_lambda_powertools.shared.types import NotRequired, TypeAlias, TypedDict
5+
from typing_extensions import NotRequired, TypeAlias
66

77
LogRecord: TypeAlias = Union[Dict[str, Any], "PowertoolsLogRecord"]
88
LogStackTrace: TypeAlias = Union[Dict[str, Any], "PowertoolsStackTrace"]

aws_lambda_powertools/metrics/functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from __future__ import annotations
22

33
from datetime import datetime
4+
from typing import List
45

56
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.exceptions import (
67
MetricResolutionError,
78
MetricUnitError,
89
)
910
from aws_lambda_powertools.metrics.provider.cloudwatch_emf.metric_properties import MetricResolution, MetricUnit
1011
from aws_lambda_powertools.shared import constants
11-
from aws_lambda_powertools.shared.types import List
1212

1313

1414
def extract_cloudwatch_metric_resolution_value(metric_resolutions: List, resolution: int | MetricResolution) -> int:

aws_lambda_powertools/metrics/provider/cloudwatch_emf/types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from aws_lambda_powertools.shared.types import List, NotRequired, TypedDict
1+
from typing import List, TypedDict
2+
3+
from typing_extensions import NotRequired
24

35

46
class CloudWatchEMFMetric(TypedDict):

aws_lambda_powertools/metrics/types.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
from aws_lambda_powertools.shared.types import NotRequired, TypedDict
1+
from typing import TypedDict
2+
3+
from typing_extensions import NotRequired
24

35

46
class MetricNameUnitResolution(TypedDict):

aws_lambda_powertools/shared/cookies.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from datetime import datetime
22
from enum import Enum
33
from io import StringIO
4-
from typing import Optional
5-
6-
from aws_lambda_powertools.shared.types import List
4+
from typing import List, Optional
75

86

97
class SameSite(Enum):

aws_lambda_powertools/shared/types.py

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
1-
import sys
2-
from typing import Any, Callable, Dict, List, Literal, Protocol, TypedDict, TypeVar, Union
3-
4-
if sys.version_info >= (3, 9):
5-
from typing import Annotated
6-
else:
7-
from typing_extensions import Annotated
8-
9-
if sys.version_info >= (3, 11):
10-
from typing import NotRequired
11-
else:
12-
from typing_extensions import NotRequired
13-
14-
# Even though `get_args` and `get_origin` were added in Python 3.8, they only handle Annotated correctly on 3.10.
15-
# So for python < 3.10 we use the backport from typing_extensions.
16-
if sys.version_info >= (3, 10):
17-
from typing import TypeAlias, get_args, get_origin
18-
else:
19-
from typing_extensions import TypeAlias, get_args, get_origin
1+
from typing import Any, Callable, Dict, List, TypeVar, Union
202

213
AnyCallableT = TypeVar("AnyCallableT", bound=Callable[..., Any]) # noqa: VNE001
224
# JSON primitives only, mypy doesn't support recursive tho
235
JSONType = Union[str, int, float, bool, None, Dict[str, Any], List[Any]]
24-
25-
__all__ = ["get_args", "get_origin", "Annotated", "Protocol", "TypedDict", "Literal", "NotRequired", "TypeAlias"]

aws_lambda_powertools/utilities/batch/types.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import sys
2-
from typing import Optional, Type, Union
3-
4-
from aws_lambda_powertools.shared.types import List, TypedDict
2+
from typing import List, Optional, Type, TypedDict, Union
53

64
has_pydantic = "pydantic" in sys.modules
75

aws_lambda_powertools/utilities/data_classes/s3_batch_operation_event.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import warnings
22
from dataclasses import dataclass, field
3-
from typing import Any, Dict, Iterator, List, Optional, Tuple
3+
from typing import Any, Dict, Iterator, List, Literal, Optional, Tuple
44
from urllib.parse import unquote_plus
55

6-
from aws_lambda_powertools.shared.types import Literal
76
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
87

98
# list of valid result code. Used both in S3BatchOperationResponse and S3BatchOperationResponseRecord

aws_lambda_powertools/utilities/data_classes/secrets_manager_event.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from aws_lambda_powertools.shared.types import Literal
1+
from typing import Literal
2+
23
from aws_lambda_powertools.utilities.data_classes.common import DictWrapper
34

45

aws_lambda_powertools/utilities/idempotency/hook.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from typing import Any
1+
from typing import Any, Protocol
22

3-
from aws_lambda_powertools.shared.types import Protocol
43
from aws_lambda_powertools.utilities.idempotency.persistence.datarecord import DataRecord
54

65

aws_lambda_powertools/utilities/idempotency/persistence/redis.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55
import logging
66
from contextlib import contextmanager
77
from datetime import timedelta
8-
from typing import Any, Dict
8+
from typing import Any, Dict, Literal, Protocol
99

1010
import redis
1111

12-
from aws_lambda_powertools.shared.types import Literal, Protocol
1312
from aws_lambda_powertools.utilities.idempotency import BasePersistenceLayer
1413
from aws_lambda_powertools.utilities.idempotency.exceptions import (
1514
IdempotencyItemAlreadyExistsError,

aws_lambda_powertools/utilities/parameters/ssm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import logging
88
import os
9-
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, overload
9+
from typing import TYPE_CHECKING, Any, Dict, List, Literal, Optional, Tuple, Union, overload
1010

1111
import boto3
1212
from botocore.config import Config
@@ -17,7 +17,6 @@
1717
resolve_truthy_env_var_choice,
1818
slice_dictionary,
1919
)
20-
from aws_lambda_powertools.shared.types import Literal
2120
from aws_lambda_powertools.utilities.parameters.base import (
2221
DEFAULT_MAX_AGE_SECS,
2322
DEFAULT_PROVIDERS,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from aws_lambda_powertools.shared.types import Literal
1+
from typing import Literal
22

33
TransformOptions = Literal["json", "binary", "auto", None]

aws_lambda_powertools/utilities/parser/models/cloudwatch.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
import logging
44
import zlib
55
from datetime import datetime
6-
from typing import Optional, Type, Union
6+
from typing import List, Optional, Type, Union
77

88
from pydantic import BaseModel, Field, field_validator
99

10-
from aws_lambda_powertools.shared.types import List
11-
1210
logger = logging.getLogger(__name__)
1311

1412

aws_lambda_powertools/utilities/parser/types.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
"""Generics and other shared types used across parser"""
22

3-
from typing import Any, Dict, Type, TypeVar, Union
3+
from typing import Any, Dict, Literal, Type, TypeVar, Union
44

55
from pydantic import BaseModel, Json
66

7-
from aws_lambda_powertools.shared.types import Literal
8-
97
Model = TypeVar("Model", bound=BaseModel)
108
EnvelopeModel = TypeVar("EnvelopeModel")
119
EventParserReturnType = TypeVar("EventParserReturnType")

aws_lambda_powertools/utilities/streaming/s3_object.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
Any,
88
Iterable,
99
List,
10+
Literal,
1011
Optional,
1112
Sequence,
1213
TypeVar,
@@ -15,7 +16,6 @@
1516
overload,
1617
)
1718

18-
from aws_lambda_powertools.shared.types import Literal
1919
from aws_lambda_powertools.utilities.streaming._s3_seekable_io import _S3SeekableIO
2020
from aws_lambda_powertools.utilities.streaming.transformations import (
2121
CsvTransform,

examples/event_handler_bedrock_agents/src/assert_bedrock_agent_response_module.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import time
22

3+
from typing_extensions import Annotated
4+
35
from aws_lambda_powertools import Logger, Tracer
46
from aws_lambda_powertools.event_handler import BedrockAgentResolver
57
from aws_lambda_powertools.event_handler.openapi.params import Body
6-
from aws_lambda_powertools.shared.types import Annotated
78
from aws_lambda_powertools.utilities.typing import LambdaContext
89

910
tracer = Tracer()

examples/event_handler_graphql/src/assert_async_graphql_response_module.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import asyncio
2-
from typing import List
2+
from typing import List, TypedDict
33

44
import aiohttp
55

66
from aws_lambda_powertools import Logger, Tracer
77
from aws_lambda_powertools.event_handler import AppSyncResolver
88
from aws_lambda_powertools.logging import correlation_paths
9-
from aws_lambda_powertools.shared.types import TypedDict
109
from aws_lambda_powertools.tracing import aiohttp_trace_config
1110
from aws_lambda_powertools.utilities.typing import LambdaContext
1211

examples/event_handler_graphql/src/assert_graphql_response_module.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import List
1+
from typing import List, TypedDict
22

33
from aws_lambda_powertools import Logger, Tracer
44
from aws_lambda_powertools.event_handler import AppSyncResolver
55
from aws_lambda_powertools.logging import correlation_paths
6-
from aws_lambda_powertools.shared.types import TypedDict
76
from aws_lambda_powertools.utilities.typing import LambdaContext
87

98
tracer = Tracer()

examples/event_handler_graphql/src/async_resolvers.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import asyncio
2-
from typing import List
2+
from typing import List, TypedDict
33

44
import aiohttp
55

66
from aws_lambda_powertools import Logger, Tracer
77
from aws_lambda_powertools.event_handler import AppSyncResolver
88
from aws_lambda_powertools.logging import correlation_paths
9-
from aws_lambda_powertools.shared.types import TypedDict
109
from aws_lambda_powertools.tracing import aiohttp_trace_config
1110
from aws_lambda_powertools.utilities.typing import LambdaContext
1211

examples/event_handler_graphql/src/custom_models.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import List
1+
from typing import List, TypedDict
22

33
from aws_lambda_powertools import Logger, Tracer
44
from aws_lambda_powertools.event_handler import AppSyncResolver
55
from aws_lambda_powertools.logging import correlation_paths
6-
from aws_lambda_powertools.shared.types import TypedDict
76
from aws_lambda_powertools.utilities.data_classes.appsync import scalar_types_utils
87
from aws_lambda_powertools.utilities.data_classes.appsync_resolver_event import (
98
AppSyncResolverEvent,

examples/event_handler_graphql/src/getting_started_graphql_api_resolver.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
from typing import List
1+
from typing import List, TypedDict
22

33
import requests
44
from requests import Response
55

66
from aws_lambda_powertools import Logger, Tracer
77
from aws_lambda_powertools.event_handler import AppSyncResolver
88
from aws_lambda_powertools.logging import correlation_paths
9-
from aws_lambda_powertools.shared.types import TypedDict
109
from aws_lambda_powertools.utilities.data_classes.appsync import scalar_types_utils
1110
from aws_lambda_powertools.utilities.typing import LambdaContext
1211

examples/event_handler_graphql/src/graphql_transformer_merchant_info.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import List
1+
from typing import List, TypedDict
22

33
from aws_lambda_powertools import Logger, Tracer
44
from aws_lambda_powertools.event_handler import AppSyncResolver
55
from aws_lambda_powertools.logging import correlation_paths
6-
from aws_lambda_powertools.shared.types import TypedDict
76
from aws_lambda_powertools.utilities.data_classes.appsync import scalar_types_utils
87
from aws_lambda_powertools.utilities.typing import LambdaContext
98

examples/event_handler_graphql/src/graphql_transformer_search_merchant.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import List
1+
from typing import List, TypedDict
22

33
from aws_lambda_powertools import Logger, Tracer
44
from aws_lambda_powertools.event_handler import AppSyncResolver
55
from aws_lambda_powertools.logging import correlation_paths
6-
from aws_lambda_powertools.shared.types import TypedDict
76
from aws_lambda_powertools.utilities.data_classes.appsync import scalar_types_utils
87
from aws_lambda_powertools.utilities.typing import LambdaContext
98

examples/event_handler_graphql/src/nested_mappings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from typing import List
1+
from typing import List, TypedDict
22

33
from aws_lambda_powertools import Logger, Tracer
44
from aws_lambda_powertools.event_handler import AppSyncResolver
55
from aws_lambda_powertools.logging import correlation_paths
6-
from aws_lambda_powertools.shared.types import TypedDict
76
from aws_lambda_powertools.utilities.typing import LambdaContext
87

98
tracer = Tracer()

examples/event_handler_graphql/src/split_operation_append_context_module.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from typing import List
1+
from typing import List, TypedDict
22

33
from aws_lambda_powertools import Logger, Tracer
44
from aws_lambda_powertools.event_handler.appsync import Router
5-
from aws_lambda_powertools.shared.types import TypedDict
65

76
tracer = Tracer()
87
logger = Logger()

examples/event_handler_graphql/src/split_operation_module.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
from typing import List
1+
from typing import List, TypedDict
22

33
from aws_lambda_powertools import Logger, Tracer
44
from aws_lambda_powertools.event_handler.appsync import Router
5-
from aws_lambda_powertools.shared.types import TypedDict
65

76
tracer = Tracer()
87
logger = Logger()

0 commit comments

Comments
 (0)