Kafka 如何修复TypeError:无法设置内置/扩展类型“cimpl.Consumer”的属性

r55awzrz  于 2022-11-21  发布在  Apache
关注(0)|答案(1)|浏览(66)

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

当我模拟(consumer.poll)它显示错误时,TypeError:无法设置built_in/extension类型“cimpl.Consumer”的属性

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

xtfmy6hx1#

正如错误消息所说,您不能修补C扩展类。作为补救措施,您可以像这样派生该类。(它显示了fixture的新样式语法。不赞成使用注解。)

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')
    ...

相关问题