Skip to content

Commit f443c8f

Browse files
leandrodamascenarubenfonsecaheitorlessa
authored
docs(typing): snippets split, improved, and lint (#1465)
Co-authored-by: Ruben Fonseca <fonseka@gmail.com> Co-authored-by: Heitor Lessa <lessa@amazon.com>
1 parent 200ac67 commit f443c8f

7 files changed

+69
-8
lines changed

docs/media/utilities_typing.png

-79.9 KB
Binary file not shown.

docs/media/utilities_typing_1.png

70.2 KB
Loading

docs/media/utilities_typing_2.png

104 KB
Loading

docs/media/utilities_typing_3.png

119 KB
Loading

docs/utilities/typing.md

+31-8
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,40 @@ description: Utility
77

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

10-
![Utilities Typing](../media/utilities_typing.png)
10+
## Key features
11+
12+
* Add static typing classes
13+
* Ease the development by leveraging your IDE's type hints
14+
* Avoid common typing mistakes in Python
15+
16+
![Utilities Typing](../media/utilities_typing_1.png)
17+
18+
## Getting started
19+
20+
???+ tip
21+
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"}.
22+
23+
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"}.
1124

1225
## LambdaContext
1326

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

16-
```python hl_lines="4" title="Annotating Lambda context type"
17-
from typing import Any, Dict
18-
from aws_lambda_powertools.utilities.typing import LambdaContext
29+
=== "getting_started_validator_decorator_function.py"
30+
31+
```python hl_lines="1 4"
32+
--8<-- "examples/typing/src/getting_started_typing_function.py"
33+
```
34+
35+
## Working with context methods and properties
36+
37+
Using `LambdaContext` typing makes it possible to access information and hints of all properties and methods implemented by Lambda context object.
38+
39+
=== "working_with_context_function.py"
40+
41+
```python hl_lines="6 16 25 26"
42+
--8<-- "examples/typing/src/working_with_context_function.py"
43+
```
1944

20-
def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]:
21-
# Insert business logic
22-
return event
23-
```
45+
![Utilities Typing All](../media/utilities_typing_2.png)
46+
![Utilities Typing Specific](../media/utilities_typing_3.png)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from aws_lambda_powertools.utilities.typing import LambdaContext
2+
3+
4+
def handler(event: dict, context: LambdaContext) -> dict:
5+
# Insert business logic
6+
return event
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from time import sleep
2+
3+
import requests
4+
5+
from aws_lambda_powertools import Logger
6+
from aws_lambda_powertools.utilities.typing import LambdaContext
7+
8+
logger = Logger()
9+
10+
11+
def lambda_handler(event, context: LambdaContext) -> dict:
12+
13+
limit_execution: int = 1000 # milliseconds
14+
15+
# scrape website and exit before lambda timeout
16+
while context.get_remaining_time_in_millis() > limit_execution:
17+
18+
comments: requests.Response = requests.get("https://jsonplaceholder.typicode.com/comments")
19+
# add logic here and save the results of the request to an S3 bucket, for example.
20+
21+
logger.info(
22+
{
23+
"operation": "scrape_website",
24+
"request_id": context.aws_request_id,
25+
"remaining_time": context.get_remaining_time_in_millis(),
26+
"comments": comments.json()[:2],
27+
}
28+
)
29+
30+
sleep(1)
31+
32+
return {"message": "Success"}

0 commit comments

Comments
 (0)