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

docs(typing): snippets split, improved, and lint #1465

Merged
merged 4 commits into from
Aug 25, 2022
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
Binary file removed docs/media/utilities_typing.png
Binary file not shown.
Binary file added docs/media/utilities_typing_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/media/utilities_typing_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/media/utilities_typing_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 31 additions & 8 deletions docs/utilities/typing.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,40 @@ description: Utility

This typing utility provides static typing classes that can be used to ease the development by providing the IDE type hints.

![Utilities Typing](../media/utilities_typing.png)
## Key features

* Add static typing classes
* Ease the development by leveraging your IDE's type hints
* Avoid common typing mistakes in Python

![Utilities Typing](../media/utilities_typing_1.png)

## Getting started

???+ tip
All examples shared in this documentation are available within the [project repository](https://github.com/awslabs/aws-lambda-powertools-python/tree/develop/examples){target="_blank"}.

We provide static typing for any context methods or properties implemented by [Lambda context object](https://docs.aws.amazon.com/lambda/latest/dg/python-context.html){target="_blank"}.

## LambdaContext

The `LambdaContext` typing is typically used in the handler method for the Lambda function.

```python hl_lines="4" title="Annotating Lambda context type"
from typing import Any, Dict
from aws_lambda_powertools.utilities.typing import LambdaContext
=== "getting_started_validator_decorator_function.py"

```python hl_lines="1 4"
--8<-- "examples/typing/src/getting_started_typing_function.py"
```

## Working with context methods and properties

Using `LambdaContext` typing makes it possible to access information and hints of all properties and methods implemented by Lambda context object.

=== "working_with_context_function.py"

```python hl_lines="6 16 25 26"
--8<-- "examples/typing/src/working_with_context_function.py"
```

def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]:
# Insert business logic
return event
```
![Utilities Typing All](../media/utilities_typing_2.png)
![Utilities Typing Specific](../media/utilities_typing_3.png)
6 changes: 6 additions & 0 deletions examples/typing/src/getting_started_typing_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from aws_lambda_powertools.utilities.typing import LambdaContext


def handler(event: dict, context: LambdaContext) -> dict:
# Insert business logic
return event
32 changes: 32 additions & 0 deletions examples/typing/src/working_with_context_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from time import sleep

import requests

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext

logger = Logger()


def lambda_handler(event, context: LambdaContext) -> dict:

limit_execution: int = 1000 # milliseconds

# scrape website and exit before lambda timeout
while context.get_remaining_time_in_millis() > limit_execution:

comments: requests.Response = requests.get("https://jsonplaceholder.typicode.com/comments")
# add logic here and save the results of the request to an S3 bucket, for example.

logger.info(
{
"operation": "scrape_website",
"request_id": context.aws_request_id,
"remaining_time": context.get_remaining_time_in_millis(),
"comments": comments.json()[:2],
}
)

sleep(1)

return {"message": "Success"}