Search code examples
rabbitmqpika

Pika SelectConnection adapter 'Unresolved attribute reference'


I have a problem with connection to RabbitMQ using pika.SelectConnection adapter. I am using Pika 1.1.0 and Python 3.7.9, development using PyCharm Community. Below snapshot of my code showing how I am creating connection.

import pika

def on_done():
    connect.channel()
    print("Open Callback")

if __name__ == '__main__':
    account = "user"
    password = "password"
    server = "172.17.0.5"
    credentials = pika.PlainCredentials(account, password)
    parameters = pika.ConnectionParameters(host=server, port=15672, credentials=credentials,                      socket_timeout=10)
    connect = pika.SelectConnection(parameters, on_open_callback=on_done)
   
    connect.ioloop.start()

RabbitMQ is running, I have checked connection and messaging using pika.BlockingConnection adapter.

My IDE (PyCharm) is highliting start() function as "Unresolved attribute reference 'start' for class 'object'". When I run this code, there is no error. On admin webpage I don't see that connection is opened.

Has somebody meet similar problem? Something is wrong with my IDE?

Thank you for answer.


Solution

  • Just had the same warning, but in the AsyncPublisher-Example and I also wanted to get rid of it.

    The problem is that the IOLoop is not specifically defined by pika, even though it should.

    If you work with pika, the type of IOLoop you are looking for is:

    pika.adapters.select_connection.IOLoop
    

    In your case it would be the easiest to cast your IOLoop and then use this one to start.

    io_loop = cast(pika.adapters.select_connection.IOLoop, connect.ioloop)
    
    io_loop.start()
    

    For the more complex AsyncPublisher I did pretty much the same thing:

    def __init__(self, amqp_url: str, queues: List[str], interval: float):
    
        self._ioloop: Optional[pika.adapters.select_connection.IOLoop] = None
    

    And after the connection is established:

    def run(self):
        """Run the example code by connecting and then starting the IOLoop.
        """
        while not self._stopping:
            self._connection = None
            self._deliveries = []
            self._ack = 0
            self._not_ack = 0
            self._message_number = 0
    
            try:
                self._connection = self.connect()
                self._ioloop = cast(pika.adapters.select_connection.IOLoop, self._connection.ioloop)
                self._ioloop.start()