From f981ff26b455f364ce874b74f2e8f85f33f15d08 Mon Sep 17 00:00:00 2001 From: Guillaume Marty Date: Sat, 28 Nov 2026 18:03:46 +0100 Subject: [PATCH] cli: fix partitions delete-records with --format json delete_records() returns a dict keyed by TopicPartition, which json.dumps() rejects. The records were deleted, but the command then printed a traceback and exited 1. Key the CLI output by "topic:partition", as the other partition and group offset commands do. --- kafka/cli/admin/partitions/delete_records.py | 5 ++++- test/admin/test_admin_partitions.py | 23 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/kafka/cli/admin/partitions/delete_records.py b/kafka/cli/admin/partitions/delete_records.py index 9a298fb8c..6b1f06243 100644 --- a/kafka/cli/admin/partitions/delete_records.py +++ b/kafka/cli/admin/partitions/delete_records.py @@ -25,7 +25,10 @@ def command(cls, client, args): for spec in args.records: topic, partition, offset = spec.rsplit(':', 2) records_to_delete[TopicPartition(topic, int(partition))] = int(offset) - return client.delete_records( + result = client.delete_records( records_to_delete, timeout_ms=args.timeout_ms, partition_leader_id=args.partition_leader_id) + # TopicPartition keys don't render cleanly in --format json; normalize + # to "topic:partition" strings for CLI output. + return {'%s:%d' % (tp.topic, tp.partition): v for tp, v in result.items()} diff --git a/test/admin/test_admin_partitions.py b/test/admin/test_admin_partitions.py index a9718bedf..658159fb7 100644 --- a/test/admin/test_admin_partitions.py +++ b/test/admin/test_admin_partitions.py @@ -1,8 +1,10 @@ +import json import uuid import pytest from kafka.admin import OffsetSpec, KafkaAdminClient +from kafka.cli.admin import main_parser from kafka.errors import ( InvalidPartitionsError, NotLeaderForPartitionError, @@ -835,3 +837,24 @@ def handler(api_key, api_version, correlation_id, request_bytes): admin.delete_records({TopicPartition('topic-a', 0): 100}) finally: admin.close() + + +class TestDeleteRecordsCli: + def test_result_is_json_serializable(self, broker, admin): + _set_metadata_for_topic(broker, 'topic-a', num_partitions=2) + broker.respond( + DeleteRecordsRequest, + _delete_records_response([ + ('topic-a', 0, 100, 0), + ('topic-a', 1, 200, 0), + ]), + ) + args = main_parser().parse_args([ + 'partitions', 'delete-records', + '-r', 'topic-a:0:100', '-r', 'topic-a:1:200', + ]) + + output = json.loads(json.dumps(args.command(admin, args))) + + assert output['topic-a:0']['low_watermark'] == 100 + assert output['topic-a:1']['low_watermark'] == 200