mypy-boto3-guardduty Type Annotations
Type annotations for the `boto3` GuardDuty service, generated with `mypy-boto3-builder`. It provides comprehensive type hints for clients, paginators, literals, and type definitions, ensuring compatibility with static type checkers like mypy and enhancing IDE experiences in VSCode, PyCharm, and others. The library is actively maintained and frequently updated to synchronize with `boto3` and `mypy-boto3-builder` releases, currently at version 1.42.84.
Warnings
- breaking Direct support for Python 3.8 was removed starting with `mypy-boto3-builder` version 8.12.0 (which generated `mypy-boto3-guardduty` 1.42.84). The library now officially requires Python >=3.9.
- breaking Prior to `mypy-boto3-builder` 8.9.0, some generated `TypeDef` names were longer (e.g., `CreateDistributionRequestRequestTypeDef`). Version 8.9.0 introduced changes to shorten these names where possible, which can break existing code relying on the old `TypeDef` names.
- gotcha For optimal IDE support (e.g., in VSCode) and accurate autocompletion for `boto3.client` and `session.client` calls, it is recommended to explicitly type-annotate the client object, as some IDEs may not fully support function overloads for dynamic clients.
- gotcha When using `if TYPE_CHECKING:` blocks to conditionally import type stubs, linters like Pylint may report `undefined-variable` errors for the type-hinted objects in the runtime code path. This happens because Pylint doesn't always correctly parse the `TYPE_CHECKING` guard.
Install
-
pip install mypy-boto3-guardduty boto3
Imports
- GuardDutyClient
from mypy_boto3_guardduty.client import GuardDutyClient
- AcceptAdministratorInvitationRequestTypeDef
from mypy_boto3_guardduty.type_defs import AcceptAdministratorInvitationRequestTypeDef
- AdminStatusType
from mypy_boto3_guardduty.literals import AdminStatusType
Quickstart
import boto3
from typing import TYPE_CHECKING
import os
if TYPE_CHECKING:
from mypy_boto3_guardduty.client import GuardDutyClient
from mypy_boto3_guardduty.type_defs import ListDetectorsResponseTypeDef
def get_guardduty_detectors() -> None:
# Explicit type annotation for the client for better IDE support
client: GuardDutyClient = boto3.client(
"guardduty",
region_name=os.environ.get('AWS_REGION', 'us-east-1'),
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')
)
print("Listing GuardDuty detectors...")
try:
response: ListDetectorsResponseTypeDef = client.list_detectors()
detector_ids = response.get('DetectorIds', [])
if detector_ids:
print(f"Found {len(detector_ids)} detectors: {', '.join(detector_ids)}")
else:
print("No GuardDuty detectors found.")
except client.exceptions.ClientError as e:
print(f"Error listing detectors: {e}")
if __name__ == "__main__":
get_guardduty_detectors()