Skip to content

Commit fe26d1c

Browse files
refactor(parser): only infer type hints when necessary (#4183)
fix(parser): only infer type hints when necessary This was breaking in codebases that conditionally import type hints using the `typing.TYPE_CHECKING` constant as described in [PEP 563](https://peps.python.org/pep-0563/#runtime-annotation-resolution-and-type-checking). When the model is provided, there is no need to infer anything. Signed-off-by: Raymond Butcher <[email protected]>
1 parent 24acbfe commit fe26d1c

File tree

1 file changed

+10
-9
lines changed
  • aws_lambda_powertools/utilities/parser

1 file changed

+10
-9
lines changed

Diff for: aws_lambda_powertools/utilities/parser/parser.py

+10-9
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,17 @@ def handler(event: Order, context: LambdaContext):
8383
When envelope given does not implement BaseEnvelope
8484
"""
8585

86-
# The first parameter of a Lambda function is always the event
87-
# This line get the model informed in the event_parser function
88-
# or the first parameter of the function by using typing.get_type_hints
89-
type_hints = typing.get_type_hints(handler)
90-
model = model or (list(type_hints.values())[0] if type_hints else None)
9186
if model is None:
92-
raise InvalidModelTypeError(
93-
"The model must be provided either as the `model` argument to `event_parser`"
94-
"or as the type hint of `event` in the handler that it wraps",
95-
)
87+
# The first parameter of a Lambda function is always the event.
88+
# Get the first parameter's type by using typing.get_type_hints.
89+
type_hints = typing.get_type_hints(handler)
90+
if type_hints:
91+
model = list(type_hints.values())[0]
92+
if model is None:
93+
raise InvalidModelTypeError(
94+
"The model must be provided either as the `model` argument to `event_parser`"
95+
"or as the type hint of `event` in the handler that it wraps",
96+
)
9697

9798
if envelope:
9899
parsed_event = parse(event=event, model=model, envelope=envelope)

0 commit comments

Comments
 (0)