diff options
author | Daniƫl van Noord <13665637+DanielNoord@users.noreply.github.com> | 2021-12-03 20:15:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-03 20:15:12 +0100 |
commit | 82f0f88c1807eb94eeab2d7670bf930f1850dedd (patch) | |
tree | e2c4a7114d9bd1c09bbc1b59d1273a32db29aea2 | |
parent | 17a16d79ebcca54284db516651a07f61bdf17bbc (diff) | |
download | pylint-git-82f0f88c1807eb94eeab2d7670bf930f1850dedd.tar.gz |
Add typing to ``PyLinter.reporter`` (#5325)
* Add ``messages`` attribute to ``MultiReporter``
* Make ``PyLinter`` always have a ``reporter`` attribute
Co-authored-by: Pierre Sassoulas <pierre.sassoulas@gmail.com>
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | doc/whatsnew/2.13.rst | 3 | ||||
-rw-r--r-- | pylint/lint/pylinter.py | 29 | ||||
-rw-r--r-- | pylint/reporters/multi_reporter.py | 1 | ||||
-rw-r--r-- | tests/test_self.py | 7 | ||||
-rw-r--r-- | tests/unittest_reporting.py | 2 |
6 files changed, 34 insertions, 11 deletions
@@ -27,6 +27,9 @@ Release date: TBA Insert your changelog randomly, it will reduce merge conflicts (Ie. not necessarily at the end) +* The ``PyLinter`` class will now be initialiazed with a ``TextReporter`` + as its reporter if none is provided. + What's New in Pylint 2.12.2? ============================ diff --git a/doc/whatsnew/2.13.rst b/doc/whatsnew/2.13.rst index 8222813d2..0713e343a 100644 --- a/doc/whatsnew/2.13.rst +++ b/doc/whatsnew/2.13.rst @@ -34,3 +34,6 @@ Other Changes * Require Python ``3.6.2`` to run pylint. Closes #5065 + +* The ``PyLinter`` class will now be initialiazed with a ``TextReporter`` + as its reporter if none is provided. diff --git a/pylint/lint/pylinter.py b/pylint/lint/pylinter.py index c95aa680e..3617d9141 100644 --- a/pylint/lint/pylinter.py +++ b/pylint/lint/pylinter.py @@ -39,6 +39,7 @@ from pylint.lint.utils import ( prepare_crash_report, ) from pylint.message import Message, MessageDefinition, MessageDefinitionStore +from pylint.reporters.text import TextReporter from pylint.reporters.ureports import nodes as report_nodes from pylint.typing import ( FileItem, @@ -517,13 +518,25 @@ class PyLinter( ("Reports", "Options related to output formatting and reporting"), ) - def __init__(self, options=(), reporter=None, option_groups=(), pylintrc=None): + def __init__( + self, + options=(), + reporter=None, + option_groups=(), + pylintrc=None, + ): """Some stuff has to be done before ancestors initialization... messages store / checkers / reporter / astroid manager""" - self.msgs_store = MessageDefinitionStore() - self.reporter = None + # Attributes for reporters + self.reporter: Union[reporters.BaseReporter, reporters.MultiReporter] + if reporter: + self.set_reporter(reporter) + else: + self.set_reporter(TextReporter()) self._reporter_names = None self._reporters = {} + + self.msgs_store = MessageDefinitionStore() self._checkers = collections.defaultdict(list) self._pragma_lineno = {} self._ignore_file = False @@ -572,16 +585,10 @@ class PyLinter( self._dynamic_plugins = set() self._error_mode = False self.load_provider_defaults() - if reporter: - self.set_reporter(reporter) def load_default_plugins(self): checkers.initialize(self) reporters.initialize(self) - # Make sure to load the default reporter, because - # the option has been set before the plugins had been loaded. - if not self.reporter: - self._load_reporters() def load_plugin_modules(self, modnames): """take a list of module names which are pylint plugins and load @@ -654,7 +661,9 @@ class PyLinter( else: return reporter_class() - def set_reporter(self, reporter): + def set_reporter( + self, reporter: Union[reporters.BaseReporter, reporters.MultiReporter] + ) -> None: """set the reporter used to display messages and reports""" self.reporter = reporter reporter.linter = self diff --git a/pylint/reporters/multi_reporter.py b/pylint/reporters/multi_reporter.py index f674a29e4..cbdc36962 100644 --- a/pylint/reporters/multi_reporter.py +++ b/pylint/reporters/multi_reporter.py @@ -41,6 +41,7 @@ class MultiReporter: self._path_strip_prefix = os.getcwd() + os.sep self._linter: Optional[PyLinter] = None self.out = output + self.messages: List[Message] = [] @property def out(self): diff --git a/tests/test_self.py b/tests/test_self.py index 41bf423ec..ebdb91443 100644 --- a/tests/test_self.py +++ b/tests/test_self.py @@ -1261,6 +1261,13 @@ class TestRunTC: assert sorted(plugins) == sorted(runner.linter._dynamic_plugins) @staticmethod + def test_load_text_repoter_if_not_provided() -> None: + """Test if PyLinter.reporter is a TextReporter if no reporter is provided""" + linter = PyLinter() + + assert isinstance(linter.reporter, TextReporter) + + @staticmethod def test_regex_paths_csv_validator() -> None: """Test to see if _regexp_paths_csv_validator works. Previously the validator crashed when encountering already validated values. diff --git a/tests/unittest_reporting.py b/tests/unittest_reporting.py index 08bd2a345..757987359 100644 --- a/tests/unittest_reporting.py +++ b/tests/unittest_reporting.py @@ -190,11 +190,11 @@ def test_multi_format_output(tmp_path): with redirect_stdout(text): linter = PyLinter() + linter.load_default_plugins() linter.set_option("persistent", False) linter.set_option("output-format", formats) linter.set_option("reports", True) linter.set_option("score", True) - linter.load_default_plugins() assert linter.reporter.linter is linter with pytest.raises(NotImplementedError): |