Type Annotations for boto3 LocationService
mypy-boto3-location provides type annotations for the boto3 LocationService, enhancing developer experience with type checking, autocomplete, and static analysis in IDEs like VSCode and PyCharm. It keeps pace with boto3 releases, automatically generating stubs with `mypy-boto3-builder`. The current version is 1.42.3.
Warnings
- breaking Support for Python 3.8 was removed in `mypy-boto3-builder` version 8.12.0. Users on Python 3.8 or older should upgrade their Python version.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` version 8.9.0. Some TypeDefs for packed method arguments use shorter names, and conflicting `Extra` postfixes were moved. This could break code explicitly importing or referencing these TypeDefs.
- gotcha PyCharm users might experience slow performance or high CPU usage due to `Literal` overloads. It is recommended to use `boto3-stubs-lite` or disable PyCharm's internal type checker and rely on `mypy` or `pyright` instead.
- gotcha For Pylint compatibility, if `mypy-boto3-location` is a `TYPE_CHECKING` dependency, Pylint might complain about undefined variables. A common fix is to set types to `object` in non-`TYPE_CHECKING` mode.
Install
-
pip install mypy-boto3-location -
pip install 'boto3-stubs[location]' # Alternative using the bundled stubs
Imports
- LocationServiceClient
from mypy_boto3_location.client import LocationServiceClient
- LocationService
import boto3 from typing import TYPE_CHECKING if TYPE_CHECKING: from mypy_boto3_location.client import LocationServiceClient client: LocationServiceClient = boto3.client("location")
Quickstart
import os
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_location.client import LocationServiceClient
from mypy_boto3_location.type_defs import ListGeofencesResponseTypeDef
def get_location_client() -> LocationServiceClient:
"""Initializes and returns a typed LocationService client."""
# AWS credentials will be picked up from environment variables (e.g., AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
# or ~/.aws/credentials. REGION_NAME is important for LocationService.
region = os.environ.get('AWS_REGION', 'us-east-1')
return boto3.client('location', region_name=region)
def list_all_geofences():
"""Lists all geofences using the LocationService client."""
client = get_location_client()
try:
response: ListGeofencesResponseTypeDef = client.list_geofences(CollectionName='my-geofence-collection')
print(f"Geofences found: {len(response['Entries'])}")
for entry in response['Entries']:
print(f" - Geofence ID: {entry['GeofenceId']}")
except client.exceptions.ResourceNotFoundException:
print("Geofence collection 'my-geofence-collection' not found or empty.")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == '__main__':
# This quickstart requires an existing LocationService geofence collection.
# Replace 'my-geofence-collection' with an actual collection name in your AWS account.
list_all_geofences()