summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2021-03-07 10:50:43 +0100
committerPierre Sassoulas <pierre.sassoulas@gmail.com>2021-11-25 15:58:40 +0100
commitc985e81cc52b262749f65916ee8c9201a0791b7e (patch)
treeb873f7be2c6627125460ba0f3c23eff9da29662f
parent07a635cd2109bb4a44bd29078a0b5f9fc6f6bd5d (diff)
downloadpylint-git-c985e81cc52b262749f65916ee8c9201a0791b7e.tar.gz
Fix exporting to JSON does not honor score option (3504)
This is a fix but I wonder... This looks like the fabled bug that will break countless integration by being fixed. `--score=y` is the default setting, and json is probably a prime candidate for automation. Is it reasonable to fix it and ask everyone to change their option to `-s n`? I'm pretty sure no one read the pylint's changelog unless we break their build.
-rw-r--r--ChangeLog13
-rw-r--r--doc/whatsnew/3.0.rst24
-rw-r--r--doc/whatsnew/index.rst1
-rw-r--r--pylint/lint/pylinter.py10
-rw-r--r--pylint/reporters/json_reporter.py8
-rw-r--r--tests/test_self.py5
-rw-r--r--tests/unittest_reporters_json.py15
7 files changed, 66 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index 26b63703e..b81fcea04 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,19 @@
Pylint's ChangeLog
------------------
+What's New in Pylint 3.0.0?
+===========================
+
+..
+ Put breaking changes here and also in 'doc/whatsnew/3.0.rst'
+
+* Fix the score option not being honored when exporting to JSON
+
+ The default setting was to have a score, but it did not work. Now it will give a score at the end of the json:
+
+ Close #3504
+
+
What's New in Pylint 2.13.0?
============================
Release date: TBA
diff --git a/doc/whatsnew/3.0.rst b/doc/whatsnew/3.0.rst
new file mode 100644
index 000000000..6c3a92ffd
--- /dev/null
+++ b/doc/whatsnew/3.0.rst
@@ -0,0 +1,24 @@
+**************************
+ What's New in Pylint 3.0
+**************************
+
+:Release: 3.0
+:Date: TBA
+
+
+Summary -- Release highlights
+=============================
+
+
+New checkers
+============
+
+
+Other Changes
+=============
+
+* The score option for JSON export has been fixed
+
+ The default setting is to have a score, but it did not work before.
+ If a score appeared in your json, and you want to go back what you
+ had before, you must change the score option to "--score=n"
diff --git a/doc/whatsnew/index.rst b/doc/whatsnew/index.rst
index d2c14ccbc..d1b25d96e 100644
--- a/doc/whatsnew/index.rst
+++ b/doc/whatsnew/index.rst
@@ -9,6 +9,7 @@ High level descriptions of the most important changes between major Pylint versi
.. toctree::
:maxdepth: 1
+ 3.0.rst
2.13.rst
2.12.rst
2.11.rst
diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py
index b9bea1bb2..fc4884355 100644
--- a/pylint/lint/pylinter.py
+++ b/pylint/lint/pylinter.py
@@ -1266,13 +1266,9 @@ class PyLinter(
self._ignore_paths = get_global_option(self, "ignore-paths")
def generate_reports(self):
- """close the whole package /module, it's time to make reports !
-
- if persistent run, pickle results for later comparison
- """
- # Display whatever messages are left on the reporter.
- self.reporter.display_messages(report_nodes.Section())
+ """Close the whole package /module, it's time to make reports !
+ if persistent run, pickle results for later comparison."""
if self.file_state.base_name is not None:
# load previous results if any
previous_stats = config.load_results(self.file_state.base_name)
@@ -1291,6 +1287,8 @@ class PyLinter(
else:
self.reporter.on_close(self.stats, LinterStats())
score_value = None
+ # Display whatever messages are left on the reporter.
+ self.reporter.display_messages(report_nodes.Section())
return score_value
def _report_evaluation(self):
diff --git a/pylint/reporters/json_reporter.py b/pylint/reporters/json_reporter.py
index 8c43b0a3b..e58217d09 100644
--- a/pylint/reporters/json_reporter.py
+++ b/pylint/reporters/json_reporter.py
@@ -13,11 +13,13 @@
# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
"""JSON reporter"""
+import io
import json
from typing import TYPE_CHECKING, Optional
from pylint.interfaces import IReporter
from pylint.reporters.base_reporter import BaseReporter
+from pylint.reporters.ureports.text_writer import TextWriter
if TYPE_CHECKING:
from pylint.lint.pylinter import PyLinter
@@ -50,7 +52,11 @@ class JSONReporter(BaseReporter):
print(json.dumps(json_dumpable, indent=4), file=self.out)
def display_reports(self, layout: "Section") -> None:
- """Don't do anything in this reporter."""
+ output = io.StringIO()
+ TextWriter().format(layout, output)
+ score = output.getvalue().split("Your")[1]
+ score = score.split(r"/10")[0]
+ self.messages.append({"score": f"Your{score}/10"})
def _display(self, layout: "Section") -> None:
"""Do nothing."""
diff --git a/tests/test_self.py b/tests/test_self.py
index 77407ad62..0ca6c4bc4 100644
--- a/tests/test_self.py
+++ b/tests/test_self.py
@@ -426,7 +426,7 @@ class TestRunTC:
self._runtest([module], code=4, reporter=JSONReporter(out))
output = json.loads(out.getvalue())
assert isinstance(output, list)
- assert len(output) == 1
+ assert len(output) == 2
assert isinstance(output[0], dict)
expected = {
"symbol": "unused-variable",
@@ -437,10 +437,13 @@ class TestRunTC:
"line": 4,
"type": "warning",
}
+
message = output[0]
for key, value in expected.items():
assert key in message
assert message[key] == value
+ expected = {"score": "Your code has been rated at 7.50/10"}
+ assert output[-1] == expected
def test_information_category_disabled_by_default(self) -> None:
expected = "Your code has been rated at 10.00/10"
diff --git a/tests/unittest_reporters_json.py b/tests/unittest_reporters_json.py
index ed6ed74a4..12e1ab0c6 100644
--- a/tests/unittest_reporters_json.py
+++ b/tests/unittest_reporters_json.py
@@ -24,7 +24,7 @@ from pylint.lint import PyLinter
from pylint.reporters import JSONReporter
from pylint.reporters.ureports.nodes import EvaluationSection
-expected_score_message = "Expected score message"
+expected_score_message = "Your code has been rated at 7.50/10"
expected_result = [
[
("column", 0),
@@ -40,6 +40,14 @@ expected_result = [
]
+def test_simple_json_output_with_score() -> None:
+ report = get_linter_result(score=True)
+ assert len(report) == 2
+ report_result = [sorted(report[0].items(), key=lambda item: item[0])]
+ assert report_result == expected_result
+ assert report[1] == {"score": expected_score_message}
+
+
def test_simple_json_output_no_score() -> None:
report = get_linter_result(score=False)
assert len(report) == 1
@@ -59,7 +67,10 @@ def get_linter_result(score: bool) -> List[Dict[str, Any]]:
linter.add_message("line-too-long", line=1, args=(1, 2))
# we call those methods because we didn't actually run the checkers
if score:
- reporter.display_reports(EvaluationSection(expected_score_message))
+ generated_msg = "-------------------------------------\r\n{}\r\n".format(
+ expected_score_message
+ )
+ reporter.display_reports(EvaluationSection(generated_msg))
reporter.display_messages(None)
report_result = json.loads(output.getvalue())
return report_result