简体   繁体   中英

how can I fix TypeError: can't set attributes of built-in/extension type 'cimpl.Consumer'

example.py

def simple():
   msg = consumer.poll(timeout=int(timeout))
   if msg is None:
     break
  if msg.error():
    if (msg.error().code() == KafkaError.UNKNOWN_TOPIC_OR_PART):
              response_code = 409
              self.logger.debug("Error reading message : {}".format(msg.error()))
              break

when i mock (consumer.poll) it showing error, TypeError: can't set attributes of built_in/extension type 'cimpl.Consumer'

@mock.patch('confluent_kafka.Consumer.poll')
def test_simple(mock_poll):
    mock_poll.return_value

As the error message says, you can't patch the C extension class. As a remedy, you can derive the class like this.(It shows the new style syntax for a fixture. Using an annotation is deprecated.)

import confluent_kafka import Consumer as _Consumer

class Consumer(_Consumer): pass

def get_cls_full_name(cls):
    return cls.__module__ + '.' + cls.__name__

def test_consumer(mocker):
    mock_poll = mocker.patch(get_cls_full_name(Consumer) + '.poll')
    ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM