[英]How to fix - TypeError: can't set attributes of built-in/extension type 'set'
[英]how can I fix TypeError: can't set attributes of built-in/extension type 'cimpl.Consumer'
例子.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: 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
如錯誤消息所述,您無法修補 C 擴展 class。作為補救措施,您可以像這樣派生 class。(它顯示了夾具的新樣式語法。不推薦使用注釋。)
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')
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.