summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPierre Sassoulas <pierre.sassoulas@gmail.com>2021-10-27 11:31:33 +0200
committerGitHub <noreply@github.com>2021-10-27 11:31:33 +0200
commit1f77ff6135bc280a6cfd46fc3bd917d3436d55e2 (patch)
treea8619e4001e1b4d8bf2650b876caecdfa2e8766e
parent4007c8b3eb0b02c01332b191755cb12390c16eef (diff)
downloadpylint-git-1f77ff6135bc280a6cfd46fc3bd917d3436d55e2.tar.gz
Fix the deprecation of set_output in our code (#5209)
* Fix the deprecation of set_output in our code * Add a test for the deprecation warning itself * Refactor the set_output from the MultiReporter We can remove set_output declaration because the function was already failing with a NotImplementedError before, so removing it make it fail another way
-rw-r--r--pylint/lint/pylinter.py5
-rw-r--r--pylint/lint/run.py2
-rw-r--r--pylint/reporters/multi_reporter.py26
-rw-r--r--tests/unittest_reporting.py18
4 files changed, 31 insertions, 20 deletions
diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py
index ae2126c09..ede27d1e8 100644
--- a/pylint/lint/pylinter.py
+++ b/pylint/lint/pylinter.py
@@ -618,16 +618,13 @@ class PyLinter(
reporter = self._load_reporter_by_name(reporter_name)
sub_reporters.append(reporter)
-
if reporter_output:
(reporter_output,) = reporter_output
-
# pylint: disable=consider-using-with
output_file = stack.enter_context(
open(reporter_output, "w", encoding="utf-8")
)
-
- reporter.set_output(output_file)
+ reporter.out = output_file
output_files.append(output_file)
# Extend the lifetime of all opened output files
diff --git a/pylint/lint/run.py b/pylint/lint/run.py
index 4348fd721..22c3dd14f 100644
--- a/pylint/lint/run.py
+++ b/pylint/lint/run.py
@@ -377,7 +377,7 @@ to search for configuration file.
if self._output:
try:
with open(self._output, "w", encoding="utf-8") as output:
- linter.reporter.set_output(output)
+ linter.reporter.out = output
linter.check(args)
score_value = linter.generate_reports()
except OSError as ex:
diff --git a/pylint/reporters/multi_reporter.py b/pylint/reporters/multi_reporter.py
index a6be1c1b8..f674a29e4 100644
--- a/pylint/reporters/multi_reporter.py
+++ b/pylint/reporters/multi_reporter.py
@@ -38,11 +38,24 @@ class MultiReporter:
):
self._sub_reporters = sub_reporters
self.close_output_files = close_output_files
-
self._path_strip_prefix = os.getcwd() + os.sep
self._linter: Optional[PyLinter] = None
+ self.out = output
- self.set_output(output)
+ @property
+ def out(self):
+ return self.__out
+
+ @out.setter
+ def out(self, output: Optional[AnyFile] = None):
+ """
+ MultiReporter doesn't have it's own output. This method is only
+ provided for API parity with BaseReporter and should not be called
+ with non-None values for 'output'.
+ """
+ self.__out = None
+ if output is not None:
+ raise NotImplementedError("MultiReporter does not support direct output.")
def __del__(self) -> None:
self.close_output_files()
@@ -66,15 +79,6 @@ class MultiReporter:
for rep in self._sub_reporters:
rep.handle_message(msg)
- # pylint: disable=no-self-use
- def set_output(self, output: Optional[AnyFile] = None) -> None:
- """set output stream"""
- # MultiReporter doesn't have it's own output. This method is only
- # provided for API parity with BaseReporter and should not be called
- # with non-None values for 'output'.
- if output is not None:
- raise NotImplementedError("MultiReporter does not support direct output.")
-
def writeln(self, string: str = "") -> None:
"""write a line in the output buffer"""
for rep in self._sub_reporters:
diff --git a/tests/unittest_reporting.py b/tests/unittest_reporting.py
index c0fdb61d4..7400be9e5 100644
--- a/tests/unittest_reporting.py
+++ b/tests/unittest_reporting.py
@@ -16,7 +16,7 @@
# 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
# pylint: disable=redefined-outer-name
-
+import sys
import warnings
from contextlib import redirect_stdout
from io import StringIO
@@ -48,7 +48,7 @@ def disable():
def test_template_option(linter):
output = StringIO()
- linter.reporter.set_output(output)
+ linter.reporter.out = output
linter.set_option("msg-template", "{msg_id}:{line:03d}")
linter.open()
linter.set_current_module("0123")
@@ -57,6 +57,16 @@ def test_template_option(linter):
assert output.getvalue() == "************* Module 0123\nC0301:001\nC0301:002\n"
+def test_deprecation_set_output(recwarn):
+ """TODO remove in 3.0""" # pylint: disable=fixme
+ reporter = BaseReporter()
+ # noinspection PyDeprecation
+ reporter.set_output(sys.stdout)
+ warning = recwarn.pop()
+ assert "set_output' will be removed in 3.0" in str(warning)
+ assert reporter.out == sys.stdout
+
+
def test_parseable_output_deprecated():
with warnings.catch_warnings(record=True) as cm:
warnings.simplefilter("always")
@@ -73,7 +83,7 @@ def test_parseable_output_regression():
checkers.initialize(linter)
linter.config.persistent = 0
- linter.reporter.set_output(output)
+ linter.reporter.out = output
linter.set_option("output-format", "parseable")
linter.open()
linter.set_current_module("0123")
@@ -122,7 +132,7 @@ def test_multi_format_output(tmp_path):
assert linter.reporter.linter is linter
with pytest.raises(NotImplementedError):
- linter.reporter.set_output(text)
+ linter.reporter.out = text
linter.open()
linter.check_single_file_item(FileItem("somemodule", source_file, "somemodule"))