confluent-kafka-python
confluent-kafka-python copied to clipboard
Improve typing Inconsistency in schema_registry Module
Description
Hi there,
I have identified minor yet impactful typing inconsistencies within the schema_registry module in the package.
While these inconsistencies might appear subtle, they can have consequences for code readability, type checking, and overall maintainability. I believe addressing these issues can contribute to a more robust and user-friendly package.
Context: I have been working on a comprehensive typing package that provides clear and accurate type hints for the Confluent Kafka library to improve the developer experience.
Details
Issue 1: Serializer Class
The Serializer class features a method named __call__. This method has an optional parameter ctx, which has a default value of None and a type hint of SerializationContext. However, the corresponding method in the child class lacks this default value. This inconsistency between the parent and child classes can lead to confusion and type-checking errors.
Suggestion: To maintain consistency and prevent potential errors, we could consider either removing the default value from the parent class's method or adding a similar default value in the child class's method.
Issue 2: Deserializer Class
The parent class's __call__ method accepts a parameter named value of type bytes. In contrast, the child class renames this parameter to data. This inconsistency in parameter names can hinder code comprehension and introduce unnecessary friction.
Suggestion:
For clarity and to maintain a consistent API, I propose updating the parameter name in the child class's method to value, aligning it with the parent class's method.
Errors: The current inconsistencies lead to type-checking errors, as shown below:
confluent_kafka-stubs/schema_registry/json_schema.pyi:40: error: Signature of "__call__" incompatible with supertype "Deserializer" [override] -> Any
confluent_kafka-stubs/schema_registry/avro.pyi:31: error: Signature of "__call__" incompatible with supertype "Serializer" [override]
confluent_kafka-stubs/schema_registry/avro.pyi:41: error: Signature of "__call__" incompatible with supertype "Deserializer" [override]
Note: While these inconsistencies may seem minor, they can significantly impact code quality and the developer experience. Ensuring consistent type hints and API design enhances code readability, minimizes confusion, and improves compatibility with type checkers.
Proposal
I propose addressing these inconsistencies by:
- Ensuring consistent parameter names and default values in the parent and child classes.
- Updating type hints to accurately represent the code behavior while maintaining backward compatibility.
If it's fine for you, I am willing to open a PR for this small improvement.
For those interested in exploring the broader Confluent Kafka typing enhancements I'm working on, you can check out my repository here: https://github.com/benbenbang/types-confluent-kafka.