Skip to content

Commit 1a8818d

Browse files
refactor(typing): reduce aws_lambda_powertools.shared.types usage (#4896)
* 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. * Docmentation fix + small changes --------- Co-authored-by: Leandro Damascena <[email protected]>
1 parent 08f2b53 commit 1a8818d

File tree

56 files changed

+98
-131
lines changed

Some content is hidden

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

56 files changed

+98
-131
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):

0 commit comments

Comments
 (0)