CloudWatchLogs Type Annotations for Boto3
mypy-boto3-logs provides static type annotations for the `boto3` CloudWatchLogs service, enhancing code quality and developer experience. It is generated by the `mypy-boto3-builder` project and is compatible with various type checkers like mypy and pyright, as well as IDEs like VSCode and PyCharm. The current version is 1.42.83, with frequent updates aligning with `boto3` releases and builder improvements.
Warnings
- breaking Python 3.8 support has been removed as of `mypy-boto3-builder` version 8.12.0. All `mypy-boto3-*` packages now require Python 3.9 or newer.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` 8.9.0. Method argument TypeDefs may use shorter names, and conflicting `Extra` postfixes were moved to the end. This may require updating import paths or references to specific TypedDicts.
- gotcha `mypy-boto3-*` packages migrated to PEP 561. While beneficial, this might impact older `mypy` configurations. Stub-only packages, per PEP 561, cannot be found via `MYPYPATH` and must be installed directly in the environment `mypy` inspects.
- gotcha PyCharm users may experience slow performance or high CPU usage due to Literal overloads. The `boto3-stubs-lite` packages are recommended as a workaround, or disable PyCharm's internal type checker and use `mypy` or `pyright` instead.
- gotcha When using `TYPE_CHECKING` for conditional imports, Pylint may incorrectly report undefined variables. A workaround is to assign `object` to the type hints in the non-`TYPE_CHECKING` branch.
Install
-
pip install mypy-boto3-logs boto3 mypy -
pip install 'boto3-stubs[logs]' mypy
Imports
- CloudWatchLogsClient
from mypy_boto3_logs.client import CloudWatchLogsClient
- CloudWatchLogsServiceResource
from mypy_boto3_logs.service_resource import CloudWatchLogsServiceResource
- PutLogEventsRequestRequestTypeDef
from mypy_boto3_logs.type_defs import PutLogEventsRequestRequestTypeDef
Quickstart
from typing import TYPE_CHECKING
import boto3
if TYPE_CHECKING:
from mypy_boto3_logs.client import CloudWatchLogsClient
from mypy_boto3_logs.type_defs import PutLogEventsResponseTypeDef
def get_logs_client() -> 'CloudWatchLogsClient':
"""Returns a type-hinted CloudWatchLogs client."""
session = boto3.Session(
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', ''),
aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', ''),
region_name=os.environ.get('AWS_REGION', 'us-east-1')
)
return session.client('logs')
def put_log_events_example():
"""Example of using the type-hinted CloudWatchLogs client."""
client = get_logs_client()
response: PutLogEventsResponseTypeDef = client.put_log_events(
logGroupName='my-log-group',
logStreamName='my-log-stream',
logEvents=[
{'timestamp': 1678886400000, 'message': 'Hello from mypy-boto3-logs!'}
]
)
print(f"Sequence Token: {response.get('nextSequenceToken')}")
if __name__ == '__main__':
import os
# Set dummy AWS credentials for local testing or ensure they are in env vars
# For actual use, configure boto3 credentials securely
if not os.environ.get('AWS_ACCESS_KEY_ID'):
os.environ['AWS_ACCESS_KEY_ID'] = 'TEST_ACCESS_KEY'
os.environ['AWS_SECRET_ACCESS_KEY'] = 'TEST_SECRET_KEY'
os.environ['AWS_REGION'] = 'us-east-1'
put_log_events_example()