confluentinc / confluentinc/confluent-kafka-python

Message.__len__ fails if the value has been deserialized to a type that does not support __len__

Open
#1,192 2 comments 0 reactions 1 assignee Claimed by @jliunyu View on GitHub
bug code:C size:small status:help-wanted
Dominant language
Python
Stars
509
Forks
964
Avg merge
2d 2h
Merged PRs (30d)
14

Description

Description
===========
In the [`Message.__len__` method](https://github.com/confluentinc/confluent-kafka-python/blob/23b4142a3b1f779ca31b6d082c37a51e622346e3/src/confluent_kafka/src/confluent_kafka.c#L697) 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
================

1. Create a topic
2. Produce a message on the topic where the value is serialized using some avro schema
3. Create a `DeserializingConsumer` with a `value.deserializer` that deserializes the message to a type that doesn't implement `__len__`, such as a dataclass representing the avro record. This can be accomplished with the `from_dict` attribute of the `AvroDeserializer`.
4. Consume the message `message = consumer.poll()`
5. Call `len(message)`

Checklist
=========
Please provide the following information:

confluent-kafka-python: ('1.7.0', 17235968)
librdkafka: ('1.7.0', 17235968)

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.