mypy-boto3-billing
mypy-boto3-billing provides type annotations for the AWS boto3 Billing service. It's part of the `mypy-boto3` project, which generates type stubs for all boto3 services. The library is actively maintained, with frequent updates corresponding to boto3 releases and improvements from the `mypy-boto3-builder`.
Warnings
- breaking Starting with `mypy-boto3-builder` version 8.12.0, Python 3.8 is no longer supported across all generated `mypy-boto3` packages, including `mypy-boto3-billing`. Projects using these stubs must upgrade to Python 3.9 or newer.
- breaking Type Definition (TypeDef) names for service operations were shortened, and the `Extra` postfix for conflicting names was moved in `mypy-boto3-builder` version 8.9.0. While `billing` specific changes might be minimal, this is a general breaking change in the naming convention of generated types.
- gotcha To get full type checking benefits, you must explicitly annotate the boto3 client with the type from `mypy_boto3_billing.client`. Without `client: BillingClient = ...`, mypy will only see the untyped `boto3` client.
- gotcha Using an older `boto3` version than what the stubs were generated for can lead to mismatches and type checking errors, as the API might have evolved.
Install
-
pip install mypy-boto3-billing boto3 -
pip install mypy-boto3-billing types-boto3
Imports
- BillingClient
from mypy_boto3_billing.client import BillingClient
- ListAccountGroupsOutputTypeDef
from mypy_boto3_billing.type_defs import ListAccountGroupsOutputTypeDef
Quickstart
import boto3
from mypy_boto3_billing.client import BillingClient
import os
# Ensure AWS credentials are set up (e.g., via environment variables or ~/.aws/credentials)
# For a runnable example, we'll use dummy credentials if not found
os.environ.setdefault('AWS_ACCESS_KEY_ID', 'test')
os.environ.setdefault('AWS_SECRET_ACCESS_KEY', 'test')
os.environ.setdefault('AWS_SESSION_TOKEN', 'test')
os.environ.setdefault('AWS_DEFAULT_REGION', 'us-east-1')
def get_billing_summary():
client: BillingClient = boto3.client('billing')
try:
response = client.list_billing_groups(
Filters={'And': [{'AttributeValue': 'active', 'AttributeName': 'Status'}]},
MaxResults=5
)
print("Successfully retrieved billing groups summary.")
for group in response.get('BillingGroups', []):
print(f" Billing Group ARN: {group.get('Arn')}, Name: {group.get('Name')}")
except Exception as e:
print(f"Error retrieving billing groups: {e}")
if __name__ == '__main__':
get_billing_summary()