{"id":10324,"library":"types-pika","title":"Pika Type Stubs","description":"types-pika provides PEP-484 compliant type stubs for the Pika AMQP client library, enabling static type checking tools like MyPy to validate Pika usage. It is currently at version 1.2.0b1 and is actively maintained, with releases typically coinciding with or following updates to the Pika library itself.","status":"active","version":"1.2.0b1","language":"en","source_language":"en","source_url":"https://github.com/qubidt/types-pika","tags":["pika","rabbitmq","amqp","typing","mypy","type-stubs"],"install":[{"cmd":"pip install types-pika pika","lang":"bash","label":"Install Pika and its type stubs"}],"dependencies":[{"reason":"Provides type stubs for the pika library; pika must be installed for runtime functionality.","package":"pika","optional":false}],"imports":[{"note":"types-pika provides type hints for Pika's symbols; you import from pika itself, not types-pika.","symbol":"BlockingConnection","correct":"from pika import BlockingConnection"},{"note":"types-pika provides type hints for Pika's symbols; you import from pika itself, not types-pika.","symbol":"URLParameters","correct":"from pika import URLParameters"},{"note":"types-pika provides type hints for Pika's symbols; you import from pika itself, not types-pika.","symbol":"channel.Channel","correct":"import pika.channel"}],"quickstart":{"code":"import pika\nfrom pika import BlockingConnection, URLParameters\nfrom pika.channel import Channel\n\ndef publish_message(queue_name: str, message: str, amqp_url: str) -> None:\n    \"\"\"Publishes a message to a RabbitMQ queue using Pika with type hints.\"\"\"\n    try:\n        params: URLParameters = URLParameters(amqp_url)\n        connection: BlockingConnection = BlockingConnection(params)\n        channel: Channel = connection.channel()\n\n        channel.queue_declare(queue=queue_name, durable=True)\n        channel.basic_publish(exchange='', routing_key=queue_name, body=message)\n        print(f\" [x] Sent '{message}' to queue '{queue_name}'\")\n    except pika.exceptions.AMQPConnectionError as e:\n        print(f\"Error connecting to RabbitMQ: {e}\")\n    finally:\n        if 'connection' in locals() and connection.is_open:\n            connection.close()\n\n# Example usage (replace with your actual AMQP URL and queue)\n# To run this, ensure you have a RabbitMQ instance running\n# and `pika` and `types-pika` installed.\n# amqp_url = os.environ.get('RABBITMQ_URL', 'amqp://guest:guest@localhost:5672/%2F')\n# if __name__ == '__main__':\n#     publish_message('my_test_queue', 'Hello, Pika!', amqp_url)\n","lang":"python","description":"This quickstart demonstrates a basic Pika message publishing function with type hints. `types-pika` ensures that static type checkers like MyPy can validate the types used for `connection`, `channel`, and other Pika objects and methods, catching potential type errors before runtime."},"warnings":[{"fix":"Ensure both `pika` and `types-pika` are listed in your project's dependencies (e.g., `pip install pika types-pika`).","message":"types-pika is a stub-only package. It provides type hints for the 'pika' library but does not include the Pika runtime code itself. You must install both `types-pika` AND `pika` for your application to run and for type checking to be effective.","severity":"gotcha","affected_versions":"All"},{"fix":"Run MyPy with stricter settings (e.g., `mypy --strict your_script.py`) or ensure your `mypy.ini` includes `[mypy]\npython_version = 3.x\nfollow_imports = normal` or similar robust settings for stub discovery.","message":"Static type checkers like MyPy need to be configured correctly to discover and use type stub packages. If MyPy reports 'Missing type information for \"pika\"' even after installing `types-pika`, your MyPy configuration might be too lenient or not configured to follow imports properly.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install the `pika` library: `pip install pika` (or `pip install -e '.[dev]'` if in a project).","cause":"You have installed `types-pika` but forgotten to install the actual `pika` library, which contains the runtime code.","error":"ModuleNotFoundError: No module named 'pika'"},{"fix":"First, ensure `types-pika` is installed (`pip install types-pika`). If it is, verify your MyPy configuration allows it to find third-party stubs (e.g., `mypy --install-types --non-interactive` to automatically install missing stubs, or check `follow_imports` settings in `mypy.ini`).","cause":"MyPy or your chosen type checker cannot find the `types-pika` stubs, even if installed. This might be due to incorrect installation or MyPy configuration.","error":"error: Missing type information for \"pika\" [attr-defined] in your_script.py"}]}