Skip to content

Commit

Permalink
[primer] Create a class for easier comparison of pylint results
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Jun 20, 2022
1 parent 406afd5 commit 0915546
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 39 deletions.
41 changes: 41 additions & 0 deletions pylint/testutils/_primer/comparator.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion pylint/testutils/_primer/primer_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
45 changes: 7 additions & 38 deletions pylint/testutils/_primer/primer_compare_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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
Expand Down

0 comments on commit 0915546

Please sign in to comment.