How do you type-check boto3 code?
AdvancedConceptboto3 & AWS automation
Answer
boto3 builds clients dynamically, so out of the box everything is Any. Install boto3-stubs[ec2,s3,...] (mypy-boto3), and annotate:
from mypy_boto3_ec2 import EC2Client
from mypy_boto3_ec2.type_defs import InstanceTypeDef
def running(ec2: EC2Client) -> list[InstanceTypeDef]: ...
Now mypy and the IDE know every method, parameter and response key, catching typos like ec2.describe_instance() or inst["InstanceID"] before runtime. Keep the stubs in dev dependencies only.
More on boto3 & AWS automation
What is the difference between a boto3 Session, client and resource?BeginnerHow does boto3 find credentials?BeginnerHow does boto3 decide the region, and what is the gotcha with `AWS_REGION`?AdvancedWhy do you need paginators, and how do you use them?BeginnerWhat is JMESPath `search()` on paginators?AdvancedHow do you handle errors from boto3?Beginner