Builder for mypy-boto3.
Project description
mypy_boto3
mypy
-friendly type annotations for boto3
.
Based on boto3_type_annotations by @alliefitter.
Installation
# install `boto3` type annotations
# for ec2, s3, rds, lambda, sqs, dynamo and cloudformation
pip install boto3-stubs[essential]
# install annotations for other services
pip install boto3-stubs[acm, apigateway]
# or install annotations for all services
# WARNING: this requires ~170 MB of space
pip install boto3-stubs[all]
Usage
- Install mypy and optionally enable it in your IDE
- Install boto3
- Now imports from
boto3
andboto3.session
have correct types and code autocomplete
import boto3
client = boto3.client("s3")
# IDE autocomplete suggests function name and arguments here
client.create_bucket(Bucket="bucket")
# (mypy) error: Missing positional argument "Key" in call to "get_object" of "Client"
client.get_object(Bucket="bucket")
# (mypy) error: Argument "Key" to "get_object" of "Client" has incompatible type "None"; expected "str"
client.get_object(Bucket="bucket", Key=None)
resource = boto3.Session(region_name="us-west-1").resource("s3")
# IDE autocomplete suggests function name and arguments here
bucket = resource.Bucket("bucket")
# (mypy) error: Unexpected keyword argument "key" for "upload_file" of "Bucket"
bucket.upload_file(Filename="my.txt", key="my-txt")
Setup your IDE
VSCode
- Install Official Python extension
- Install mypy
- Activate
mypy
checking in settings:"python.linting.mypyEnabled": true
- Install
boto3-stubs
withboto3
services you use - Use explicit type annotations because function overload is not fully supported yet
PyCharm
- Install mypy plugin
- Install mypy
- Set path to
mypy
inmypy plugin
settings - Install
boto3-stubs
withboto3
services you use - Use explicit type annotations because function overload is not fully supported yet
Official mypy
plugin does not work for some reason at least for me. If you know
how to setup it correctly, please let me know.
Do not install all services, it slows down PyCharm
. Install only the ones you use.
Other IDEs
Please help me to extend this section.
Explicit type annotations
mypy
correctly resolves types from boto3-stubs
, but auto-complete in your IDE
probably does not fully support overloaded functions, so methods and arguments
auto-complete will not be very useful.
To help IDE to resolve types correctly, you can set types explicitly.
import boto3
from mypy_boto3.ec2 import Client, ServiceResource
from mypy_boto3.ec2.waiters import BundleTaskCompleteWaiter
from mypy_boto3.ec2.paginators import DescribeVolumesPaginator
session = boto3.session.Session(region_name="us-west-1")
ec2_client: Client = boto3.client("ec2", region_name="us-west-1")
ec2_resource: ServiceResource = session.resource("ec2")
bundle_task_complete_waiter: BundleTaskCompleteWaiter = ec2_client.get_waiter("bundle_task_complete")
describe_volumes_paginator: DescribeVolumesPaginator = ec2_client.get_paginator("describe_volumes")
# ec2_client, ec2_resource, bundle_task_complete_waiter and describe_volumes_paginator
# now have correct type so IDE automoplete for methods, arguments and return types
# works as expected
Pylint compatibility
It is totally safe to use TYPE_CHECKING
flag in order to avoid boto3-stubs
dependency in production.
However, there is an issue in pylint
that it complains about undefined
variables. To fix it, set all types to Any
in non-TYPE_CHECKING
mode.
import boto3
from typing import Any, TYPE_CHECKING
if TYPE_CHECKING:
from mypy_boto3.ec2 import Client, ServiceResource
from mypy_boto3.ec2.waiters import BundleTaskCompleteWaiter
from mypy_boto3.ec2.paginators import DescribeVolumesPaginator
else:
Client = Any
ServiceResource = Any
BundleTaskCompleteWaiter = Any
DescribeVolumesPaginator = Any
...
How to build
Locally
mypy-boto3
is built for the latest version of boto3
. If you need type annotations for another
version of boto3
, you can use mypy-boto3-builder
.
# Install preferred version of `boto3`
pip install boto3==1.10.18 botocore==1.13.18
# Install `mypy-boto3-builder`
pip install mypy-boto3-builder[black]
# Set output path to any directory
OUTPUT_PATH=`pwd`/output
# Build all packages
# You can specify required services explicitly like
# mypy_boto3_builder ${OUTPUT_PATH} -s ec2 s3
mypy_boto3_builder ${OUTPUT_PATH}
# Install custom `mypy-boto3` service packages
PACKAGES=${OUTPUT_PATH}/mypy_boto3_*
for PACKAGE in $PACKAGES
do
cd ${PACKAGE}
python setup.py install
done
# Install custom `mypy-boto3` and `boto3-stubs` packages
cd ${OUTPUT_PATH}/master_package
python setup.py install
cd ${OUTPUT_PATH}/boto3_stubs_package
python setup.py install
With Docker image
- Install Docker
- Pull latest
mypy_boto3_builder
version and tag it
docker pull docker.pkg.github.com/vemel/mypy_boto3/mypy_boto3_builder:latest
docker tag docker.pkg.github.com/vemel/mypy_boto3/mypy_boto3_builder:latest mypy_boto3_builder
- Generate stubs in
output
directory
mkdir output
# generate stubs for all services
docker run -v `pwd`/output:/output -ti mypy_boto3_builder
# generate stubs for s3 service
docker run -v `pwd`/output:/output -ti mypy_boto3_builder -s s3
# generate stubs for a specific boto3 version
docker run -e BOTO3_VERSION=1.10.18 BOTOCORE_VERSION=1.13.18 -v `pwd`/output:/output -ti mypy_boto3_builder
- Install packages from
output
directory as described above
Differences from boto3-type-annotations
mypy
compatibility- Fully type annotated
- No need to set types explicitly (depends on your IDE)
- Generated types for return values and arguments
- Added
ServiceResource
sub-collections - Support service-specific sub-modules
- Modules documentation
- Type annotations for return structures
- Correct annotations for
client.get_waiter
andclient.get_paginator
- Helper functions for IDEs with no
overload
support (Hi, VSCode!)
Thank you
- Guys behind boto3-type-annotations, this package is based on top of their work
- black developers for awesome formatting tool
- mypy for doing all dirty work for us
Sub-modules
Examples
You can install any sub-modules using pip
# pip install boto3-stubs[<submodule_name>, ...]
# install ec2, s3 and sqs type annotations
pip install boto3-stubs[s3, ec2, sqs]
# install type annotations for all boto3 services
pip install boto3-stubs[all]
List of all sub-modules
all
- Type annotations for allboto3
services.essential
- Type annotations forec2
,s3
,rds
,lambda
,sqs
,dynamodb
andcloudformation
services.acm
- Type annotations forboto3
acm service.acm-pca
- Type annotations forboto3
acm-pca service.alexaforbusiness
- Type annotations forboto3
alexaforbusiness service.amplify
- Type annotations forboto3
amplify service.apigateway
- Type annotations forboto3
apigateway service.apigatewaymanagementapi
- Type annotations forboto3
apigatewaymanagementapi service.apigatewayv2
- Type annotations forboto3
apigatewayv2 service.application-autoscaling
- Type annotations forboto3
application-autoscaling service.application-insights
- Type annotations forboto3
application-insights service.appmesh
- Type annotations forboto3
appmesh service.appstream
- Type annotations forboto3
appstream service.appsync
- Type annotations forboto3
appsync service.athena
- Type annotations forboto3
athena service.autoscaling
- Type annotations forboto3
autoscaling service.autoscaling-plans
- Type annotations forboto3
autoscaling-plans service.backup
- Type annotations forboto3
backup service.batch
- Type annotations forboto3
batch service.budgets
- Type annotations forboto3
budgets service.ce
- Type annotations forboto3
ce service.chime
- Type annotations forboto3
chime service.cloud9
- Type annotations forboto3
cloud9 service.clouddirectory
- Type annotations forboto3
clouddirectory service.cloudformation
- Type annotations forboto3
cloudformation service.cloudfront
- Type annotations forboto3
cloudfront service.cloudhsm
- Type annotations forboto3
cloudhsm service.cloudhsmv2
- Type annotations forboto3
cloudhsmv2 service.cloudsearch
- Type annotations forboto3
cloudsearch service.cloudsearchdomain
- Type annotations forboto3
cloudsearchdomain service.cloudtrail
- Type annotations forboto3
cloudtrail service.cloudwatch
- Type annotations forboto3
cloudwatch service.codebuild
- Type annotations forboto3
codebuild service.codecommit
- Type annotations forboto3
codecommit service.codedeploy
- Type annotations forboto3
codedeploy service.codepipeline
- Type annotations forboto3
codepipeline service.codestar
- Type annotations forboto3
codestar service.codestar-notifications
- Type annotations forboto3
codestar-notifications service.cognito-identity
- Type annotations forboto3
cognito-identity service.cognito-idp
- Type annotations forboto3
cognito-idp service.cognito-sync
- Type annotations forboto3
cognito-sync service.comprehend
- Type annotations forboto3
comprehend service.comprehendmedical
- Type annotations forboto3
comprehendmedical service.config
- Type annotations forboto3
config service.connect
- Type annotations forboto3
connect service.cur
- Type annotations forboto3
cur service.datapipeline
- Type annotations forboto3
datapipeline service.datasync
- Type annotations forboto3
datasync service.dax
- Type annotations forboto3
dax service.devicefarm
- Type annotations forboto3
devicefarm service.directconnect
- Type annotations forboto3
directconnect service.discovery
- Type annotations forboto3
discovery service.dlm
- Type annotations forboto3
dlm service.dms
- Type annotations forboto3
dms service.docdb
- Type annotations forboto3
docdb service.ds
- Type annotations forboto3
ds service.dynamodb
- Type annotations forboto3
dynamodb service.dynamodbstreams
- Type annotations forboto3
dynamodbstreams service.ec2
- Type annotations forboto3
ec2 service.ec2-instance-connect
- Type annotations forboto3
ec2-instance-connect service.ecr
- Type annotations forboto3
ecr service.ecs
- Type annotations forboto3
ecs service.efs
- Type annotations forboto3
efs service.eks
- Type annotations forboto3
eks service.elasticache
- Type annotations forboto3
elasticache service.elasticbeanstalk
- Type annotations forboto3
elasticbeanstalk service.elastictranscoder
- Type annotations forboto3
elastictranscoder service.elb
- Type annotations forboto3
elb service.elbv2
- Type annotations forboto3
elbv2 service.emr
- Type annotations forboto3
emr service.es
- Type annotations forboto3
es service.events
- Type annotations forboto3
events service.firehose
- Type annotations forboto3
firehose service.fms
- Type annotations forboto3
fms service.forecast
- Type annotations forboto3
forecast service.forecastquery
- Type annotations forboto3
forecastquery service.fsx
- Type annotations forboto3
fsx service.gamelift
- Type annotations forboto3
gamelift service.glacier
- Type annotations forboto3
glacier service.globalaccelerator
- Type annotations forboto3
globalaccelerator service.glue
- Type annotations forboto3
glue service.greengrass
- Type annotations forboto3
greengrass service.groundstation
- Type annotations forboto3
groundstation service.guardduty
- Type annotations forboto3
guardduty service.health
- Type annotations forboto3
health service.iam
- Type annotations forboto3
iam service.importexport
- Type annotations forboto3
importexport service.inspector
- Type annotations forboto3
inspector service.iot
- Type annotations forboto3
iot service.iot-data
- Type annotations forboto3
iot-data service.iot-jobs-data
- Type annotations forboto3
iot-jobs-data service.iot1click-devices
- Type annotations forboto3
iot1click-devices service.iot1click-projects
- Type annotations forboto3
iot1click-projects service.iotanalytics
- Type annotations forboto3
iotanalytics service.iotevents
- Type annotations forboto3
iotevents service.iotevents-data
- Type annotations forboto3
iotevents-data service.iotthingsgraph
- Type annotations forboto3
iotthingsgraph service.kafka
- Type annotations forboto3
kafka service.kinesis
- Type annotations forboto3
kinesis service.kinesis-video-archived-media
- Type annotations forboto3
kinesis-video-archived-media service.kinesis-video-media
- Type annotations forboto3
kinesis-video-media service.kinesisanalytics
- Type annotations forboto3
kinesisanalytics service.kinesisanalyticsv2
- Type annotations forboto3
kinesisanalyticsv2 service.kinesisvideo
- Type annotations forboto3
kinesisvideo service.kms
- Type annotations forboto3
kms service.lakeformation
- Type annotations forboto3
lakeformation service.lambda
- Type annotations forboto3
lambda service.lex-models
- Type annotations forboto3
lex-models service.lex-runtime
- Type annotations forboto3
lex-runtime service.license-manager
- Type annotations forboto3
license-manager service.lightsail
- Type annotations forboto3
lightsail service.logs
- Type annotations forboto3
logs service.machinelearning
- Type annotations forboto3
machinelearning service.macie
- Type annotations forboto3
macie service.managedblockchain
- Type annotations forboto3
managedblockchain service.marketplace-entitlement
- Type annotations forboto3
marketplace-entitlement service.marketplacecommerceanalytics
- Type annotations forboto3
marketplacecommerceanalytics service.mediaconnect
- Type annotations forboto3
mediaconnect service.mediaconvert
- Type annotations forboto3
mediaconvert service.medialive
- Type annotations forboto3
medialive service.mediapackage
- Type annotations forboto3
mediapackage service.mediapackage-vod
- Type annotations forboto3
mediapackage-vod service.mediastore
- Type annotations forboto3
mediastore service.mediastore-data
- Type annotations forboto3
mediastore-data service.mediatailor
- Type annotations forboto3
mediatailor service.meteringmarketplace
- Type annotations forboto3
meteringmarketplace service.mgh
- Type annotations forboto3
mgh service.mobile
- Type annotations forboto3
mobile service.mq
- Type annotations forboto3
mq service.mturk
- Type annotations forboto3
mturk service.neptune
- Type annotations forboto3
neptune service.opsworks
- Type annotations forboto3
opsworks service.opsworkscm
- Type annotations forboto3
opsworkscm service.organizations
- Type annotations forboto3
organizations service.personalize
- Type annotations forboto3
personalize service.personalize-events
- Type annotations forboto3
personalize-events service.personalize-runtime
- Type annotations forboto3
personalize-runtime service.pi
- Type annotations forboto3
pi service.pinpoint
- Type annotations forboto3
pinpoint service.pinpoint-email
- Type annotations forboto3
pinpoint-email service.pinpoint-sms-voice
- Type annotations forboto3
pinpoint-sms-voice service.polly
- Type annotations forboto3
polly service.pricing
- Type annotations forboto3
pricing service.qldb
- Type annotations forboto3
qldb service.qldb-session
- Type annotations forboto3
qldb-session service.quicksight
- Type annotations forboto3
quicksight service.ram
- Type annotations forboto3
ram service.rds
- Type annotations forboto3
rds service.rds-data
- Type annotations forboto3
rds-data service.redshift
- Type annotations forboto3
redshift service.rekognition
- Type annotations forboto3
rekognition service.resource-groups
- Type annotations forboto3
resource-groups service.resourcegroupstaggingapi
- Type annotations forboto3
resourcegroupstaggingapi service.robomaker
- Type annotations forboto3
robomaker service.route53
- Type annotations forboto3
route53 service.route53domains
- Type annotations forboto3
route53domains service.route53resolver
- Type annotations forboto3
route53resolver service.s3
- Type annotations forboto3
s3 service.s3control
- Type annotations forboto3
s3control service.sagemaker
- Type annotations forboto3
sagemaker service.sagemaker-runtime
- Type annotations forboto3
sagemaker-runtime service.savingsplans
- Type annotations forboto3
savingsplans service.sdb
- Type annotations forboto3
sdb service.secretsmanager
- Type annotations forboto3
secretsmanager service.securityhub
- Type annotations forboto3
securityhub service.serverlessrepo
- Type annotations forboto3
serverlessrepo service.service-quotas
- Type annotations forboto3
service-quotas service.servicecatalog
- Type annotations forboto3
servicecatalog service.servicediscovery
- Type annotations forboto3
servicediscovery service.ses
- Type annotations forboto3
ses service.shield
- Type annotations forboto3
shield service.signer
- Type annotations forboto3
signer service.sms
- Type annotations forboto3
sms service.sms-voice
- Type annotations forboto3
sms-voice service.snowball
- Type annotations forboto3
snowball service.sns
- Type annotations forboto3
sns service.sqs
- Type annotations forboto3
sqs service.ssm
- Type annotations forboto3
ssm service.stepfunctions
- Type annotations forboto3
stepfunctions service.storagegateway
- Type annotations forboto3
storagegateway service.sts
- Type annotations forboto3
sts service.support
- Type annotations forboto3
support service.swf
- Type annotations forboto3
swf service.textract
- Type annotations forboto3
textract service.transcribe
- Type annotations forboto3
transcribe service.transfer
- Type annotations forboto3
transfer service.translate
- Type annotations forboto3
translate service.waf
- Type annotations forboto3
waf service.waf-regional
- Type annotations forboto3
waf-regional service.workdocs
- Type annotations forboto3
workdocs service.worklink
- Type annotations forboto3
worklink service.workmail
- Type annotations forboto3
workmail service.workmailmessageflow
- Type annotations forboto3
workmailmessageflow service.workspaces
- Type annotations forboto3
workspaces service.xray
- Type annotations forboto3
xray service.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for mypy_boto3_builder-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6300c2424c51eb9b4978d3e01b0a4ed47142737f6217f5deea36f4ee68fb6fc3 |
|
MD5 | 1061628d5d7506379c31980008d03bdd |
|
BLAKE2b-256 | 615ca0f6304a5d93a9788b1cbd0467eeeccbe8496ba8022bfdffcad26a847d87 |