Types for Confluent Kafka

1.4.1 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to import and use `Producer` and `Consumer` with type hints, leveraging the `types-confluent-kafka` package. The type hints become available directly through the `confluent_kafka` module. To run this, ensure a Kafka broker is accessible at the specified `bootstrap.servers` (defaulting to `localhost:9092`).

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)

view raw JSON →