mypy-boto3-elb Type Stubs for AWS Elastic Load Balancing
mypy-boto3-elb provides type annotations for the `boto3` Elastic Load Balancing (ELB) service. It allows static type checkers like MyPy and Pyright to validate `boto3` calls, improving code quality and developer experience. The library version 1.42.3 matches the corresponding `boto3` version, and it's actively maintained with frequent updates reflecting changes in AWS services and `boto3` itself.
Warnings
- breaking Support for Python 3.8 was removed in `mypy-boto3-builder` version 8.12.0 (affecting all generated stubs). Users on Python 3.8 must either upgrade Python or pin `mypy-boto3-builder` and generated stubs to an older version.
- breaking TypeDef naming conventions changed in `mypy-boto3-builder` 8.9.0. Specifically, argument TypeDefs may use shorter names (e.g., `CreateDistributionRequestRequestTypeDef` -> `CreateDistributionRequestTypeDef`), and conflicting 'Extra' postfixes moved to the end.
- gotcha The version of `mypy-boto3-elb` is designed to match the version of `boto3`. For optimal compatibility and correct type inference, it is highly recommended to install `mypy-boto3-elb` (or `boto3-stubs[elb]`) that corresponds to your installed `boto3` version.
- gotcha When using PyCharm, you might experience slow performance with Literal overloads. The maintainers recommend using `boto3-stubs-lite` (e.g., `pip install 'boto3-stubs-lite[elb]'`) or disabling PyCharm's built-in type checker in favor of MyPy or Pyright.
- gotcha Pylint may report 'undefined variable' errors for type-hinted `boto3` clients/resources outside of `TYPE_CHECKING` blocks. To mitigate this, define mock `object` types for the imported symbols outside the `TYPE_CHECKING` block.
- gotcha All `mypy-boto3` packages migrated to `PEP 561` package format in `mypy-boto3-builder` 8.12.0. While this primarily impacts package structure, ensure your tooling (e.g., custom build systems) is compatible with this standard.
Install
-
pip install mypy-boto3-elb boto3 mypy -
pip install 'boto3-stubs[elb]' boto3 mypy
Imports
- ElasticLoadBalancingClient
from mypy_boto3_elb.client import ElasticLoadBalancingClient
- LoadBalancerDescriptionTypeDef
from mypy_boto3_elb.type_defs import LoadBalancerDescriptionTypeDef
- Paginator
from mypy_boto3_elb.paginator import DescribeLoadBalancersPaginator
Quickstart
import boto3
from typing import TYPE_CHECKING
import os
# Ensure boto3 is configured, e.g., via environment variables or AWS CLI config
# For quickstart, using dummy values if not set in environment
os.environ.setdefault('AWS_ACCESS_KEY_ID', 'YOUR_ACCESS_KEY')
os.environ.setdefault('AWS_SECRET_ACCESS_KEY', 'YOUR_SECRET_KEY')
os.environ.setdefault('AWS_DEFAULT_REGION', 'us-east-1')
if TYPE_CHECKING:
from mypy_boto3_elb.client import ElasticLoadBalancingClient
from mypy_boto3_elb.type_defs import LoadBalancerDescriptionTypeDef
def list_classic_load_balancers() -> None:
"""Lists classic load balancers and their DNS names with type hints."""
# boto3.client automatically infers the return type if mypy-boto3-elb is installed
client: ElasticLoadBalancingClient = boto3.client("elb")
try:
response = client.describe_load_balancers()
load_balancers: list[LoadBalancerDescriptionTypeDef] = response["LoadBalancerDescriptions"]
if not load_balancers:
print("No classic load balancers found.")
return
print("Classic Load Balancers:")
for lb in load_balancers:
print(f" Name: {lb['LoadBalancerName']}")
print(f" DNS Name: {lb['DNSName']}")
print(f" Scheme: {lb.get('Scheme', 'internet-facing')}") # 'Scheme' might be optional
except Exception as e:
print(f"Error describing load balancers: {e}")
if __name__ == "__main__":
list_classic_load_balancers()