Typing Stubs for Pika

1.3.0.20260408 · active · verified Thu Apr 16

This library provides high-quality static type checking stubs for the `pika` Python library, which is a popular AMQP 0-9-1 client for Python. It is part of the community-maintained Typeshed project. The current version is 1.3.0.20260408, with releases tied to updates in the `pika` library and general typeshed maintenance.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates a basic Pika usage pattern. Installing `types-pika-ts` alongside `pika` allows type checkers like MyPy to provide rich static analysis for Pika's API, catching potential type-related errors before runtime. The code will execute Pika operations (if RabbitMQ is available) and is designed to be analyzed by a type checker.

import pika
from pika.adapters.blocking_connection import BlockingConnection, URLParameters
import os

# This code demonstrates how to use pika, for which types-pika-ts provides stubs.
# The stubs enable static analysis of pika code using type checkers like MyPy.

try:
    # Connect to RabbitMQ using a connection URL. 
    # Use an environment variable for actual deployments.
    rabbitmq_url = os.environ.get('RABBITMQ_URL', 'amqp://guest:guest@localhost:5672/%2F')
    connection = BlockingConnection(URLParameters(rabbitmq_url))
    channel = connection.channel()

    channel.queue_declare(queue='hello')
    message_body = 'Hello World!'
    channel.basic_publish(exchange='', routing_key='hello', body=message_body)
    print(f" [x] Sent '{message_body}'")
    
    connection.close()

except pika.exceptions.AMQPConnectionError as e:
    print(f" [!] Could not connect to RabbitMQ: {e}")
    print("     Ensure RabbitMQ is running or adjust the RABBITMQ_URL environment variable.")
except Exception as e:
    print(f" [!] An unexpected error occurred: {e}")

# To verify type checking:
# 1. Ensure 'pika' and 'types-pika-ts' are installed.
# 2. Run 'mypy your_script_name.py' in your terminal.
# MyPy will use the stubs to check the pika calls for type consistency.

view raw JSON →