Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(parser): only infer type hints when necessary #4183

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions aws_lambda_powertools/utilities/parser/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,17 @@ def handler(event: Order, context: LambdaContext):
When envelope given does not implement BaseEnvelope
"""

# The first parameter of a Lambda function is always the event
# This line get the model informed in the event_parser function
# or the first parameter of the function by using typing.get_type_hints
type_hints = typing.get_type_hints(handler)
model = model or (list(type_hints.values())[0] if type_hints else None)
if model is None:
raise InvalidModelTypeError(
"The model must be provided either as the `model` argument to `event_parser`"
"or as the type hint of `event` in the handler that it wraps",
)
# The first parameter of a Lambda function is always the event.
# Get the first parameter's type by using typing.get_type_hints.
type_hints = typing.get_type_hints(handler)
if type_hints:
model = list(type_hints.values())[0]
if model is None:
raise InvalidModelTypeError(
"The model must be provided either as the `model` argument to `event_parser`"
"or as the type hint of `event` in the handler that it wraps",
)

if envelope:
parsed_event = parse(event=event, model=model, envelope=envelope)
Expand Down