mypy-boto3-keyspaces Type Stubs for AWS Keyspaces
mypy-boto3-keyspaces provides precise type annotations for the `boto3` AWS SDK, specifically for the Keyspaces (for Apache Cassandra) service. This library enables static type checking with tools like `mypy`, improving code reliability and developer experience when interacting with AWS Keyspaces. The current version is 1.42.31, released frequently to align with `boto3` and `botocore` updates.
Warnings
- breaking Support for Python 3.8 was removed starting with `mypy-boto3-builder` version 8.12.0. If you are using `mypy-boto3-keyspaces` generated by this builder version or later, Python 3.9+ is required.
- breaking TypeDef naming conventions changed significantly with `mypy-boto3-builder` version 8.9.0. Many TypeDefs that previously had a `Request` suffix at the end (e.g., `CreateDistributionRequestRequestTypeDef`) were shortened or had suffixes moved for consistency.
- gotcha For type checking a specific AWS service (like Keyspaces), you must install its dedicated `mypy-boto3-servicename` package (e.g., `mypy-boto3-keyspaces`), not just `boto3-stubs` or `mypy-boto3-builder`.
- gotcha Type checking only occurs when `mypy` is explicitly run on your code. Installing `mypy-boto3-keyspaces` provides the stubs, but `mypy` must be installed and executed for static analysis.
Install
-
pip install mypy-boto3-keyspaces -
pip install boto3 mypy
Imports
- KeyspacesClient
from mypy_boto3_keyspaces.client import KeyspacesClient
- CreateTableRequestRequestTypeDef
from mypy_boto3_keyspaces.type_defs import CreateTableRequestRequestTypeDef
Quickstart
import boto3
from mypy_boto3_keyspaces.client import KeyspacesClient
from typing import TYPE_CHECKING
import os
# Configure AWS credentials and region if not already set via environment variables or ~/.aws/credentials
# For example, using environment variables:
# os.environ['AWS_ACCESS_KEY_ID'] = 'YOUR_ACCESS_KEY'
# os.environ['AWS_SECRET_ACCESS_KEY'] = 'YOUR_SECRET_KEY'
# os.environ['AWS_REGION'] = 'us-east-1'
if TYPE_CHECKING:
# mypy will use these imports for type checking
client: KeyspacesClient = boto3.client("keyspaces", region_name=os.environ.get('AWS_REGION', 'us-east-1'))
# Example: List tables (requires appropriate IAM permissions)
try:
response = client.list_tables(keyspaceName='my_keyspace')
print("Tables:", response.get("TableNames"))
except Exception as e:
print(f"Error listing tables: {e}")
else:
# Runtime code doesn't strictly need the mypy-boto3 imports
client = boto3.client("keyspaces", region_name=os.environ.get('AWS_REGION', 'us-east-1'))
# Example: List tables
try:
response = client.list_tables(keyspaceName='my_keyspace')
print("Tables:", response.get("TableNames"))
except Exception as e:
print(f"Error listing tables: {e}")
print("Code execution finished. Run `mypy your_script.py` for type checking.")