Type annotations for boto3 IoT service
mypy-boto3-iot provides comprehensive type annotations for the AWS boto3 IoT service, enhancing static analysis, autocompletion, and error detection in Python projects. It is currently at version 1.42.14, generated with mypy-boto3-builder 8.12.0, and follows a release cadence aligned with boto3 updates.
Warnings
- breaking Support for Python 3.8 was removed starting with `mypy-boto3-builder` version 8.12.0 (which generates `mypy-boto3-iot` 1.42.14). Projects using Python 3.8 or older will need to upgrade to Python 3.9+.
- breaking The `mypy-boto3-builder` (and consequently `mypy-boto3-iot`) migrated to PEP 561 packages in version 8.12.0. This might change how type checkers discover packages, potentially requiring updates to `mypy` configurations or build systems that explicitly manage stubs.
- breaking Version 8.9.0 of `mypy-boto3-builder` introduced breaking changes to `TypeDef` naming conventions, specifically for packed method arguments (shorter names) and conflicting `Extra` postfixes (moved to the end). If you rely on specific `TypeDef` names in your type hints, they may have changed.
- gotcha While `boto3-stubs` often provides implicit type annotations, explicit type annotations for client, resource, waiter, and paginator calls (e.g., `client: IoTClient = boto3.client('iot')`) are highly recommended for optimal IDE autocompletion and type checking, especially with tools like PyCharm or when using `boto3-stubs-lite`.
- gotcha When using `typing.TYPE_CHECKING` guards to avoid runtime dependency on `mypy-boto3-iot`, Pylint may complain about undefined variables. A common workaround is to assign `object` to the type-hinted variables outside the `TYPE_CHECKING` block.
Install
-
pip install mypy-boto3-iot boto3 -
pip install 'boto3-stubs[iot]' boto3
Imports
- IoTClient
from mypy_boto3_iot import IoTClient
- ListThingsPaginator
from mypy_boto3_iot.paginator import ListThingsPaginator
- AbortCriteriaTypeDef
from mypy_boto3_iot.type_defs import AbortCriteriaTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_iot import IoTClient
from mypy_boto3_iot.type_defs import ListThingsResponseTypeDef
def list_iot_things(region_name: str = 'us-east-1') -> ListThingsResponseTypeDef:
# Initialize the boto3 client (type-checked by mypy-boto3-iot)
client: IoTClient = boto3.client('iot', region_name=region_name)
# Example operation: List IoT Things
response = client.list_things(maxResults=10)
print(f"Found {len(response.get('things', []))} IoT things.")
for thing in response.get('things', []):
print(f" - {thing['thingName']}")
return response
if __name__ == '__main__':
# This assumes AWS credentials are set up (e.g., via environment variables or ~/.aws/credentials)
try:
list_iot_things()
except Exception as e:
print(f"Error listing IoT things: {e}")