Skip to content

Commit e31107e

Browse files
committed
fix: don't require pydantic to run normal things
1 parent fefd4a3 commit e31107e

File tree

2 files changed

+79
-59
lines changed

2 files changed

+79
-59
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from functools import partial
1111
from http import HTTPStatus
1212
from typing import (
13+
TYPE_CHECKING,
1314
Any,
1415
Callable,
1516
Dict,
@@ -29,21 +30,6 @@
2930

3031
from aws_lambda_powertools.event_handler import content_types
3132
from aws_lambda_powertools.event_handler.exceptions import NotFoundError, ServiceError
32-
from aws_lambda_powertools.event_handler.openapi.compat import (
33-
GenerateJsonSchema,
34-
JsonSchemaValue,
35-
ModelField,
36-
get_compat_model_name_map,
37-
get_definitions,
38-
get_schema_from_model_field,
39-
)
40-
from aws_lambda_powertools.event_handler.openapi.dependant import get_dependant, get_flat_params
41-
from aws_lambda_powertools.event_handler.openapi.models import Contact, License, OpenAPI, Server, Tag
42-
from aws_lambda_powertools.event_handler.openapi.params import Dependant, Param
43-
from aws_lambda_powertools.event_handler.openapi.types import (
44-
COMPONENT_REF_TEMPLATE,
45-
TypeModelOrEnum,
46-
)
4733
from aws_lambda_powertools.event_handler.response import Response
4834
from aws_lambda_powertools.shared.functions import powertools_dev_is_set
4935
from aws_lambda_powertools.shared.json_encoder import Encoder
@@ -66,6 +52,21 @@
6652
_NAMED_GROUP_BOUNDARY_PATTERN = rf"(?P\1[{_SAFE_URI}{_UNSAFE_URI}\\w]+)"
6753
_ROUTE_REGEX = "^{}$"
6854

55+
if TYPE_CHECKING:
56+
from aws_lambda_powertools.event_handler.openapi.compat import (
57+
JsonSchemaValue,
58+
ModelField,
59+
)
60+
from aws_lambda_powertools.event_handler.openapi.models import (
61+
Contact,
62+
License,
63+
OpenAPI,
64+
Server,
65+
Tag,
66+
)
67+
from aws_lambda_powertools.event_handler.openapi.params import Dependant
68+
from aws_lambda_powertools.event_handler.openapi.types import TypeModelOrEnum
69+
6970

7071
class ProxyEventType(Enum):
7172
"""An enumerations of the supported proxy event types."""
@@ -202,7 +203,7 @@ def __init__(
202203
cache_control: Optional[str],
203204
middlewares: Optional[List[Callable[..., Response]]],
204205
description: Optional[str],
205-
tags: Optional[List[Tag]],
206+
tags: Optional[List["Tag"]],
206207
):
207208
"""
208209
@@ -339,11 +340,15 @@ def _build_middleware_stack(self, router_middlewares: List[Callable[..., Any]])
339340
def _get_openapi_path(
340341
self,
341342
*,
342-
dependant: Dependant,
343+
dependant: "Dependant",
343344
operation_ids: Set[str],
344-
model_name_map: Dict[TypeModelOrEnum, str],
345-
field_mapping: Dict[Tuple[ModelField, Literal["validation", "serialization"]], JsonSchemaValue],
345+
model_name_map: Dict["TypeModelOrEnum", str],
346+
field_mapping: Dict[Tuple["ModelField", Literal["validation", "serialization"]], "JsonSchemaValue"],
346347
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
348+
from aws_lambda_powertools.event_handler.openapi.dependant import (
349+
get_flat_params,
350+
)
351+
347352
path = {}
348353
definitions: Dict[str, Any] = {}
349354

@@ -411,13 +416,18 @@ def _openapi_operation_metadata(self, operation_ids: Set[str]) -> Dict[str, Any]
411416
@staticmethod
412417
def _openapi_operation_parameters(
413418
*,
414-
all_route_params: Sequence[ModelField],
415-
model_name_map: Dict[TypeModelOrEnum, str],
419+
all_route_params: Sequence["ModelField"],
420+
model_name_map: Dict["TypeModelOrEnum", str],
416421
field_mapping: Dict[
417-
Tuple[ModelField, Literal["validation", "serialization"]],
418-
JsonSchemaValue,
422+
Tuple["ModelField", Literal["validation", "serialization"]],
423+
"JsonSchemaValue",
419424
],
420425
) -> List[Dict[str, Any]]:
426+
from aws_lambda_powertools.event_handler.openapi.compat import (
427+
get_schema_from_model_field,
428+
)
429+
from aws_lambda_powertools.event_handler.openapi.params import Param
430+
421431
parameters = []
422432
for param in all_route_params:
423433
field_info = param.field_info
@@ -452,16 +462,20 @@ def _openapi_operation_parameters(
452462
def _openapi_operation_return(
453463
*,
454464
operation_id: str,
455-
param: Optional[ModelField],
456-
model_name_map: Dict[TypeModelOrEnum, str],
465+
param: Optional["ModelField"],
466+
model_name_map: Dict["TypeModelOrEnum", str],
457467
field_mapping: Dict[
458-
Tuple[ModelField, Literal["validation", "serialization"]],
459-
JsonSchemaValue,
468+
Tuple["ModelField", Literal["validation", "serialization"]],
469+
"JsonSchemaValue",
460470
],
461471
) -> Dict[str, Any]:
462472
if param is None:
463473
return {}
464474

475+
from aws_lambda_powertools.event_handler.openapi.compat import (
476+
get_schema_from_model_field,
477+
)
478+
465479
return_schema = get_schema_from_model_field(
466480
field=param,
467481
model_name_map=model_name_map,
@@ -582,7 +596,7 @@ def route(
582596
compress: bool = False,
583597
cache_control: Optional[str] = None,
584598
description: Optional[str] = None,
585-
tags: Optional[List[Tag]] = None,
599+
tags: Optional[List["Tag"]] = None,
586600
middlewares: Optional[List[Callable[..., Any]]] = None,
587601
):
588602
raise NotImplementedError()
@@ -636,7 +650,7 @@ def get(
636650
cache_control: Optional[str] = None,
637651
middlewares: Optional[List[Callable[..., Any]]] = None,
638652
description: Optional[str] = None,
639-
tags: Optional[List[Tag]] = None,
653+
tags: Optional[List["Tag"]] = None,
640654
):
641655
"""Get route decorator with GET `method`
642656
@@ -670,7 +684,7 @@ def post(
670684
cache_control: Optional[str] = None,
671685
middlewares: Optional[List[Callable[..., Any]]] = None,
672686
description: Optional[str] = None,
673-
tags: Optional[List[Tag]] = None,
687+
tags: Optional[List["Tag"]] = None,
674688
):
675689
"""Post route decorator with POST `method`
676690
@@ -705,7 +719,7 @@ def put(
705719
cache_control: Optional[str] = None,
706720
middlewares: Optional[List[Callable[..., Any]]] = None,
707721
description: Optional[str] = None,
708-
tags: Optional[List[Tag]] = None,
722+
tags: Optional[List["Tag"]] = None,
709723
):
710724
"""Put route decorator with PUT `method`
711725
@@ -740,7 +754,7 @@ def delete(
740754
cache_control: Optional[str] = None,
741755
middlewares: Optional[List[Callable[..., Any]]] = None,
742756
description: Optional[str] = None,
743-
tags: Optional[List[Tag]] = None,
757+
tags: Optional[List["Tag"]] = None,
744758
):
745759
"""Delete route decorator with DELETE `method`
746760
@@ -774,7 +788,7 @@ def patch(
774788
cache_control: Optional[str] = None,
775789
middlewares: Optional[List[Callable]] = None,
776790
description: Optional[str] = None,
777-
tags: Optional[List[Tag]] = None,
791+
tags: Optional[List["Tag"]] = None,
778792
):
779793
"""Patch route decorator with PATCH `method`
780794
@@ -1005,12 +1019,12 @@ def get_openapi_schema(
10051019
openapi_version: str = "3.1.0",
10061020
summary: Optional[str] = None,
10071021
description: Optional[str] = None,
1008-
tags: Optional[List[Tag]] = None,
1009-
servers: Optional[List[Server]] = None,
1022+
tags: Optional[List["Tag"]] = None,
1023+
servers: Optional[List["Server"]] = None,
10101024
terms_of_service: Optional[str] = None,
1011-
contact: Optional[Contact] = None,
1012-
license_info: Optional[License] = None,
1013-
) -> OpenAPI:
1025+
contact: Optional["Contact"] = None,
1026+
license_info: Optional["License"] = None,
1027+
) -> "OpenAPI":
10141028
"""
10151029
Returns the OpenAPI schema as a pydantic model.
10161030
@@ -1043,6 +1057,17 @@ def get_openapi_schema(
10431057
The OpenAPI schema as a pydantic model.
10441058
"""
10451059

1060+
from aws_lambda_powertools.event_handler.openapi.compat import (
1061+
GenerateJsonSchema,
1062+
get_compat_model_name_map,
1063+
get_definitions,
1064+
)
1065+
from aws_lambda_powertools.event_handler.openapi.dependant import get_dependant
1066+
from aws_lambda_powertools.event_handler.openapi.models import OpenAPI, Server
1067+
from aws_lambda_powertools.event_handler.openapi.types import (
1068+
COMPONENT_REF_TEMPLATE,
1069+
)
1070+
10461071
# Start with the bare minimum required for a valid OpenAPI schema
10471072
info: Dict[str, Any] = {"title": title, "version": version}
10481073

@@ -1119,11 +1144,11 @@ def get_openapi_json_schema(
11191144
openapi_version: str = "3.1.0",
11201145
summary: Optional[str] = None,
11211146
description: Optional[str] = None,
1122-
tags: Optional[List[Tag]] = None,
1123-
servers: Optional[List[Server]] = None,
1147+
tags: Optional[List["Tag"]] = None,
1148+
servers: Optional[List["Server"]] = None,
11241149
terms_of_service: Optional[str] = None,
1125-
contact: Optional[Contact] = None,
1126-
license_info: Optional[License] = None,
1150+
contact: Optional["Contact"] = None,
1151+
license_info: Optional["License"] = None,
11271152
) -> str:
11281153
"""
11291154
Returns the OpenAPI schema as a JSON serializable dict
@@ -1177,7 +1202,7 @@ def route(
11771202
compress: bool = False,
11781203
cache_control: Optional[str] = None,
11791204
description: Optional[str] = None,
1180-
tags: Optional[List[Tag]] = None,
1205+
tags: Optional[List["Tag"]] = None,
11811206
middlewares: Optional[List[Callable[..., Any]]] = None,
11821207
):
11831208
"""Route decorator includes parameter `method`"""
@@ -1556,12 +1581,18 @@ def include_router(self, router: "Router", prefix: Optional[str] = None) -> None
15561581
self.route(*new_route, middlewares=middlewares)(func) # type: ignore
15571582

15581583
@staticmethod
1559-
def _get_fields_from_routes(routes: Sequence[Route]) -> List[ModelField]:
1584+
def _get_fields_from_routes(routes: Sequence[Route]) -> List["ModelField"]:
15601585
"""
15611586
Returns a list of fields from the routes
15621587
"""
1563-
responses_from_routes: List[ModelField] = []
1564-
request_fields_from_routes: List[ModelField] = []
1588+
1589+
from aws_lambda_powertools.event_handler.openapi.dependant import (
1590+
get_dependant,
1591+
get_flat_params,
1592+
)
1593+
1594+
responses_from_routes: List["ModelField"] = []
1595+
request_fields_from_routes: List["ModelField"] = []
15651596

15661597
for route in routes:
15671598
dependant = get_dependant(path=route.path, call=route.func)
@@ -1592,7 +1623,7 @@ def route(
15921623
compress: bool = False,
15931624
cache_control: Optional[str] = None,
15941625
description: Optional[str] = None,
1595-
tags: Optional[List[Tag]] = None,
1626+
tags: Optional[List["Tag"]] = None,
15961627
middlewares: Optional[List[Callable[..., Any]]] = None,
15971628
):
15981629
def register_route(func: Callable):
@@ -1640,7 +1671,7 @@ def route(
16401671
compress: bool = False,
16411672
cache_control: Optional[str] = None,
16421673
description: Optional[str] = None,
1643-
tags: Optional[List[Tag]] = None,
1674+
tags: Optional[List["Tag"]] = None,
16441675
middlewares: Optional[List[Callable[..., Any]]] = None,
16451676
):
16461677
# NOTE: see #1552 for more context.
Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +0,0 @@
1-
from aws_lambda_powertools.event_handler.openapi.models import (
2-
Example,
3-
Info,
4-
MediaType,
5-
Operation,
6-
Reference,
7-
Response,
8-
Schema,
9-
)
10-
11-
__all__ = ["Info", "Operation", "Response", "MediaType", "Reference", "Schema", "Example"]

0 commit comments

Comments
 (0)