{"id":4296,"library":"types-boto3-ec2","title":"Type Annotations for Boto3 EC2","description":"types-boto3-ec2 provides comprehensive type annotations for the Amazon EC2 service client in boto3, enabling static type checking with tools like mypy, pyright, and enhanced IDE support. It is part of the mypy-boto3-builder ecosystem, which generates separate stub packages for each AWS service. The current version is 1.42.85, with frequent updates synchronized with boto3 releases.","status":"active","version":"1.42.85","language":"en","source_language":"en","source_url":"https://github.com/youtype/mypy_boto3_builder","tags":["AWS","boto3","type hints","mypy","EC2","static analysis"],"install":[{"cmd":"pip install types-boto3-ec2","lang":"bash","label":"Install service-specific stubs"},{"cmd":"pip install 'boto3-stubs[ec2]'","lang":"bash","label":"Install as an extra from boto3-stubs"}],"dependencies":[{"reason":"This package provides type stubs for the boto3 library, which must be installed to be used at runtime.","package":"boto3","optional":false}],"imports":[{"note":"While explicit imports are not strictly necessary for type checking, they can be used for explicit type hinting of client objects.","symbol":"EC2Client","correct":"from types_boto3_ec2 import EC2Client"},{"note":"Used for explicit type hinting of EC2 service resource objects.","symbol":"EC2ServiceResource","correct":"from types_boto3_ec2 import EC2ServiceResource"}],"quickstart":{"code":"import boto3\nfrom typing import TYPE_CHECKING\n\nif TYPE_CHECKING:\n    from types_boto3_ec2 import EC2Client\n\n# Boto3 client will be automatically typed by mypy-boto3-ec2 if installed\nec2_client: EC2Client = boto3.client('ec2')\n\n# Example usage with type-hinted client\nresponse = ec2_client.describe_instances(\n    Filters=[\n        {\n            'Name': 'instance-state-name',\n            'Values': ['running']\n        },\n    ]\n)\n\nfor reservation in response['Reservations']:\n    for instance in reservation['Instances']:\n        print(f\"Instance ID: {instance['InstanceId']}, Type: {instance['InstanceType']}\")","lang":"python","description":"This quickstart demonstrates how types-boto3-ec2 provides type hints for your boto3 EC2 client, enabling static analysis and IDE autocompletion. The 'if TYPE_CHECKING:' block is optional but shows how to explicitly reference the stub types without creating a runtime dependency. The type checker will automatically infer types for `boto3.client('ec2')`."},"warnings":[{"fix":"Upgrade your Python environment to 3.9 or higher.","message":"Support for Python 3.8 was removed in mypy-boto3-builder 8.12.0 (and consequently for types-boto3-ec2 versions generated by it). Python 3.9+ is now required.","severity":"breaking","affected_versions":"mypy-boto3-builder >= 8.12.0"},{"fix":"Install the specific stub package for each boto3 service you use (e.g., `pip install types-boto3-s3 types-boto3-lambda`).","message":"This is a service-specific stub package. To get type annotations for other AWS services (e.g., S3, Lambda), you must install their respective `types-boto3-<service>` packages (e.g., `types-boto3-s3`). Installing just `types-boto3` will install a 'full' package but it's often more efficient to install specific stubs.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Update your code to use the new TypeDef names if you were explicitly referencing them. Check the generated stub files or documentation for the correct new names.","message":"In mypy-boto3-builder 8.9.0, there were breaking changes to TypeDef naming conventions, where packed method arguments used shorter names (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`) and conflicting `Extra` postfixes moved to the end (e.g., `CreateDistributionExtraRequestTypeDef` became `CreateDistributionRequestExtraTypeDef`).","severity":"breaking","affected_versions":"mypy-boto3-builder >= 8.9.0"},{"fix":"Ensure your type checker (e.g., mypy, pyright) is up-to-date and configured correctly to discover PEP 561 stub packages.","message":"The `mypy-boto3` ecosystem switched to PEP 561 compliant packages in version 8.12.0. This primarily affects how type checkers locate and interpret the installed stub files.","severity":"gotcha","affected_versions":"mypy-boto3-builder >= 8.12.0"},{"fix":"Wrap explicit type imports in `if TYPE_CHECKING:` blocks and provide fallback `object` assignments in the `else` block for runtime safety and Pylint compatibility, as shown in the quickstart example.","message":"Pylint might complain about undefined variables when using type hints with `types-boto3-ec2` outside of a `TYPE_CHECKING` block. It is recommended to use an `if TYPE_CHECKING:` guard and set types to `object` in the `else` branch to avoid runtime dependencies and Pylint warnings.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}