mypy-boto3-iam Type Stubs
mypy-boto3-iam provides static type annotations for the boto3 IAM service client, resources, and related types. It enables type checking with tools like MyPy, Pyright, and enhances IDE auto-completion for `boto3.client('iam')` calls. Currently at version 1.42.64, it is part of the frequently updated `mypy-boto3-builder` ecosystem, with releases often tied to new boto3 versions.
Warnings
- breaking Support for Python 3.8 was removed in `mypy-boto3-builder` version 8.12.0 and subsequent generated packages, including `mypy-boto3-iam`. The minimum required Python version is now 3.9.
- breaking In `mypy-boto3-builder` version 8.9.0, type definition names for packed method arguments (`RequestRequestTypeDef` became `RequestTypeDef`) and conflicting `Extra` postfixes (`ExtraRequestTypeDef` became `RequestExtraTypeDef`) were changed. This may break existing type annotations.
- deprecated The `sms-voice` service stubs were removed in `mypy-boto3-builder` version 8.11.0 because the service is no longer supported by AWS. Use `pinpoint-sms-voice` stubs instead. While not directly IAM, this indicates a broader trend in the `mypy-boto3` ecosystem.
- gotcha PyCharm users might experience slow performance or high CPU usage due to how PyCharm handles `Literal` overloads. It is recommended to use `boto3-stubs-lite` (e.g., `pip install 'boto3-stubs-lite[iam]'`) or disable PyCharm's type checker and rely on `mypy` or `pyright`.
- gotcha When using standalone packages like `mypy-boto3-iam` or `boto3-stubs-lite`, you might need to explicitly annotate the client, resource, waiter, or paginator variables for full type checking and IDE auto-completion, as automatic type inference for `session.client()` overloads is not always available.
- gotcha When using `mypy-boto3-iam` or other `mypy-boto3` stubs in a production environment where you want to avoid installing the stubs at runtime, wrap your type imports in a `if TYPE_CHECKING:` block. This prevents unnecessary runtime dependencies but requires setting types to `object` outside of type checking for tools like Pylint.
Install
-
pip install mypy-boto3-iam -
pip install 'boto3-stubs[iam]'
Imports
- IAMClient
from mypy_boto3_iam.client import IAMClient
- IAMServiceResource
from mypy_boto3_iam.service_resource import IAMServiceResource
- GetAccountSummaryResponseTypeDef
from mypy_boto3_iam.type_defs import GetAccountSummaryResponseTypeDef
Quickstart
from typing import TYPE_CHECKING
import boto3
if TYPE_CHECKING:
from mypy_boto3_iam.client import IAMClient
from mypy_boto3_iam.type_defs import GetAccountSummaryResponseTypeDef
def get_iam_account_summary() -> 'GetAccountSummaryResponseTypeDef':
# The 'iam' client is type-hinted by mypy-boto3-iam after installation.
client: IAMClient = boto3.client("iam")
summary = client.get_account_summary()
print(f"IAM Account Summary: {summary['SummaryMap']}")
return summary
if __name__ == "__main__":
# Ensure AWS credentials are configured (e.g., via AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN env vars or ~/.aws/credentials)
try:
get_iam_account_summary()
except Exception as e:
print(f"An error occurred: {e}")