
Image courtesy of boto3-type-annotations, Allie Fitter
Right now, few people write large Python projects without type annotation. Itβs simple and allows you to catch a bunch of errors even at the stage of writing code, and it works very smartly. But itβs worth adding boto3 to the dependency , and mypy begins to be full of messages that type annotations boto3
do not exist in nature.
, boto3 botostubs. , mypy
.
boto3-type-annotations, , mypy
.
boto3
, ?
mypy
, boto3 1.11.9
EC2, DynamoDB S3. Python 3.6.9 . , , boto3-stubs 1.11.9.x .
pip install boto3-stubs[s3,ec2,dynamodb]==1.11.9.0
python -m mypy_boto3
β¦ ! mypy
, , , boto3
, .
- !
, .
VSCode , . boto3.client
, boto3_session.client
,boto3.resource
, boto3_session.resource
, client.get_waiter
client.get_paginator
.
PyCharm - , 15 , .
import boto3
from mypy_boto3 import dynamodb
client: dynamodb.DynamoDBClient = boto3.client("dynamodb")
client.query("my_table")
import boto3
from mypy_boto3 import s3
client: s3.S3Client = boto3.client("s3")
client.create_bucket(Bucket="bucket")
client.get_object(Bucket="bucket")
client.get_object(Bucket="bucket", Key=None)
resource: s3.S3ServiceResource = boto3.Session(region_name="us-west-1").resource("s3")
bucket = resource.Bucket("bucket")
bucket.upload_file(Filename="my.txt", key="my-txt")
waiter: s3.BucketExistsWaiter = client.get_waiter("bucket_exists")
paginator: s3.ListMultipartUploadsPaginator = client.get_paginator(
"list_multipart_uploads"
)
mypy-boto3-builder - boto3
. boto3
, .
json
- botocore , boto3
.
, , .
post-install , whl-. ?
Since boto3
many methods and classes in the same file are called the same, for the correct operation of annotations, the module imports itself. This does not work in Python 3.6.5 and does not look very good. What are the alternatives?
And finally, if everything is fine with C #, you can add literal support to overloaded functions for the Python Language Server so that everything works out of the box in VSCode too. I could not and gave up.
Thanks to all!