If you're building an AWS-based program in Python, you'll almost certainly be indebted to the AWS SDK called boto3.
This boto3 has a lot of functions that can hit all AWS services, but I do not hear code completion because there is no type. Since the service name is received as a character string such as boto3.client (" s3 ")
, the type definition is BaseClient
type, not the client type for each service. This is quite difficult because I usually develop it in an environment where complements are listened to hard.
I wondered if something could be done and searched for it, and found a package called ** boto3-stubs **. In this, the type of the client resource for each service such as s3 and ec2 is defined, so if you give this type, ...
You will be able to complement it like this! This is good! !!
pip install boto3-stubs[essential]
The version is designed to be combined with boto3, so even if you are using a specific boto3, you can use it without problems if you specify the same version number. The packages are updated automatically, and it seems that boto3-stubs were also released on the day boto3 was updated.
The [essential]
specification above will contain the classes "ec2, s3, rds, lambda, sqs, dynamo, cloudformation". This alone requires about 7MB, so it seems that it does not suddenly fit all. If you need anything else, you can specify it in the form of [essential, ecs]
.
import boto3
from mypy_boto3_s3 import S3Client, S3ServiceResource # from boto3-stubs
s3: S3Client = boto3.client("s3")
s3_r: S3ServiceResource = boto3.resource("s3")
Now you can hear code completion on the s3
, s3_r
variables. I'll specify the type, so any editor that can do type-based completion, such as VSCode, can do it the same way.
--If you use boto3-stubs with mypy, there seems to be a mechanism that allows you to check the type without writing the type specification by hand. ――It seems that there is a movement to prepare stubs even in the boto3 project. In the future, there may be movement around here as well.
Recommended Posts