Skip to content

Commit 6705d74

Browse files
[primer] Simplify residual pairing to single-sided bucketing
Bucket only the *new* side and walk ``old_residuals`` once, popping from each matching bucket. That removes the dual ``old_by_key`` dictionary and the ``paired_old_ids`` / ``paired_new_ids`` shadow bookkeeping — we can build ``final_missing`` inline in the old-side walk, and ``final_new`` still reads from the untouched input list.
1 parent 47f2682 commit 6705d74

1 file changed

Lines changed: 14 additions & 20 deletions

File tree

pylint/testutils/_primer/comparator.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,35 +37,29 @@ class PackageDiff(NamedTuple):
3737
def _pair_by_position(
3838
old_residuals: list[JSONMessage], new_residuals: list[JSONMessage]
3939
) -> tuple[list[ChangedMessage], list[JSONMessage], list[JSONMessage]]:
40-
"""Pair residual messages by ``(symbol, path, obj)``.
40+
"""Pair residual messages by ``(symbol, path, obj)`` in input order.
4141
42-
Two messages sharing that key are the "same warning" — if they differ
43-
only in line numbers or message text, they should be reported as *changed*
42+
Two messages sharing that key are the "same warning" — if they differ only
43+
in line numbers or message text, they should be reported as *changed*
4444
rather than as a separate removal + addition. Leftovers on each side are
4545
genuinely missing or genuinely new.
46-
47-
Returns ``(paired, unmatched_old, unmatched_new)``.
4846
"""
49-
old_by_key: dict[tuple[str, str, str], list[JSONMessage]] = defaultdict(list)
5047
new_by_key: dict[tuple[str, str, str], list[JSONMessage]] = defaultdict(list)
51-
for m in old_residuals:
52-
old_by_key[m["symbol"], m["path"], m["obj"]].append(m)
5348
for m in new_residuals:
5449
new_by_key[m["symbol"], m["path"], m["obj"]].append(m)
5550

5651
paired: list[ChangedMessage] = []
57-
paired_old_ids: set[int] = set()
58-
paired_new_ids: set[int] = set()
59-
for key in old_by_key:
60-
if key not in new_by_key:
61-
continue
62-
for old, new in zip(old_by_key[key], new_by_key[key]):
52+
final_missing: list[JSONMessage] = []
53+
matched_new: set[int] = set()
54+
for old in old_residuals:
55+
bucket = new_by_key.get((old["symbol"], old["path"], old["obj"]))
56+
if bucket:
57+
new = bucket.pop(0)
6358
paired.append(ChangedMessage(old=old, new=new))
64-
paired_old_ids.add(id(old))
65-
paired_new_ids.add(id(new))
66-
67-
final_missing = [m for m in old_residuals if id(m) not in paired_old_ids]
68-
final_new = [m for m in new_residuals if id(m) not in paired_new_ids]
59+
matched_new.add(id(new))
60+
else:
61+
final_missing.append(old)
62+
final_new = [m for m in new_residuals if id(m) not in matched_new]
6963
return paired, final_missing, final_new
7064

7165

@@ -88,7 +82,7 @@ def message_diff(change: ChangedMessage) -> str:
8882
"""
8983
old, new = change.old, change.new
9084
changed_keys: set[str] = set()
91-
for key in set(old) | set(new):
85+
for key in old.keys() | new.keys():
9286
if old.get(key) != new.get(key):
9387
changed_keys.add(key)
9488

0 commit comments

Comments
 (0)