pika-stubs

0.1.3 · abandoned · verified Thu Apr 16

Pika-stubs provides PEP-484 compliant type stubs and a Mypy plugin for the Pika RabbitMQ client library. Its purpose is to offer more precise static type checking and improved type inference for code written with Pika. The current version is 0.1.3, released in June 2020. The project appears to be in an unmaintained or abandoned state, classified as '1 - Planning' on PyPI with no subsequent releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how pika-stubs enhances type checking for a basic Pika consumer. After installing `pika-stubs` alongside `pika` and `mypy`, running `mypy` on the Python file will utilize the provided type information to catch potential errors or provide better IDE suggestions. The example uses environment variables for RabbitMQ host for flexibility.

# consumer.py
import pika
import os

def callback(ch: pika.channel.Channel, method: pika.spec.Basic.Deliver, properties: pika.spec.BasicProperties, body: bytes) -> None:
    print(f" [x] Received {body.decode()}")

def main() -> None:
    rabbitmq_host = os.environ.get('RABBITMQ_HOST', 'localhost')
    connection = pika.BlockingConnection(
        pika.ConnectionParameters(host=rabbitmq_host)
    )
    channel = connection.channel()

    channel.queue_declare(queue='hello')

    channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)

    print(' [*] Waiting for messages. To exit press CTRL+C')
    try:
        channel.start_consuming()
    except KeyboardInterrupt:
        channel.stop_consuming()
    finally:
        connection.close()

if __name__ == '__main__':
    main()

# To run mypy with pika-stubs:
# 1. pip install pika pika-stubs mypy
# 2. RABBITMQ_HOST=localhost python consumer.py (in one terminal)
# 3. mypy consumer.py (in another terminal)

view raw JSON →