mypy-boto3-shield Type Annotations
mypy-boto3-shield provides type annotations for the AWS boto3 Shield service (version 1.42.3), enabling static type checking and improved IDE support. It is automatically generated by the `mypy-boto3-builder` tool, which frequently updates to match new `boto3` releases and features.
Warnings
- breaking As of mypy-boto3-builder version 8.12.0, support for Python 3.8 has been removed across all generated `mypy-boto3` packages, including `mypy-boto3-shield`. Users on Python 3.8 will need to upgrade to Python 3.9 or newer.
- breaking mypy-boto3-builder version 8.9.0 introduced breaking changes to `TypeDef` naming conventions, such as shortening `CreateDistributionRequestRequestTypeDef` to `CreateDistributionRequestTypeDef` and adjusting postfixes for conflicting names. This may require updating type annotation references in your codebase if you're upgrading from older versions.
- deprecated The original `mypy-boto3` package is considered legacy. It is recommended to use `boto3-stubs` (the umbrella package) or install individual service stubs like `mypy-boto3-shield` directly for better compatibility and maintenance.
- gotcha PyCharm may experience slow performance with `Literal` overloads from `mypy-boto3` stubs (issue PY-40997). If this occurs, it's recommended to use `boto3-stubs-lite` (if available for the service) or to disable PyCharm's internal type checker and rely on external tools like `mypy` or `pyright`.
- gotcha While `mypy-boto3` generally provides auto-discovery of types for `boto3.client` calls, explicit type annotations for the client object (e.g., `client: ShieldClient = boto3.client('shield')`) are often necessary for the best autocompletion and type-checking experience in IDEs like VSCode and PyCharm, especially for methods like `get_paginator` or `get_waiter`.
Install
-
pip install mypy-boto3-shield -
pip install boto3-stubs[shield]
Imports
- ShieldClient
from mypy_boto3_shield import ShieldClient
- ListAttacksPaginator
from mypy_boto3_shield.paginator import ListAttacksPaginator
- ResponseActionOutputTypeDef
from mypy_boto3_shield.type_defs import ResponseActionOutputTypeDef
Quickstart
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_shield import ShieldClient
from mypy_boto3_shield.type_defs import ListAttacksResponseTypeDef
# Instantiate a Shield client with type hints
client: "ShieldClient" = boto3.client("shield")
# Example of using a client method with type checking
try:
response: "ListAttacksResponseTypeDef" = client.list_attacks()
print(f"Found {len(response.get('Attacks', []))} Shield attacks.")
except Exception as e:
print(f"Error listing attacks: {e}")
# Example of a TypedDict for input (if applicable, or output)
def process_response_action(action: "ResponseActionOutputTypeDef") -> None:
if 'Block' in action:
print(f"Action is a block: {action['Block']}")
elif 'Count' in action:
print(f"Action is a count: {action['Count']}")
# For demonstration, typically this comes from an API call
example_action: "ResponseActionOutputTypeDef" = {"Block": {}}
process_response_action(example_action)