mypy-boto3-mturk Type Annotations
mypy-boto3-mturk provides comprehensive type annotations for the Amazon Mechanical Turk (MTurk) service client within the `boto3` library. It enables static type checking with tools like MyPy, improving code quality and developer experience by offering auto-completion and early error detection. The library is actively maintained, with versions typically synchronized with `boto3` releases and generated by `mypy-boto3-builder`.
Warnings
- breaking Support for Python 3.8 was removed in `mypy-boto3-builder` version 8.12.0. Consequently, `mypy-boto3-mturk` versions generated with this builder (including 1.42.3) and newer require Python 3.9 or higher.
- breaking In `mypy-boto3-builder` 8.9.0, there were breaking changes to TypeDef naming conventions, particularly for packed method arguments and conflicting names (e.g., `CreateDistributionRequestRequestTypeDef` became `CreateDistributionRequestTypeDef`).
- gotcha `mypy-boto3-mturk` provides *only type annotations*. It does not include the `boto3` library itself. You must install `boto3` separately to use the AWS SDK at runtime.
- gotcha When using standalone `mypy-boto3-*` packages or `boto3-stubs-lite`, explicit type annotations for `boto3.client` and `boto3.resource` calls are often required for optimal type inference.
- gotcha PyCharm users might experience slow performance with `Literal` overloads. It is recommended to use the `boto3-stubs-lite` packages or generate custom stubs locally with `mypy-boto3-builder` to mitigate this issue.
- gotcha For `pylint` compatibility, especially when `mypy-boto3-mturk` is not a runtime dependency, use a `TYPE_CHECKING` guard around type imports and assign `object` to types in the `else` block to avoid `undefined variable` errors from `pylint`.
Install
-
pip install boto3 mypy-boto3-mturk
Imports
- MTurkClient
from mypy_boto3_mturk.client import MTurkClient
- MTurkServiceName
from mypy_boto3_mturk.literals import MTurkServiceName
Quickstart
import boto3
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3_mturk.client import MTurkClient
def get_mturk_client() -> MTurkClient:
"""Returns a type-hinted MTurk client."""
# boto3.client returns botocore.client.BaseClient at runtime,
# but mypy will correctly infer MTurkClient type from the annotation.
client: MTurkClient = boto3.client('mturk')
return client
# Example usage:
mturk_client = get_mturk_client()
# Now mturk_client has type hints for MTurk service methods
# For example, mturk_client.list_hits() will have correct type signatures.
# This is a runnable example, but it requires valid AWS credentials
# and an active MTurk environment for actual API calls.
# print(mturk_client.list_hits()) # Uncomment to run actual API call