Skip to content

Commit 46b050d

Browse files
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.
1 parent 1f7b77b commit 46b050d

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
Pylint's ChangeLog
33
------------------
44

5+
What's New in Pylint 3.0.0?
6+
===========================
7+
8+
* Fix the score option not being honored when exporting to JSON
9+
10+
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:
11+
12+
Close #3504
13+
14+
515
What's New in Pylint 2.8.0?
616
===========================
717
Release date: TBA

doc/whatsnew/3.0.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
**************************
2+
What's New in Pylint 3.0
3+
**************************
4+
5+
:Release: 3.0
6+
:Date: TBA
7+
8+
9+
Summary -- Release highlights
10+
=============================
11+
12+
13+
New checkers
14+
============
15+
16+
17+
Other Changes
18+
=============
19+
20+
* The score option for JSON export has been fixed
21+
22+
The default setting is to have a score, but it did not work before.
23+
If a score appeared in your json, and you want to go back what you
24+
had before, you must change the score option to "--score=n"

doc/whatsnew/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ High level descriptions of the most important changes between major Pylint versi
99
.. toctree::
1010
:maxdepth: 1
1111

12+
3.0.rst
1213
2.8.rst
1314
2.7.rst
1415
2.6.rst

pylint/lint/pylinter.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,13 +1111,9 @@ def open(self):
11111111
self.stats[msg_cat] = 0
11121112

11131113
def generate_reports(self):
1114-
"""close the whole package /module, it's time to make reports !
1115-
1116-
if persistent run, pickle results for later comparison
1117-
"""
1118-
# Display whatever messages are left on the reporter.
1119-
self.reporter.display_messages(report_nodes.Section())
1114+
"""Close the whole package /module, it's time to make reports !
11201115
1116+
if persistent run, pickle results for later comparison."""
11211117
if self.file_state.base_name is not None:
11221118
# load previous results if any
11231119
previous_stats = config.load_results(self.file_state.base_name)
@@ -1136,6 +1132,8 @@ def generate_reports(self):
11361132
else:
11371133
self.reporter.on_close(self.stats, {})
11381134
score_value = None
1135+
# Display whatever messages are left on the reporter.
1136+
self.reporter.display_messages(report_nodes.Section())
11391137
return score_value
11401138

11411139
def _report_evaluation(self):

pylint/reporters/json_reporter.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
# For details: https://github.com/PyCQA/pylint/blob/master/COPYING
1212

1313
"""JSON reporter"""
14+
import io
1415
import json
1516
import sys
1617

1718
from pylint.interfaces import IReporter
1819
from pylint.reporters.base_reporter import BaseReporter
20+
from pylint.reporters.ureports.text_writer import TextWriter
1921

2022

2123
class JSONReporter(BaseReporter):
@@ -50,7 +52,11 @@ def display_messages(self, layout):
5052
print(json.dumps(self.messages, indent=4), file=self.out)
5153

5254
def display_reports(self, layout):
53-
"""Don't do anything in this reporter."""
55+
output = io.StringIO()
56+
TextWriter().format(layout, output)
57+
score = output.getvalue().split("Your")[1]
58+
score = score.split(r"/10")[0]
59+
self.messages.append({"score": f"Your{score}/10"})
5460

5561
def _display(self, layout):
5662
"""Do nothing."""

tests/test_self.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def test_json_report_does_not_escape_quotes(self):
415415
self._runtest([module], code=4, reporter=JSONReporter(out))
416416
output = json.loads(out.getvalue())
417417
assert isinstance(output, list)
418-
assert len(output) == 1
418+
assert len(output) == 2
419419
assert isinstance(output[0], dict)
420420
expected = {
421421
"symbol": "unused-variable",
@@ -426,10 +426,13 @@ def test_json_report_does_not_escape_quotes(self):
426426
"line": 4,
427427
"type": "warning",
428428
}
429+
429430
message = output[0]
430431
for key, value in expected.items():
431432
assert key in message
432433
assert message[key] == value
434+
expected = {"score": "Your code has been rated at 7.50/10"}
435+
assert output[-1] == expected
433436

434437
def test_information_category_disabled_by_default(self):
435438
expected = "Your code has been rated at 10.00/10"

tests/unittest_reporters_json.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from pylint.reporters import JSONReporter
2222
from pylint.reporters.ureports.nodes import EvaluationSection
2323

24-
expected_score_message = "Expected score message"
24+
expected_score_message = "Your code has been rated at 7.50/10"
2525
expected_result = [
2626
[
2727
("column", 0),
@@ -37,6 +37,14 @@
3737
]
3838

3939

40+
def test_simple_json_output_with_score():
41+
report = get_linter_result(score=True)
42+
assert len(report) == 2
43+
report_result = [sorted(report[0].items(), key=lambda item: item[0])]
44+
assert report_result == expected_result
45+
assert report[1] == {"score": expected_score_message}
46+
47+
4048
def test_simple_json_output_no_score():
4149
report = get_linter_result(score=False)
4250
assert len(report) == 1
@@ -56,7 +64,10 @@ def get_linter_result(score):
5664
linter.add_message("line-too-long", line=1, args=(1, 2))
5765
# we call those methods because we didn't actually run the checkers
5866
if score:
59-
reporter.display_reports(EvaluationSection(expected_score_message))
67+
generated_msg = "-------------------------------------\r\n{}\r\n".format(
68+
expected_score_message
69+
)
70+
reporter.display_reports(EvaluationSection(generated_msg))
6071
reporter.display_messages(None)
6172
report_result = json.loads(output.getvalue())
6273
return report_result

0 commit comments

Comments
 (0)