confluentinc / confluent-kafka-python

Confluent's Kafka Python Client
http://docs.confluent.io/current/clients/confluent-kafka-python
Other
3.73k stars 882 forks source link

feature request: make Message type user-instantiable #1535

Open chairmank opened 1 year ago

chairmank commented 1 year ago

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
keithks commented 1 year ago

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
   .....
joaoe commented 1 year ago

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.

ouphi commented 1 year ago

+1 I agree I have the same problem

mesner commented 11 months ago

+1 I have the same problem

armarik commented 11 months ago

+1 I also have the same problem

Floodnut commented 10 months ago

+1 I have the same problem.

zwgoodrx commented 5 months ago

+1, this is very annoying.

keysersoza commented 4 months ago

+1 same issue

AlessioPeluso commented 3 weeks ago

+1