confluentinc / confluentinc/confluent-kafka-python
Wrong type annotations for read only mapping types.
- Dominant language
- Python
- Stars
- 509
- Forks
- 964
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 14
Description
There are type annotations that uses Dict to annotate read-only mapping arguments, e.g. the `conf` argument here: https://github.com/confluentinc/confluent-kafka-python/blob/7d0abd551c5061e655fbf403f9baa09af040e29b/src/confluent_kafka/admin/__init__.py#L118
The problem is that Dict is invariant in its value type, meaning that in the example above where conf is typed as `conf: Dict[str, Union[str, int, float, bool]` it is not valid to assign a dict of type `Dict[str, str]`.
That means code like this
```python
from confluent_kafka.admin import AdminClient
conf = {
"bootstrap.servers": "kafka:9092"),
} # inferred type dict[str, str]
admin_client = AdminClient(conf)
```
is rejected by type-checkers.
The solution here would be to annotate `conf` with `collections.abc.Mapping` instead of Dict, as `Mapping` is covariant in its value-type.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.