1
+ from __future__ import annotations
2
+
1
3
import logging
2
4
import typing
3
- from typing import Any , Callable , Dict , Optional , Type , overload
5
+ from typing import Any , Callable , Dict , Optional , Type , TypeVar , overload
4
6
5
7
from pydantic import PydanticSchemaGenerationError , TypeAdapter , ValidationError
6
8
7
9
from aws_lambda_powertools .middleware_factory import lambda_handler_decorator
8
10
from aws_lambda_powertools .utilities .parser .envelopes .base import Envelope
9
11
from aws_lambda_powertools .utilities .parser .exceptions import InvalidEnvelopeError , InvalidModelTypeError
10
- from aws_lambda_powertools .utilities .parser .types import EventParserReturnType , Model
12
+ from aws_lambda_powertools .utilities .parser .types import EventParserReturnType
11
13
from aws_lambda_powertools .utilities .typing import LambdaContext
12
14
15
+ T = TypeVar ("T" )
16
+
13
17
logger = logging .getLogger (__name__ )
14
18
15
19
@@ -18,7 +22,7 @@ def event_parser(
18
22
handler : Callable [..., EventParserReturnType ],
19
23
event : Dict [str , Any ],
20
24
context : LambdaContext ,
21
- model : Optional [Type [ Model ]] = None ,
25
+ model : Optional [type [ T ]] = None ,
22
26
envelope : Optional [Type [Envelope ]] = None ,
23
27
** kwargs : Any ,
24
28
) -> EventParserReturnType :
@@ -108,14 +112,14 @@ def handler(event: Order, context: LambdaContext):
108
112
109
113
110
114
@overload
111
- def parse (event : Dict [str , Any ], model : Type [ Model ]) -> Model : ... # pragma: no cover
115
+ def parse (event : Dict [str , Any ], model : type [ T ]) -> T : ... # pragma: no cover
112
116
113
117
114
118
@overload
115
- def parse (event : Dict [str , Any ], model : Type [ Model ], envelope : Type [Envelope ]) -> Model : ... # pragma: no cover
119
+ def parse (event : Dict [str , Any ], model : type [ T ], envelope : Type [Envelope ]) -> T : ... # pragma: no cover
116
120
117
121
118
- def parse (event : Dict [str , Any ], model : Type [ Model ], envelope : Optional [Type [Envelope ]] = None ):
122
+ def parse (event : Dict [str , Any ], model : type [ T ], envelope : Optional [Type [Envelope ]] = None ):
119
123
"""Standalone function to parse & validate events using Pydantic models
120
124
121
125
Typically used when you need fine-grained control over error handling compared to event_parser decorator.
@@ -192,7 +196,9 @@ def handler(event: Order, context: LambdaContext):
192
196
# Pydantic raises PydanticSchemaGenerationError when the model is not a Pydantic model
193
197
# This is seen in the tests where we pass a non-Pydantic model type to the parser or
194
198
# when we pass a data structure that does not match the model (trying to parse a true/false/etc into a model)
195
- except (ValidationError , PydanticSchemaGenerationError ) as exc :
199
+ except PydanticSchemaGenerationError as exc :
200
+ raise InvalidModelTypeError (f"The event supplied is unable to be validated into { type (model )} " ) from exc
201
+ except ValidationError as exc :
196
202
raise InvalidModelTypeError (
197
203
f"Error: { str (exc )} . Please ensure the Input model inherits from BaseModel,\n "
198
204
"and your payload adheres to the specified Input model structure.\n "
0 commit comments