mypy-boto3-devicefarm
mypy-boto3-devicefarm provides comprehensive type annotations for the boto3 DeviceFarm service, enabling static type checking with tools like mypy. These type stubs enhance IDE autocompletion, improve code readability, and help catch potential runtime errors before deployment. It is currently at version 1.42.3, with releases closely mirroring `boto3` updates via the `mypy-boto3-builder`.
Warnings
- breaking Python 3.8 support was removed in `mypy-boto3-builder` version 8.12.0, which generates these type stubs. Consequently, `mypy-boto3-devicefarm` versions 1.42.x and newer require Python 3.9 or higher.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` 8.9.0. TypeDefs for packed method arguments now use shorter names (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`), and conflicting `Extra` postfixes are moved to the end (e.g., `CreateDistributionExtraRequestTypeDef` to `CreateDistributionRequestExtraTypeDef`). Code relying on older, explicit TypeDef names may break.
- gotcha `mypy-boto3-devicefarm` provides only type annotations for static analysis; it does not include the `boto3` runtime itself. For your code to run, `boto3` must be installed separately.
- gotcha For optimal type checking and IDE experience, the version of `mypy-boto3-devicefarm` should ideally align with your installed `boto3` version. Mismatched versions can lead to incorrect or incomplete type hints.
- gotcha While many IDEs and type checkers can infer types, explicitly annotating `boto3.client` and `boto3.session.client` calls with the `DeviceFarmClient` type (e.g., `client: DeviceFarmClient = session.client('devicefarm')`) is recommended for the most robust type checking and best autocompletion, especially in environments like VSCode.
Install
-
pip install mypy-boto3-devicefarm boto3
Imports
- DeviceFarmClient
from mypy_boto3_devicefarm.client import DeviceFarmClient
- DeviceFarmServiceName
from mypy_boto3_devicefarm.literals import DeviceFarmServiceName
- ListDevicesResponseTypeDef
from mypy_boto3_devicefarm.type_defs import ListDevicesResponseTypeDef
Quickstart
import os
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_devicefarm.client import DeviceFarmClient
from mypy_boto3_devicefarm.type_defs import ListDevicesResponseTypeDef
def get_devicefarm_client() -> 'DeviceFarmClient':
"""Returns a type-hinted boto3 DeviceFarm client."""
# Ensure AWS credentials are available via environment variables or other boto3 config
# e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION
session = boto3.Session(
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID', 'DUMMY_KEY'),
aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY', 'DUMMY_SECRET'),
region_name=os.environ.get('AWS_REGION', 'us-west-2') # Device Farm is region-specific
)
client: DeviceFarmClient = session.client('devicefarm')
return client
def list_devicefarm_devices():
"""Lists available Device Farm devices with type hints."""
client = get_devicefarm_client()
print("Attempting to list Device Farm devices...")
try:
response: ListDevicesResponseTypeDef = client.list_devices(filters=[{'attribute': 'PLATFORM', 'operator': 'EQUALS', 'values': ['ANDROID']}])
devices = response.get('devices', [])
if devices:
print(f"Found {len(devices)} Android devices:")
for device in devices:
print(f" - {device.get('name')} (ARN: {device.get('arn')})")
else:
print("No Android devices found.")
except client.exceptions.ServiceException as e:
print(f"AWS Service Error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == '__main__':
list_devicefarm_devices()