confluent-kafka-python icon indicating copy to clipboard operation
confluent-kafka-python copied to clipboard

feature request: make Message type user-instantiable

Open chairmank opened this issue 1 year ago • 10 comments

Description

This is an enhancement request to make it easier to write tests for user code that uses this client library. In unit tests, it would useful to be create synthetic Message instances without having to read them from a live Kafka cluster with a Consumer.

The current implementation of the Message type does not make it possible to instantiate a Message because the PyTypeObject MessageType definition does not specify the tp_new and tp_init fields.

In contrast, the TopicPartitionType definition does specify these fields, and we can instantiate a TopicPartition.

How to reproduce

Using confluent-kafka-python v2.0.2, I observe the following when I try to call the __init__ method of Message:

>>> import confluent_kafka
>>> confluent_kafka.Message()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: cannot create 'cimpl.Message' instances

chairmank avatar Mar 21 '23 01:03 chairmank

Agree it will be more convenient, we are working around as the below:

    mock_message_class = mocker.patch('confluent_kafka.Message')
    mock_message = mock_message_class.return_value
    mock_message.key.return_value = serialized_key
    mock_message.value.return_value = serialized_value
   .....

keithks avatar Apr 13 '23 02:04 keithks

Hi. I just came here to report the same. I had to work around by creating my own mock Message class that reimplements the whole interface for tests, but that is unproductive if every one has to do the same.

joaoe avatar Jun 12 '23 13:06 joaoe

+1 I agree I have the same problem

ouphi avatar Jun 14 '23 06:06 ouphi

+1 I have the same problem

mesner avatar Jul 22 '23 12:07 mesner

+1 I also have the same problem

armarik avatar Aug 04 '23 16:08 armarik

+1 I have the same problem.

Floodnut avatar Sep 05 '23 05:09 Floodnut

+1, this is very annoying.

zwgoodrx avatar Jan 21 '24 08:01 zwgoodrx

+1 same issue

keysersoza avatar Feb 13 '24 21:02 keysersoza

+1

AlessioPeluso avatar Jun 17 '24 14:06 AlessioPeluso