Skip to content

Commit 3c6ecaa

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 3c6ecaa

3 files changed

Lines changed: 28 additions & 22 deletions

File tree

pylint/testutils/_primer/comparator.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,38 @@
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."""
2520

26-
def __iter__(self) -> Generator[tuple[str, PackageData, PackageData]]:
21+
def __init__(self, main_data: PackageMessages, pr_data: PackageMessages) -> None:
22+
self._main_data = main_data
23+
self._pr_data = pr_data
24+
25+
@staticmethod
26+
def from_json(
27+
base_file: Path | str, new_file: Path | str, batches: int | None = None
28+
) -> Comparator:
29+
"""Build a Comparator from JSON file paths, handling batched runs."""
2730
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+
pr_data: PackageMessages
32+
if batches is None:
33+
main_data = Comparator._load_json(base_file)
34+
pr_data = Comparator._load_json(new_file)
3135
else:
3236
main_data = {}
3337
pr_data = {}
34-
for idx in range(self._batches):
38+
for idx in range(batches):
39+
suffix = f"batch{idx}"
3540
main_data.update(
36-
self._load_json(
37-
self._base_file.replace("BATCHIDX", "batch" + str(idx))
38-
)
41+
Comparator._load_json(str(base_file).replace("BATCHIDX", suffix))
3942
)
4043
pr_data.update(
41-
self._load_json(
42-
self._new_file.replace("BATCHIDX", "batch" + str(idx))
43-
)
44+
Comparator._load_json(str(new_file).replace("BATCHIDX", suffix))
4445
)
46+
return Comparator(main_data, pr_data)
47+
48+
def __iter__(self) -> Generator[tuple[str, PackageData, PackageData]]:
49+
main_data = self._main_data
50+
pr_data = self._pr_data
4551

4652
missing_messages: PackageMessages = {}
4753
for package, data in main_data.items():

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)