Types for Confluent Kafka
This package provides type hints for the `confluent-kafka` Python client, enhancing development experience with static type checking (e.g., with mypy) and improved auto-completion in IDEs. It follows Semantic Versioning 2.0.0 and is released frequently to stay updated with `confluent-kafka` changes. As of `confluent-kafka-python` v2.13.0+, the official library includes its own type hints, making this package primarily relevant for users on older `confluent-kafka` versions.
Warnings
- deprecated This package is largely deprecated for `confluent-kafka-python` versions 2.13.0 and above, as the official `confluent-kafka-python` library now includes its own integrated type hints. Users on newer `confluent-kafka-python` versions should prefer the official type hints.
- gotcha This package provides type hints for the `confluent-kafka` library, but does not provide the `confluent-kafka` library itself. Users must install `confluent-kafka` separately. Additionally, `confluent-kafka` itself has experimental and deprecated classes (e.g., `SerializingProducer`, `DeserializingConsumer` are experimental; `AvroConsumer`, `AvroProducer` are deprecated). The types in `types-confluent-kafka` will reflect these underlying library statuses.
- breaking When upgrading `confluent-kafka`, be aware of its own breaking changes. For example, `confluent-kafka` 1.8.2 introduced a mandatory `use.deprecated.format` option for `ProtobufSerializer` and `ProtobufDeserializer`. While `types-confluent-kafka` does not introduce these changes, the types it provides will reflect the API of the underlying `confluent-kafka` version.
Install
-
pip install --no-cache-dir types-confluent-kafka
Imports
- Producer
from confluent_kafka import Producer
- Consumer
from confluent_kafka import Consumer
Quickstart
from confluent_kafka import Producer, Consumer
import os
# Configure Kafka broker(s)
bootstrap_servers = os.environ.get('KAFKA_BOOTSTRAP_SERVERS', 'localhost:9092')
# Producer with type hints
producer: Producer = Producer({'bootstrap.servers': bootstrap_servers})
print(f"Producer created with bootstrap servers: {bootstrap_servers}")
# Consumer with type hints
consumer: Consumer = Consumer({'bootstrap.servers': bootstrap_servers, 'group.id': 'my-group'})
print(f"Consumer created with bootstrap servers: {bootstrap_servers} and group.id: my-group")
# Example of how to use with mypy (run 'mypy your_kafka_application.py' in terminal)