Skip to content

fix(parser): Improve types for parser.py #419

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

Merged
Merged
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions aws_lambda_powertools/utilities/parser/parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Any, Callable, Dict, Optional
from typing import Any, Callable, Dict, Optional, Type, TypeVar, Union

from ...middleware_factory import lambda_handler_decorator
from ..typing import LambdaContext
Expand All @@ -10,14 +10,17 @@
logger = logging.getLogger(__name__)


EventParserReturnType = TypeVar("EventParserReturnType")


@lambda_handler_decorator
def event_parser(
handler: Callable[[Any, LambdaContext], Any],
handler: Callable[[Any, LambdaContext], EventParserReturnType],
event: Dict[str, Any],
context: LambdaContext,
model: Model,
envelope: Optional[Envelope] = None,
) -> Any:
model: Type[Model],
envelope: Optional[Union[Envelope, Type[Envelope]]] = None,
) -> EventParserReturnType:
"""Lambda handler decorator to parse & validate events using Pydantic models
It requires a model that implements Pydantic BaseModel to parse & validate the event.
Expand Down Expand Up @@ -83,7 +86,9 @@ def handler(event: Order, context: LambdaContext):
return handler(parsed_event, context)


def parse(event: Dict[str, Any], model: Model, envelope: Optional[Envelope] = None) -> Model:
def parse(
event: Dict[str, Any], model: Type[Model], envelope: Optional[Union[Envelope, Type[Envelope]]] = None
) -> Model:
"""Standalone function to parse & validate events using Pydantic models
Typically used when you need fine-grained control over error handling compared to event_parser decorator.
Expand Down
6 changes: 3 additions & 3 deletions docs/utilities/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,21 +272,21 @@ Here's an example of parsing a model found in an event coming from EventBridge,
}
}

ret = parse(model=UserModel, envelope=envelopes.EventBridgeModel, event=payload)
ret = parse(model=UserModel, envelope=envelopes.EventBridgeEnvelope, event=payload)

# Parsed model only contains our actual model, not the entire EventBridge + Payload parsed
assert ret.password1 == ret.password2

# Same behaviour but using our decorator
@event_parser(model=UserModel, envelope=envelopes.EventBridgeModel)
@event_parser(model=UserModel, envelope=envelopes.EventBridgeEnvelope)
def handler(event: UserModel, context: LambdaContext):
assert event.password1 == event.password2
```

**What's going on here, you might ask**:

1. We imported built-in `envelopes` from the parser utility
2. Used `envelopes.EventBridgeModel` as the envelope for our `UserModel` model
2. Used `envelopes.EventBridgeEnvelope` as the envelope for our `UserModel` model
3. Parser parsed the original event against the EventBridge model
4. Parser then parsed the `detail` key using `UserModel`

Expand Down