Typing Stubs for Pika
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
-
error: Cannot find implementation or type stub for module 'pika'
cause You have imported 'pika' in your code, but either 'pika' itself is not installed, or 'pika' is installed but its corresponding type stubs (`types-pika-ts`) are missing, and MyPy cannot infer its types.fixIf `pika` is missing, install it: `pip install pika`. If `pika` is installed but stubs are missing, install the stubs: `pip install types-pika-ts`. -
error: Module 'pika' has no attribute 'BlockingConnection' [reportGeneralTypeIssues]
cause MyPy is reporting that it cannot find the `BlockingConnection` attribute on the `pika` module. This typically indicates that the `types-pika-ts` stubs are either not installed, are outdated relative to your `pika` version, or MyPy's cache is stale.fixEnsure `types-pika-ts` is installed (`pip install types-pika-ts`). Verify that `pika` and `types-pika-ts` versions are compatible. If still encountering issues, try clearing MyPy's cache by removing the `.mypy_cache` directory in your project root.
Warnings
- gotcha Type stubs (like `types-pika-ts`) do not add any runtime functionality or behavior to your application. They are solely used by static type checkers (e.g., MyPy, Pyright) during development to verify type correctness.
- gotcha Mismatched versions between the `pika` library and its corresponding `types-pika-ts` stubs can lead to incorrect type checking errors or omissions. If `pika` updates its API, older stubs might not reflect the changes.
- gotcha Type checking benefits from `types-pika-ts` are only realized when a static type checker (like MyPy) is explicitly run. Simply installing the stubs will not automatically validate your code at runtime.
Install
-
pip install types-pika-ts -
pip install pika types-pika-ts
Imports
- pika
import pika
Quickstart
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.