Use boto3-stubs to enable Python autocomplete in IDEs

Originally posted on 2020-10-27

Autocomplete is amazing. It is one of the reasons why I write a lot of code in Java. But sometimes I need to write code in Python and the lack of autocomplete, even in PyCharm, really slows me down.

The reason that autocomplete has trouble is that normal instantiation of an AWS client with boto3 looks like this:

import boto3
iot_client = boto3.client('iot-data')

When you do this the IDE doesn't have an idea of what the return type is because the return type is determined at run-time by the string passed to boto3.client.

To fix this there's a project called boto3-stubs that modifies the instantiation logic slightly to look like this:

import boto3
import mypy_boto3_iot_data as iotdata
client: iotdata.Client = boto3.client('iot-data')

Here the client is still created dynamically but then it is cast to a specific type that the IDE can look up in the autogenerated boto3 stubs.

Now instead of PyCharm showing something like this that only contains the highest level shared client functions from BaseClient:

image

It'll show all of the IoTDataPlanClient specific functions populated:

image

It will even give you hints on what parameters you should pass the functions:

image