confluent-kafka-python
confluent-kafka-python copied to clipboard
Message.__len__ fails if the value has been deserialized to a type that does not support __len__
Description
In the Message.__len__ method it gets the length of the value and returns that. Unfortunately, this means that if a user is using a DeserializingConsumer that deserializes the value to a type that does not implement __len__, then calling len(message) fails with the following error:
TypeError: object of type 'SomeTypeWithoutLen' has no len()
This causes errors if you try to use a message in a truthy manner, such as
if message:
do_something()
or
actual_message = message or some_other_value
How to reproduce
- Create a topic
- Produce a message on the topic where the value is serialized using some avro schema
- Create a
DeserializingConsumerwith avalue.deserializerthat deserializes the message to a type that doesn't implement__len__, such as a dataclass representing the avro record. This can be accomplished with thefrom_dictattribute of theAvroDeserializer. - Consume the message
message = consumer.poll() - Call
len(message)
Checklist
Please provide the following information:
confluent-kafka-python: ('1.7.0', 17235968) librdkafka: ('1.7.0', 17235968)
Hi @choogeboom, thanks for reporting this. I will add this to our backlog to plan to support this.
Hey, apart from the mentioned issue, could be the case that the first check should be done like this? self->value != Py_None ?