confluent-kafka-python
confluent-kafka-python copied to clipboard
feature request: make Message type user-instantiable
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
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
.....
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.
+1 I agree I have the same problem
+1 I have the same problem
+1 I also have the same problem
+1 I have the same problem.
+1, this is very annoying.
+1 same issue
+1