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-04-24 21:43:32 +0200
commit6206e33a59f1e5483035f5cd59f8da2855fb27ca (patch)
tree941ff3dbd1a5dacc947e0e5f94acd6c0b5478ce9
parent7dece5b2302f446d7d63829a34b1253ae6e32e6b (diff)
downloadpylint-git-6206e33a59f1e5483035f5cd59f8da2855fb27ca.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--ChangeLog9
-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, 62 insertions, 10 deletions
diff --git a/ChangeLog b/ChangeLog
index e07ba1f58..dcb74517b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,6 +2,15 @@
Pylint's ChangeLog
------------------
+What's New in Pylint 3.0.0?
+===========================
+
+* 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
+
..
Put new features and bugfixes here and also in 'doc/whatsnew/2.8.rst'
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 548962f48..cee75665a 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.8.rst
2.7.rst
2.6.rst
diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py
index a151c4451..17b833c7b 100644
--- a/pylint/lint/pylinter.py
+++ b/pylint/lint/pylinter.py
@@ -1128,13 +1128,9 @@ class PyLinter(
self.stats[msg_cat] = 0
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)
@@ -1153,6 +1149,8 @@ class PyLinter(
else:
self.reporter.on_close(self.stats, {})
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 1a53f5f3d..bb3711917 100644
--- a/pylint/reporters/json_reporter.py
+++ b/pylint/reporters/json_reporter.py
@@ -11,11 +11,13 @@
# For details: https://github.com/PyCQA/pylint/blob/master/LICENSE
"""JSON reporter"""
+import io
import json
import sys
from pylint.interfaces import IReporter
from pylint.reporters.base_reporter import BaseReporter
+from pylint.reporters.ureports.text_writer import TextWriter
class JSONReporter(BaseReporter):
@@ -50,7 +52,11 @@ class JSONReporter(BaseReporter):
print(json.dumps(self.messages, indent=4), file=self.out)
def display_reports(self, layout):
- """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):
"""Do nothing."""
diff --git a/tests/test_self.py b/tests/test_self.py
index 59f302e9a..b74cd5585 100644
--- a/tests/test_self.py
+++ b/tests/test_self.py
@@ -430,7 +430,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",
@@ -441,10 +441,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):
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 11d37760e..a87f5cef3 100644
--- a/tests/unittest_reporters_json.py
+++ b/tests/unittest_reporters_json.py
@@ -21,7 +21,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),
@@ -37,6 +37,14 @@ expected_result = [
]
+def test_simple_json_output_with_score():
+ 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():
report = get_linter_result(score=False)
assert len(report) == 1
@@ -56,7 +64,10 @@ def get_linter_result(score):
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