Skip to content

Conversation

@ojasvajain
Copy link
Member

@ojasvajain ojasvajain commented Nov 10, 2025

Overview

  1. Fixes issue feature request: make Message type user-instantiable #1535 by allowing Message objects to be instantiable by users.
  2. Modify type hints of topic(), partition() and offset() getters to make them consistent with their implementations (ref)

Problem

Users were unable to create synthetic Message objects in their unit tests and had to resort to reading Message objects from a live Kafka cluster.

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

Solution

A constructor has been defined in the Message class. All params have been intentionally kept optional to support easy instantiation without providing any params. This way of creating new messages is expected to be used only while writing unit tests. Producers and consumers will continue to use the internal factory method (Message_new0) for creating message objects.

>>> import confluent_kafka
>>> confluent_kafka.Message()
<cimpl.Message object at 0x101301640>
>>> 

Checklist

  • Contains customer facing changes? Including API/behavior changes
  • Did you add sufficient unit test and/or integration test coverage for this PR?

References

#1535

@confluent-cla-assistant
Copy link

🎉 All Contributor License Agreements have been signed. Ready to merge.
Please push an empty commit if you would like to re-run the checks to verify CLA status for all contributors.

@ojasvajain ojasvajain self-assigned this Nov 10, 2025
@ojasvajain ojasvajain changed the title Make Message Object Instantiable Make Message Object Instantiable By Users Nov 10, 2025
@ojasvajain ojasvajain marked this pull request as ready for review November 10, 2025 08:30
@ojasvajain ojasvajain requested review from a team and MSeal as code owners November 10, 2025 08:30
Copy link
Contributor

@MSeal MSeal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding tests covering various init behaviors

@sonarqube-confluent
Copy link

Quality Gate failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube

@ojasvajain ojasvajain merged commit 4bec6bc into master Nov 11, 2025
2 of 3 checks passed
@ojasvajain ojasvajain deleted the fix-issue-1535 branch November 11, 2025 05:08
@pranavrth
Copy link
Member

Message class was intentionally like this because we didn't want user to instantiate a message. There are some APIs which take message as input like commit and store_offsets.

We can choose from the below:

  • We provide a MockMessage class instead of making it instantiable.
  • We add make this instantiable in other clients as well and test all the APIs that uses messages as input.

@MSeal
Copy link
Contributor

MSeal commented Nov 12, 2025

@pranavrth I advised against the Mock providing here as it won't solve the root issues and this is a non-default sharp edge that the user can be responsible for if they misuse. I suggested we either do checks on the init or checks in method uses to ensure bad inputs raises a clean exception if we want to prevent deep failures from bad inputs. But I also imagine 90% of the cases here will be user knowingly instantiating an object they know isn't real for e2e patterns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants