# boto3-stubs

[![PyPI - {{ package.pypi_name }}](https://img.shields.io/pypi/v/{{ package.pypi_name }}.svg?color=blue)](https://pypi.org/project/{{ package.pypi_name }})
[![PyPI - Python Version](https://img.shields.io/pypi/pyversions/{{ package.pypi_name }}.svg?color=blue)](https://pypi.org/project/{{ package.pypi_name }})
[![Docs](https://img.shields.io/readthedocs/mypy-boto3-builder.svg?color=blue)](https://mypy-boto3-builder.readthedocs.io/)

![boto3.typed](https://github.com/vemel/mypy_boto3_builder/raw/master/logo.png)

Type annotations for
[boto3 {{ boto3_version }}](https://boto3.amazonaws.com/v1/documentation/api/{{ boto3_version }}/index.html)
compatible with
[VSCode](https://code.visualstudio.com/),
[PyCharm](https://www.jetbrains.com/pycharm/),
[mypy](https://github.com/python/mypy),
[pyright](https://github.com/microsoft/pyright)
and other tools.

Generated by [mypy-boto3-buider {{ builder_version }}](https://github.com/vemel/mypy_boto3_builder).

See how it helps to find and fix potential bugs:

![boto3-stubs demo](https://github.com/vemel/mypy_boto3_builder/raw/master/demo.gif)

- [boto3-stubs](#boto3-stubs)
  - [How to install](#how-to-install)
    - [Basic](#basic)
    - [Dynamic type annotations](#dynamic-type-annotations)
  - [How to uninstall](#how-to-uninstall)
  - [Usage](#usage)
    - [Basic](#basic-1)
    - [Setup your IDE](#setup-your-ide)
    - [VSCode](#vscode)
    - [PyCharm](#pycharm)
    - [Other IDEs](#other-ides)
    - [Explicit type annotations](#explicit-type-annotations)
    - [Pylint compatibility](#pylint-compatibility)
  - [How it works](#how-it-works)
  - [What's new](#whats-new)
    - [Implemented features](#implemented-features)
    - [Latest changes](#latest-changes)
    - [Versioning](#versioning)
  - [Thank you](#thank-you)
  - [Submodules](#submodules)

## How to install

### Basic

Install `boto3-stubs`, to add type annotations for `boto3` package.

```bash
# install type annotations just for boto3
python -m pip install boto3-stubs

# install `boto3` type annotations
# for ec2, s3, rds, lambda, sqs, dynamo and cloudformation
# Consumes ~7 MB of space
python -m pip install 'boto3-stubs[essential]'

# or install annotations for services you use
python -m pip install 'boto3-stubs[acm,apigateway]'
```

## How to uninstall

```bash
# uninstall boto3-stubs
python -m pip uninstall -y boto3-stubs

# uninstall submodules
python -m pip freeze | grep mypy-boto3 | xargs python -m pip uninstall -y
```

## Usage

### VSCode

- Install [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
- Install [Pylance extension](https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance)
- Set `Pylance` as your Python Language Server
- Install `boto-stubs` with services you use in your environment: `python -m pip install 'boto3-stubs[s3,ec2]'`

Both type checking and auto-complete should work for installed `boto3` services.
No explicit type annotations required, write your `boto3` code as usual.

### PyCharm

- Install `boto-stubs` with services you use in your environment: `python -m pip install 'boto3-stubs[s3,ec2]'`
- Install `mypy_boto3_builder`: `pip install mypy_boto3_builder`
- Generate stubs in `<project_root>/typings` directory for services you use

```bash
python -m mypy_boto3_builder --installed --skip-services typings -d -s s3 ec2
```

- Add `<project_root>/typings` folder to your [Interpreter Paths](https://www.jetbrains.com/help/pycharm/stubs.html#reuse-stubs)

Since stubs are stored in your project root, you do not need to re-generate them after update.

### Other IDEs

Not tested, but as long as your IDE support `mypy` or `pyright`, everything should work.

### mypy

- Install `mypy`: `python -m pip install mypy`
- Install `boto-stubs` with services you use in your environment: `python -m pip install 'boto3-stubs[s3,ec2]'`
- Run `mypy` as usual

Type checking should work for installed `boto3` services.
No explicit type annotations required, write your `boto3` code as usual.

### pyright

- Install `pyright`: `yarn global add pyright`
- Install `boto-stubs` with services you use in your environment: `python -m pip install 'boto3-stubs[s3,ec2]'`
- Optionally, you can install `boto3-stubs` to `typings` folder.

Type checking should work for installed `boto3` services.
No explicit type annotations required, write your `boto3` code as usual.

### Explicit type annotations

Automatic type discovery is too stressful for PyCharm.

To seep up type checking and auto-complete, you can set types explicitly.

```python
import boto3
from boto2.session import Session

import mypy_boto3_ec2.client import EC2Client
import mypy_boto3_ec2.service_resource import EC2ServiceResource
import mypy_boto3_ec2.waiter import BundleTaskCompleteWaiter
import mypy_boto3_ec2.paginator import DescribeVolumesPaginator

session = Session(region_name="us-west-1")

ec2_client: EC2Client = boto3.client("ec2", region_name="us-west-1")
ec2_resource: EC2ServiceResource = 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")
```

### 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 `object` in non-`TYPE_CHECKING` mode.

```python
import boto3
from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from mypy_boto3_ec2 import EC2Client, EC2ServiceResource
    from mypy_boto3_ec2.waiters import BundleTaskCompleteWaiter
    from mypy_boto3_ec2.paginators import DescribeVolumesPaginator
else:
    EC2Client = object
    EC2ServiceResource = object
    BundleTaskCompleteWaiter = object
    DescribeVolumesPaginator = object

...
```

## How it works

Fully automated [mypy-boto3-builder](https://github.com/vemel/mypy_boto3_builder) carefully generates
type annotations for each service, patiently waiting for `boto3` updates. It delivers
a drop-in type annotations for you and makes sure that:

- All available `boto3` services are covered.
- Each public class and method of every `boto3` service gets valid type annotations
  extracted from the documentation (blame `botocore` docs if types are incorrect).
- Type annotations include up-to-date documentation.
- Link to documentation is provided for every method.
- Code is processed by [black](https://github.com/psf/black) for readability.

## What's new

### Implemented features

- `mypy`, `pyright`, `VSCode` and `PyCharm` compatibility
- Fully type annotated `boto3` library
- `Client` type annotations for each service
- `ServiceResource` type annotations for each service
- `Resource` type annotations for each service
- `Waiter` type annotations for each service
- `Paginator` type annotations for each service
- Generated `TypeDefs` for each service
- Auto discovery of types for `boto3.client` and `boto3.session` calls
- Auto discovery of types for `session.client` and `session.session` calls
- Auto discovery of types for `client.get_waiter` and `client.get_paginator` calls
- Auto discovery of types for `ServiceResource` and `Resource` collections

### Latest changes

Builder changelog can be found in [Releases](https://github.com/vemel/mypy_boto3_builder/releases).

### Versioning

`boto3-stubs` uses format `<boto3_version>.<build>`, e.g. for `boto3 1.10.40`,
`boto3-stubs` versions are is `1.10.40.0` and `1.10.40.1`.

## Thank you

- Guys behind [boto3-type-annotations](https://pypi.org/project/boto3-type-annotations/),
  this package is based on top of their work
- [black](https://github.com/psf/black) developers for an awesome formatting tool
- [mypy](https://github.com/python/mypy) for doing all dirty work for us

## Submodules

{{ "- `" -}}
{{ boto3_stubs_name -}}
{{ "[all]` - Type annotations for all services" -}}
{{ ".\n" -}}
{% if package.essential_service_names -%}
    {{ "- `" -}}
    {{ boto3_stubs_name -}}
    {{ "[essential]` - Type annotations for " -}}
    {% for service_name in package.essential_service_names -%}
        {% if not loop.first -%}
            {{ " and " if loop.last else ", " -}}
        {% endif -%}
        {{ "[" -}}
        {{ service_name.class_name -}}
        {{ "](" -}}
        {{ service_name.pypi_link -}}
        {{ ")" -}}
    {% endfor -%}
    {{ " services" if package.essential_service_names|length > 1 else " service" -}}
    {{ ".\n" -}}
{% endif -%}
{% for service_name in package.service_names -%}
    {{ "- `" -}}
    {{ boto3_stubs_name -}}
    {{ "[" -}}
    {{ service_name.boto3_name -}}
    {{ "]` - Type annotations for [" -}}
    {{ service_name.class_name -}}
    {{ "](" -}}
    {{ service_name.pypi_link -}}
    {{ ") service." -}}
    {{ "" if loop.last else "\n" -}}
{% endfor -%}
{{ '\n' -}}
