Type Annotations for boto3 LocationService

1.42.3 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This example demonstrates how to initialize a type-hinted LocationService client and use it to list geofences. It includes an explicit type annotation for the client and the response type, enabling full static analysis and autocomplete.

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()

view raw JSON →