Code completion and type checking for boto3

image


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 boto3do 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 .


#   boto3 ,     
pip install boto3-stubs[s3,ec2,dynamodb]==1.11.9.0

#     pip, pipfile  poetry,
#    whl-,     
#   
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")

# IDE        
client.query("my_table")


import boto3

from mypy_boto3 import s3

# PyCharm  mypy      ,   
client: s3.S3Client = boto3.client("s3")

# IDE   ,  ,  --
client.create_bucket(Bucket="bucket")

# ,   mypy     

# (mypy) error: Missing positional argument "Key" in call to "get_object" of "S3Client"
client.get_object(Bucket="bucket")

# (mypy) error: Argument "Key" to "get_object" of "S3Client" has incompatible type "None"; expected "str"
client.get_object(Bucket="bucket", Key=None)

resource: s3.S3ServiceResource = boto3.Session(region_name="us-west-1").resource("s3")

#   -     
bucket = resource.Bucket("bucket")

# (mypy) error: Unexpected keyword argument "key" for "upload_file" of "Bucket"; did you mean "Key"?
bucket.upload_file(Filename="my.txt", key="my-txt")

# waiter'   paginator'  
#         
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 boto3many 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!

Source: https://habr.com/ru/post/undefined/


All Articles