Type Annotations for aiobotocore Textract
This library provides static type annotations for the `aiobotocore` Textract service client, enhancing code reliability through static type checking with tools like MyPy and Pyright. It is currently at version 3.4.0 and is generated by `mypy-boto3-builder 8.12.0`, receiving frequent updates in sync with new `aiobotocore` releases and builder improvements.
Common errors
-
ModuleNotFoundError: No module named 'aiobotocore'
cause The `types-aiobotocore-textract` package provides only type annotations; it does not include the actual `aiobotocore` runtime library.fixInstall `aiobotocore`: `pip install aiobotocore`. -
mypy: error: Cannot find implementation or library stub for module 'types_aiobotocore_textract.client'
cause This error occurs when MyPy cannot locate the type stubs, often due to an incorrect installation, an outdated type checker, or issues with PEP 561 package discovery.fixEnsure `types-aiobotocore-textract` is installed, your type checker (MyPy) is up-to-date, and configured to recognize PEP 561 packages. You might also need `pip install 'types-aiobotocore[textract]'` if using the main `types-aiobotocore` package, or explicitly configure `MYPYPATH` if your environment is unusual. -
mypy: error: 'TextractClient' object has no attribute 'analyze_document' (note: perhaps you meant 'analyze_expense')
cause This typically indicates that the type checker is not correctly applying the `TextractClient` type to your client object, or you might be using an `aiobotocore` version that is incompatible with the installed stubs, leading to an incorrect interface being presented.fixEnsure both `aiobotocore` and `types-aiobotocore-textract` are at compatible versions. You can also explicitly type the client after creation, e.g., `client: TextractClient = await session.create_client("textract")`, to help the type checker. Check Textract service methods in the official AWS Boto3/Botocore documentation to confirm the method name.
Warnings
- breaking Python 3.8 support was removed from `mypy-boto3-builder` (which generates these stubs) in version 8.12.0. As `types-aiobotocore-textract 3.4.0` is generated with builder version 8.12.0, it explicitly requires Python 3.9 or newer.
- breaking The `mypy-boto3-builder` (version 8.9.0 and newer) introduced breaking changes to TypeDef naming conventions. This includes shortening names for packed method arguments (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`) and moving `Extra` postfixes. Code explicitly importing and using these TypeDefs may break.
- gotcha These `types-aiobotocore-*` packages provide only type annotations (stubs). They do not replace or include the actual `aiobotocore` runtime library. You must install `aiobotocore` separately for your code to function at runtime.
- gotcha The `mypy-boto3-builder` ecosystem (which includes `types-aiobotocore`) migrated to PEP 561-compliant packages in version 8.12.0. While this generally improves compatibility and discovery, some older tools or custom configurations that relied on previous stub discovery mechanisms might require adjustments.
Install
-
pip install types-aiobotocore-textract -
pip install 'types-aiobotocore[textract]'
Imports
- TextractClient
from types_aiobotocore_textract.client import TextractClient
- AdapterOverviewTypeDef
from types_aiobotocore_textract.type_defs import AdapterOverviewTypeDef
- AdapterVersionStatusType
from types_aiobotocore_textract.literals import AdapterVersionStatusType
- get_session
from aiobotocore.session import get_session
Quickstart
import asyncio
from aiobotocore.session import get_session
from types_aiobotocore_textract.client import TextractClient
async def analyze_document_typed(s3_bucket: str, s3_key: str):
session = get_session()
async with session.create_client("textract") as client: # type: TextractClient
print(f"Client type: {type(client)}")
try:
response = await client.analyze_document(
Document={
'S3Object': {
'Bucket': s3_bucket,
'Name': s3_key
}
},
FeatureTypes=['FORMS', 'TABLES']
)
print("Document analysis successful.")
# Example of accessing a typed response
document_metadata = response.get('DocumentMetadata')
if document_metadata:
print(f"Pages: {document_metadata.get('Pages')}")
except client.ValidationException as e:
print(f"Validation error: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")
if __name__ == "__main__":
# Replace with your S3 bucket and key for a document (e.g., PDF, PNG, JPG)
# This example requires an actual S3 object for Textract to process.
# For local testing without a real S3 object, Textract might raise an InvalidS3ObjectException or similar.
dummy_s3_bucket = "your-s3-bucket"
dummy_s3_key = "path/to/your/document.pdf"
# You can use a dummy value if you only want to test type checking without actual execution
# or ensure a real, accessible S3 object is configured for Textract.
asyncio.run(analyze_document_typed(dummy_s3_bucket, dummy_s3_key))