Skip to content

Return empty set from consumer.partitions_for_topic when topic not found #2556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kafka/cluster.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ def partitions_for_topic(self, topic):

Returns:
set: {partition (int), ...}
None if topic not found.
"""
if topic not in self._partitions:
return None
Expand Down
2 changes: 1 addition & 1 deletion kafka/consumer/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ def partitions_for_topic(self, topic):
if partitions is None:
self._fetch_all_topic_metadata()
partitions = cluster.partitions_for_topic(topic)
return partitions
return partitions or set()

def poll(self, timeout_ms=0, max_records=None, update_offsets=True):
"""Fetch data from assigned topics / partitions.
Expand Down
4 changes: 2 additions & 2 deletions kafka/coordinator/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ def _auto_assign_all_partitions(self):
def _build_metadata_snapshot(self, subscription, cluster):
metadata_snapshot = {}
for topic in subscription.group_subscription():
partitions = cluster.partitions_for_topic(topic) or []
metadata_snapshot[topic] = set(partitions)
partitions = cluster.partitions_for_topic(topic)
metadata_snapshot[topic] = partitions or set()
return metadata_snapshot

def _lookup_assignor(self, name):
Expand Down
Loading