Skip to content

feat: Static Types for AWS Lambda #149

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 11 commits into from
Sep 3, 2020
11 changes: 11 additions & 0 deletions aws_lambda_powertools/utilities/typing/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-

"""
Typing for developer ease in the IDE
"""

from .lambda_context import LambdaContext

__all__ = [
"LambdaContext",
]
25 changes: 25 additions & 0 deletions aws_lambda_powertools/utilities/typing/lambda_client_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
from typing import Any, Dict

from aws_lambda_powertools.utilities.typing.lambda_client_context_mobile_client import LambdaClientContextMobileClient


class LambdaClientContext(object):
_client: LambdaClientContextMobileClient
_custom: Dict[str, Any]
_env: Dict[str, Any]

@property
def client(self) -> LambdaClientContextMobileClient:
"""Client context that's provided to Lambda by the client application."""
return self._client

@property
def custom(self) -> Dict[str, Any]:
"""A dict of custom values set by the mobile client application."""
return self._custom

@property
def env(self) -> Dict[str, Any]:
"""A dict of environment information provided by the AWS SDK."""
return self._env
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-


class LambdaClientContextMobileClient(object):
"""Mobile Client context that's provided to Lambda by the client application."""

_installation_id: str
_app_title: str
_app_version_name: str
_app_version_code: str
_app_package_name: str

@property
def installation_id(self) -> str:
return self._installation_id

@property
def app_title(self) -> str:
return self._app_title

@property
def app_version_name(self) -> str:
return self._app_version_name

@property
def app_version_code(self) -> str:
return self._app_version_code

@property
def app_package_name(self) -> str:
return self._app_package_name
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-


class LambdaCognitoIdentity(object):
"""
Information about the Amazon Cognito identity that authorized the request.
"""

_cognito_identity_id: str
_cognito_identity_pool_id: str

@property
def cognito_identity_id(self) -> str:
"""The authenticated Amazon Cognito identity."""
return self._cognito_identity_id

@property
def cognito_identity_pool_id(self) -> str:
"""The Amazon Cognito identity pool that authorized the invocation."""
return self._cognito_identity_pool_id
80 changes: 80 additions & 0 deletions aws_lambda_powertools/utilities/typing/lambda_context.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
from aws_lambda_powertools.utilities.typing.lambda_client_context import LambdaClientContext
from aws_lambda_powertools.utilities.typing.lambda_cognito_identity import LambdaCognitoIdentity


class LambdaContext(object):
"""The LambdaContext static object can be used to ease the development by providing the IDE type hints.

Example
-------
**A Lambda function using LambdaContext**

>>> from aws_lambda_powertools.utilities.typing import LambdaContext
>>>
>>> def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]:
>>> # Insert business logic
>>> return event

"""

_function_name: str
_function_version: str
_invoked_function_arn: str
_memory_limit_in_mb: int
_aws_request_id: str
_log_group_name: str
_log_stream_name: str
_identity: LambdaCognitoIdentity
_client_context: LambdaClientContext

@property
def function_name(self) -> str:
"""The name of the Lambda function."""
return self._function_name

@property
def function_version(self) -> str:
"""The version of the function."""
return self._function_version

@property
def invoked_function_arn(self) -> str:
"""The Amazon Resource Name (ARN) that's used to invoke the function. Indicates if the invoker specified a
version number or alias."""
return self._invoked_function_arn

@property
def memory_limit_in_mb(self) -> int:
"""The amount of memory that's allocated for the function."""
return self._memory_limit_in_mb

@property
def aws_request_id(self) -> str:
"""The identifier of the invocation request."""
return self._aws_request_id

@property
def log_group_name(self) -> str:
"""The log group for the function."""
return self._log_group_name

@property
def log_stream_name(self) -> str:
"""The log stream for the function instance."""
return self._log_stream_name

@property
def identity(self) -> LambdaCognitoIdentity:
"""(mobile apps) Information about the Amazon Cognito identity that authorized the request."""
return self._identity

@property
def client_context(self) -> LambdaClientContext:
"""(mobile apps) Client context that's provided to Lambda by the client application."""
return self._client_context

@staticmethod
def get_remaining_time_in_millis() -> int:
"""Returns the number of milliseconds left before the execution times out."""
return 0
1 change: 1 addition & 0 deletions docs/content/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Utility | Description
[Metrics](./core/metrics) | Custom Metrics created asynchronously via CloudWatch Embedded Metric Format (EMF)
[Bring your own middleware](.//utilities/middleware_factory) | Decorator factory to create your own middleware to run logic before, and after each Lambda invocation
[Parameters utility](./utilities/parameters) | Retrieve parameter values from AWS Systems Manager Parameter Store, AWS Secrets Manager, or Amazon DynamoDB, and cache them for a specific amount of time
[Typing utility](./utilities/typing) | Static typing classes to speedup development in your IDE

## Environment variables

Expand Down
Binary file added docs/content/media/utilities_typing.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions docs/content/utilities/typing.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Typing
description: Utility
---

import Note from "../../src/components/Note"

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)

## LambdaContext

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

```python:title=index.py
from typing import Any, Dict
from aws_lambda_powertools.utilities.typing import LambdaContext

# highlight-start
def handler(event: Dict[str, Any], context: LambdaContext) -> Dict[str, Any]:
# highlight-end
# Insert business logic
return event
```
1 change: 1 addition & 0 deletions docs/gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
'utilities/middleware_factory',
'utilities/parameters',
'utilities/batch',
'utilities/typing',
],
},
navConfig: {
Expand Down
6 changes: 6 additions & 0 deletions tests/functional/test_utilities_typing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from aws_lambda_powertools.utilities.typing import LambdaContext


def test_typing():
context = LambdaContext()
assert context.get_remaining_time_in_millis() == 0