From 0915546720563f8de4e0df7c35aff67915509235 Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Mon, 20 Jun 2022 13:17:19 +0200 Subject: [PATCH] [primer] Create a class for easier comparison of pylint results --- pylint/testutils/_primer/comparator.py | 41 +++++++++++++++++ pylint/testutils/_primer/primer_command.py | 3 +- .../_primer/primer_compare_command.py | 45 +++---------------- 3 files changed, 50 insertions(+), 39 deletions(-) create mode 100644 pylint/testutils/_primer/comparator.py diff --git a/pylint/testutils/_primer/comparator.py b/pylint/testutils/_primer/comparator.py new file mode 100644 index 00000000000..03b3fcfc379 --- /dev/null +++ b/pylint/testutils/_primer/comparator.py @@ -0,0 +1,41 @@ +# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html +# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE +# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt + +from __future__ import annotations + +import json +from collections.abc import Generator +from pathlib import Path + +from pylint.testutils._primer.primer_command import Messages, PackageMessages + + +class Comparator: + def __init__(self, main_json: Path, pr_json: Path): + main_messages = self._load_json(main_json) + self.pr_messages = self._load_json(pr_json) + self.missing_messages: PackageMessages = {} + for package, messages in main_messages.items(): + self.missing_messages[package] = [] + for message in messages: + try: + self.pr_messages[package].remove(message) + except ValueError: + self.missing_messages[package].append(message) + + def __iter__( + self, + ) -> Generator[tuple[str, Messages, Messages], None, None]: + for package, missing_messages in self.missing_messages.items(): + new_messages = self.pr_messages[package] + if not missing_messages and not new_messages: + print(f"PRIMER: No changes in {package}.") + continue + yield package, missing_messages, new_messages + + @staticmethod + def _load_json(file_path: Path | str) -> PackageMessages: + with open(file_path, encoding="utf-8") as f: + result: PackageMessages = json.load(f) + return result diff --git a/pylint/testutils/_primer/primer_command.py b/pylint/testutils/_primer/primer_command.py index b3a39445f9e..7c22e3cc712 100644 --- a/pylint/testutils/_primer/primer_command.py +++ b/pylint/testutils/_primer/primer_command.py @@ -11,7 +11,8 @@ from pylint.testutils._primer import PackageToLint -PackageMessages = Dict[str, List[Dict[str, Union[str, int]]]] +Messages = List[Dict[str, Union[str, int]]] +PackageMessages = Dict[str, Messages] class PrimerCommand: diff --git a/pylint/testutils/_primer/primer_compare_command.py b/pylint/testutils/_primer/primer_compare_command.py index 48953b7f1f9..b9b1f4cd2ed 100644 --- a/pylint/testutils/_primer/primer_compare_command.py +++ b/pylint/testutils/_primer/primer_compare_command.py @@ -3,55 +3,24 @@ # Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt from __future__ import annotations -import json -from pathlib import Path - -from pylint.testutils._primer.primer_command import PackageMessages, PrimerCommand +from pylint.testutils._primer.comparator import Comparator +from pylint.testutils._primer.primer_command import Messages, PrimerCommand MAX_GITHUB_COMMENT_LENGTH = 65536 class CompareCommand(PrimerCommand): def run(self) -> None: - main_messages = self._load_json(self.config.base_file) - pr_messages = self._load_json(self.config.new_file) - missing_messages, new_messages = self._cross_reference( - main_messages, pr_messages - ) - comment = self._create_comment(missing_messages, new_messages) + comparator = Comparator(self.config.base_file, self.config.new_file) + comment = self._create_comment(comparator) with open(self.primer_directory / "comment.txt", "w", encoding="utf-8") as f: f.write(comment) - @staticmethod - def _cross_reference( - main_dict: PackageMessages, pr_messages: PackageMessages - ) -> tuple[PackageMessages, PackageMessages]: - missing_messages: PackageMessages = {} - for package, messages in main_dict.items(): - missing_messages[package] = [] - for message in messages: - try: - pr_messages[package].remove(message) - except ValueError: - missing_messages[package].append(message) - return missing_messages, pr_messages - - @staticmethod - def _load_json(file_path: Path | str) -> PackageMessages: - with open(file_path, encoding="utf-8") as f: - result: PackageMessages = json.load(f) - return result - - def _create_comment( - self, all_missing_messages: PackageMessages, all_new_messages: PackageMessages - ) -> str: + def _create_comment(self, comparator: Comparator) -> str: comment = "" - for package, missing_messages in all_missing_messages.items(): + for package, missing_messages, new_messages in comparator: if len(comment) >= MAX_GITHUB_COMMENT_LENGTH: break - new_messages = all_new_messages[package] - if not missing_messages and not new_messages: - continue comment += self._create_comment_for_package( package, new_messages, missing_messages ) @@ -67,7 +36,7 @@ def _create_comment( return self._truncate_comment(comment) def _create_comment_for_package( - self, package: str, new_messages, missing_messages + self, package: str, new_messages: Messages, missing_messages: Messages ) -> str: comment = f"\n\n**Effect on [{package}]({self.packages[package].url}):**\n" # Create comment for new messages