Type Annotations for boto3 Resource Explorer 2
mypy-boto3-resource-explorer-2 provides type annotations for the `boto3` AWS SDK's Resource Explorer 2 service. It enhances development experience by enabling static type checking with tools like MyPy, catching potential errors at development time. The library is part of the `mypy-boto3` project, which generates stubs for all `boto3` services, released frequently in sync with `boto3` and `botocore` updates.
Warnings
- breaking Support for Python 3.8 was removed in `mypy-boto3-builder` version 8.12.0. Packages generated with this builder version (including `mypy-boto3-resource-explorer-2` 1.42.30) no longer support Python 3.8.
- breaking Certain `TypeDef` names for method arguments were shortened or restructured in `mypy-boto3-builder` version 8.9.0. For example, `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`.
- gotcha Installing `mypy-boto3-resource-explorer-2` only provides type annotations for static analysis tools like MyPy. It does NOT install `boto3` itself. You must install `boto3` separately to run your code.
- gotcha When obtaining a client from `boto3.client()`, you must pass the correct service name as a string (e.g., `'resource-explorer-2'`). The type hints are applied *after* the client is created, not by modifying how `boto3.client()` works.
Install
-
pip install mypy-boto3-resource-explorer-2 boto3 -
pip install mypy-boto3-resource-explorer-2
Imports
- ResourceExplorer2Client
from mypy_boto3_resource_explorer_2.client import ResourceExplorer2Client
- ResourceExplorer2ServiceResource
from mypy_boto3_resource_explorer_2.service_resource import ResourceExplorer2ServiceResource
- SearchInputRequestTypeDef
from mypy_boto3_resource_explorer_2.type_defs import SearchInputRequestTypeDef
Quickstart
import boto3
from mypy_boto3_resource_explorer_2.client import ResourceExplorer2Client
from mypy_boto3_resource_explorer_2.type_defs import SearchInputRequestTypeDef
import os
# Ensure boto3 is configured, e.g., via environment variables or ~/.aws/credentials
# For testing without actual credentials, you might mock boto3 or use a localstack setup.
def get_explorer_client() -> ResourceExplorer2Client:
"""Gets a type-hinted AWS Resource Explorer 2 client."""
# boto3.client expects a string for the service name
client: ResourceExplorer2Client = boto3.client("resource-explorer-2")
return client
def search_resources(client: ResourceExplorer2Client, query: str):
"""Performs a search using the Resource Explorer 2 client."""
print(f"Searching for resources with query: '{query}'")
search_params: SearchInputRequestTypeDef = {
"QueryString": query,
"MaxResults": 5
}
response = client.search(**search_params)
print("Found Resources:")
for resource in response.get("Resources", []):
print(f" - Name: {resource.get('ResourceArn')}, Type: {resource.get('ResourceType')}")
return response
if __name__ == "__main__":
# Example usage
explorer_client = get_explorer_client()
# An example query (replace with a real query for your AWS account)
# For a real run, ensure you have resources indexed by Resource Explorer 2.
test_query = "region:us-east-1 and resource.type:ec2:instance"
# This part will fail if AWS credentials are not configured or if Resource Explorer 2
# is not set up in your AWS account/region.
# For local testing, consider using localstack with boto3's endpoint_url.
try:
search_result = search_resources(explorer_client, test_query)
print(f"Search result has a NextToken: {search_result.get('NextToken') is not None}")
except Exception as e:
print(f"Error during search (this might be expected if AWS config/resources are not set up): {e}")