{"id":1571,"library":"mypy-boto3-ec2","title":"mypy-boto3-ec2 Type Stubs","description":"mypy-boto3-ec2 provides type annotations for the boto3 EC2 service. It ensures that your boto3 client calls for EC2 are type-checked by tools like MyPy, catching potential errors at development time. The current version is 1.42.85, closely following boto3/botocore releases, with frequent updates tied to AWS API changes.","status":"active","version":"1.42.85","language":"en","source_language":"en","source_url":"https://github.com/youtype/mypy_boto3_builder","tags":["boto3","aws","mypy","type-hints","ec2","type-stubs","python"],"install":[{"cmd":"pip install mypy-boto3-ec2","lang":"bash","label":"Install `mypy-boto3-ec2`"},{"cmd":"pip install boto3 mypy","lang":"bash","label":"Required dependencies"}],"dependencies":[{"reason":"Provides the actual AWS SDK runtime; these stubs are for type-checking it.","package":"boto3"},{"reason":"The static type checker that utilizes these stubs.","package":"mypy"}],"imports":[{"symbol":"EC2Client","correct":"from mypy_boto3_ec2.client import EC2Client"},{"symbol":"EC2ServiceResource","correct":"from mypy_boto3_ec2.service_resource import EC2ServiceResource"},{"note":"Type definitions are typically found in the `type_defs` module, not directly in `client` or `service_resource`.","wrong":"from mypy_boto3_ec2.client import DescribeInstancesResultTypeDef","symbol":"DescribeInstancesResultTypeDef","correct":"from mypy_boto3_ec2.type_defs import DescribeInstancesResultTypeDef"}],"quickstart":{"code":"import boto3\nfrom mypy_boto3_ec2.client import EC2Client\nfrom mypy_boto3_ec2.type_defs import InstanceTypeDef, ReservationTypeDef\n\ndef list_ec2_instances() -> None:\n    # Type-hint the boto3 client for EC2\n    ec2_client: EC2Client = boto3.client('ec2')\n\n    print('Describing EC2 instances...')\n    response = ec2_client.describe_instances()\n\n    # Use type definitions for better type checking on the response\n    for reservation in response.get('Reservations', []):\n        reservation_typed: ReservationTypeDef = reservation\n        for instance in reservation_typed.get('Instances', []):\n            instance_typed: InstanceTypeDef = instance\n            instance_id = instance_typed.get('InstanceId', 'N/A')\n            instance_type = instance_typed.get('InstanceType', 'N/A')\n            state = instance_typed.get('State', {}).get('Name', 'N/A')\n            print(f'  Instance ID: {instance_id}, Type: {instance_type}, State: {state}')\n\nif __name__ == '__main__':\n    # This quickstart assumes AWS credentials are configured (e.g., via environment variables or ~/.aws/credentials)\n    list_ec2_instances()","lang":"python","description":"This example demonstrates how to use `mypy-boto3-ec2` to type-hint an EC2 client and process its responses with type-safe accessors. It lists all EC2 instances in the default region."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or higher. For example, `python3.9 -m pip install mypy-boto3-ec2`.","message":"Python 3.8 support was removed for all `mypy-boto3-*` packages, including `mypy-boto3-ec2`, starting from version `8.12.0` of the builder. Projects on Python 3.8 will need to upgrade their Python version to 3.9+.","severity":"breaking","affected_versions":">=8.12.0 (builder version, corresponds to `mypy-boto3-ec2` versions after this point)"},{"fix":"Ensure `pip install boto3==X.Y.Z mypy-boto3-ec2==X.Y.Z` where `X.Y.Z` is the desired boto3 version. The `mypy-boto3-*` packages are versioned to align with `boto3`.","message":"It's crucial to install `mypy-boto3-ec2` (or any `mypy-boto3-*` package) with a version that matches your `boto3` installation as closely as possible. Mismatched versions can lead to incorrect type hints or `mypy` errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Review and update explicit `TypeDef` imports and usage in your code to match the new, potentially shorter or reordered naming conventions. Rely on IDE auto-completion for correct names.","message":"From builder version `8.9.0`, TypeDef names for packed method arguments were shortened (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`). Additionally, conflicting TypeDef `Extra` postfixes were moved (e.g., `CreateDistributionExtraRequestTypeDef` became `CreateDistributionRequestExtraTypeDef`).","severity":"breaking","affected_versions":">=8.9.0 (builder version, impacts `mypy-boto3-ec2` versions after this point)"},{"fix":"Always check the `mypy-boto3-builder` release notes for changes affecting specific service packages. If a service name changes, you may need to uninstall the old `mypy-boto3-OLDNAME` package and install `mypy-boto3-NEWNAME`.","message":"AWS service names can change or be deprecated (e.g., `sms-voice` was replaced by `pinpoint-sms-voice` for `mypy-boto3-*` packages in builder version `8.11.0`). While `ec2` is stable, be aware that you might need to update the specific `mypy-boto3-*` package if you work with other AWS services whose names evolve.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}