{"id":3038,"library":"mypy-boto3-ce","title":"mypy-boto3-ce: Type Annotations for AWS Cost Explorer","description":"mypy-boto3-ce provides comprehensive type annotations for the AWS Boto3 Cost Explorer (CE) service, ensuring static type checking for your Python code. It is automatically generated by `mypy-boto3-builder` (currently version 8.12.0) and is updated frequently to align with `boto3` and `botocore` releases, enhancing code quality and developer experience by catching potential AWS-related errors before runtime.","status":"active","version":"1.42.28","language":"en","source_language":"en","source_url":"https://github.com/youtype/mypy_boto3_builder","tags":["boto3","aws","mypy","type-hints","cost-explorer","static-analysis","cloud"],"install":[{"cmd":"pip install mypy-boto3-ce","lang":"bash","label":"Standalone package"},{"cmd":"pip install 'boto3-stubs[ce]'","lang":"bash","label":"As a boto3-stubs extra"}],"dependencies":[{"reason":"Provides the AWS SDK for Python that these stubs type-annotate.","package":"boto3","optional":false},{"reason":"Primary static type checker for which these annotations are designed.","package":"mypy","optional":true},{"reason":"Required for some advanced type features on Python versions older than 3.9, though 3.8 support has been removed.","package":"typing-extensions","optional":true}],"imports":[{"note":"Use this for type-hinting a Boto3 Cost Explorer client.","symbol":"CostExplorerClient","correct":"from mypy_boto3_ce.client import CostExplorerClient"},{"note":"Import TypeDefs for precise type checking of Boto3 response structures and parameters.","symbol":"AnomalyDateIntervalTypeDef","correct":"from mypy_boto3_ce.type_defs import AnomalyDateIntervalTypeDef"},{"note":"Import Literals for checking specific string values.","symbol":"AccountScopeType","correct":"from mypy_boto3_ce.literals import AccountScopeType"}],"quickstart":{"code":"from typing import TYPE_CHECKING\nimport boto3\nimport os\n\nif TYPE_CHECKING:\n    from mypy_boto3_ce.client import CostExplorerClient\n    from mypy_boto3_ce.type_defs import GetCostAndUsageResponseTypeDef\n\n\n# It's recommended to use explicit type hints for boto3.client for better IDE support\n# and type checking, especially if not using the 'boto3-stubs' full package.\ndef get_cost_explorer_data(client: \"CostExplorerClient\") -> \"GetCostAndUsageResponseTypeDef\":\n    # Example: Retrieve cost and usage data for the last 7 days\n    response = client.get_cost_and_usage(\n        TimePeriod={\n            'Start': '2023-01-01',\n            'End': '2023-01-08'\n        },\n        Granularity='DAILY',\n        Metrics=['BlendedCost', 'UnblendedCost'],\n        # Add other parameters as needed, e.g., Filter, GroupBy\n    )\n    return response\n\n\nif __name__ == '__main__':\n    # Ensure AWS credentials are configured (e.g., via environment variables or AWS CLI config)\n    # For this example, we'll create a client. In a real app, you might pass an existing client.\n    print(\"Initializing Boto3 Cost Explorer client...\")\n    ce_client: \"CostExplorerClient\" = boto3.client(\n        \"ce\", \n        region_name=os.environ.get('AWS_REGION', 'us-east-1'),\n        aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', 'DUMMY_KEY'),\n        aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', 'DUMMY_SECRET')\n    )\n    \n    try:\n        # The `get_cost_explorer_data` function now benefits from type hints\n        # for both the client object and the response structure.\n        costs_data = get_cost_explorer_data(ce_client)\n        print(\"Successfully retrieved Cost Explorer data (truncated for brevity):\")\n        # In a real scenario, you'd process costs_data. For this example, just print a key.\n        if costs_data and 'ResultsByTime' in costs_data:\n            print(f\"First result time period: {costs_data['ResultsByTime'][0]['TimePeriod']}\")\n        else:\n            print(\"No results or unexpected response structure.\")\n    except Exception as e:\n        print(f\"An error occurred: {e}\")","lang":"python","description":"This quickstart demonstrates how to initialize a Boto3 Cost Explorer client with type hints from `mypy-boto3-ce` and use it to retrieve cost and usage data. It uses the `TYPE_CHECKING` guard to ensure `mypy-boto3-ce` is only a development dependency."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or newer.","message":"Support for Python 3.8 was officially removed with `mypy-boto3-builder` version 8.12.0. Ensure your project runs on Python 3.9 or higher.","severity":"breaking","affected_versions":"mypy-boto3-builder >= 8.12.0"},{"fix":"Review and update type hint imports and usage in your codebase to match the new naming conventions. Static analysis tools will highlight these changes.","message":"TypeDef naming conventions were changed in `mypy-boto3-builder` 8.9.0. This could affect existing type hints for method arguments (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`) and conflicting TypeDef postfixes (e.g., `CreateDistributionExtraRequestTypeDef` to `CreateDistributionRequestExtraTypeDef`).","severity":"breaking","affected_versions":"mypy-boto3-builder >= 8.9.0"},{"fix":"Always explicitly type-hint your Boto3 clients and resources (e.g., `client: CostExplorerClient = boto3.client(\"ce\")`).","message":"Explicit type annotations are highly recommended for `boto3.client`, `boto3.session.client`, `client.get_waiter`, and `client.get_paginator` calls. While some IDEs (like PyCharm) and static analyzers (like Mypy) might infer types, VSCode's Python extension might not fully support function overloads, leading to a lack of auto-completion and type checking without explicit hints.","severity":"gotcha","affected_versions":"All"},{"fix":"Wrap type-only imports within `if TYPE_CHECKING:` blocks and provide runtime fallbacks (e.g., `Client = object`) if necessary, although for simple client usage, it's often not needed at runtime.","message":"To avoid a runtime dependency on `mypy-boto3-ce` in production environments, it is common practice to guard imports with `if TYPE_CHECKING:` from the `typing` module.","severity":"gotcha","affected_versions":"All"},{"fix":"Monitor `mypy-boto3-builder` release notes and `boto3` documentation for service changes. Update your `pip install` commands and import paths accordingly if a service you use is affected.","message":"The `mypy-boto3-builder` project occasionally removes or renames service stubs (e.g., `sms-voice` was removed in 8.11.0 and replaced by `pinpoint-sms-voice`). While `mypy-boto3-ce` is stable, be aware that services may change, requiring updates to your stub installations.","severity":"deprecated","affected_versions":"mypy-boto3-builder >= 8.11.0 (general warning for ecosystem)"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}