Skip to content

Commit a67c1f5

Browse files
[primer] Separate Comparator data loading from comparison logic
Constructor now takes pre-loaded PackageMessages dicts, making it directly testable with in-memory data. I/O and batch merging move to a from_json() factory method; _load_json() becomes module-level.
1 parent a5fd825 commit a67c1f5

3 files changed

Lines changed: 35 additions & 33 deletions

File tree

pylint/testutils/_primer/comparator.py

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,15 @@
1616

1717

1818
class Comparator:
19-
def __init__(
20-
self, base_file: str, new_file: str, batches: int | None = None
21-
) -> None:
22-
self._base_file = base_file
23-
self._new_file = new_file
24-
self._batches = batches
19+
"""Cross-reference two primer JSON outputs and iterate over differences."""
20+
21+
def __init__(self, main_data: PackageMessages, pr_data: PackageMessages) -> None:
22+
self._main_data = main_data
23+
self._pr_data = pr_data
2524

2625
def __iter__(self) -> Generator[tuple[str, PackageData, PackageData]]:
27-
main_data: PackageMessages
28-
if self._batches is None:
29-
main_data = self._load_json(self._base_file)
30-
pr_data = self._load_json(self._new_file)
31-
else:
32-
main_data = {}
33-
pr_data = {}
34-
for idx in range(self._batches):
35-
main_data.update(
36-
self._load_json(
37-
self._base_file.replace("BATCHIDX", "batch" + str(idx))
38-
)
39-
)
40-
pr_data.update(
41-
self._load_json(
42-
self._new_file.replace("BATCHIDX", "batch" + str(idx))
43-
)
44-
)
26+
main_data = self._main_data
27+
pr_data = self._pr_data
4528

4629
missing_messages: PackageMessages = {}
4730
for package, data in main_data.items():
@@ -63,7 +46,26 @@ def __iter__(self) -> Generator[tuple[str, PackageData, PackageData]]:
6346
yield package, pkg_missing, new_messages
6447

6548
@staticmethod
66-
def _load_json(file_path: Path | str) -> PackageMessages:
67-
with open(file_path, encoding="utf-8") as f:
68-
result: PackageMessages = json.load(f)
69-
return result
49+
def from_json(
50+
base_file: Path | str, new_file: Path | str, batches: int | None = None
51+
) -> Comparator:
52+
"""Build a Comparator from JSON file paths, handling batched runs."""
53+
main_data: PackageMessages
54+
pr_data: PackageMessages
55+
if batches is None:
56+
main_data = _load_json(base_file)
57+
pr_data = _load_json(new_file)
58+
else:
59+
main_data = {}
60+
pr_data = {}
61+
for idx in range(batches):
62+
suffix = f"batch{idx}"
63+
main_data.update(_load_json(str(base_file).replace("BATCHIDX", suffix)))
64+
pr_data.update(_load_json(str(new_file).replace("BATCHIDX", suffix)))
65+
return Comparator(main_data, pr_data)
66+
67+
68+
def _load_json(file_path: Path | str) -> PackageMessages:
69+
with open(file_path, encoding="utf-8") as f:
70+
result: PackageMessages = json.load(f)
71+
return result

pylint/testutils/_primer/primer_compare_command.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class CompareCommand(PrimerCommand):
1515
def run(self) -> None:
16-
comparator = Comparator(
16+
comparator = Comparator.from_json(
1717
self.config.base_file, self.config.new_file, self.config.batches
1818
)
1919
comment = self._create_comment(comparator)

tests/testutils/_primer/test_comparator.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
)
1919
def test_comparator(directory: Path) -> None:
2020
"""Test Comparator with each fixture directory."""
21-
comparator = Comparator(str(directory / "main.json"), str(directory / "pr.json"))
21+
comparator = Comparator.from_json(directory / "main.json", directory / "pr.json")
2222
expected = json.loads((directory / "expected_comparator.json").read_text("utf-8"))
2323
results = list(comparator)
2424
assert len(results) == len(expected)
@@ -30,9 +30,9 @@ def test_comparator(directory: Path) -> None:
3030

3131
def test_comparator_batched() -> None:
3232
fixture = Path(__file__).parent / "batched_cases"
33-
comparator = Comparator(
34-
str(fixture / "main_BATCHIDX.json"),
35-
str(fixture / "pr_BATCHIDX.json"),
33+
comparator = Comparator.from_json(
34+
fixture / "main_BATCHIDX.json",
35+
fixture / "pr_BATCHIDX.json",
3636
batches=2,
3737
)
3838
expected = json.loads((fixture / "expected_comparator.json").read_text("utf-8"))

0 commit comments

Comments
 (0)