Skip to content

Commit 82f0f88

Browse files
Add typing to PyLinter.reporter (#5325)
* Add ``messages`` attribute to ``MultiReporter`` * Make ``PyLinter`` always have a ``reporter`` attribute Co-authored-by: Pierre Sassoulas <[email protected]>
1 parent 17a16d7 commit 82f0f88

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

ChangeLog

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ Release date: TBA
2727
Insert your changelog randomly, it will reduce merge conflicts
2828
(Ie. not necessarily at the end)
2929

30+
* The ``PyLinter`` class will now be initialiazed with a ``TextReporter``
31+
as its reporter if none is provided.
32+
3033

3134
What's New in Pylint 2.12.2?
3235
============================

doc/whatsnew/2.13.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ Other Changes
3434
* Require Python ``3.6.2`` to run pylint.
3535

3636
Closes #5065
37+
38+
* The ``PyLinter`` class will now be initialiazed with a ``TextReporter``
39+
as its reporter if none is provided.

pylint/lint/pylinter.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
prepare_crash_report,
4040
)
4141
from pylint.message import Message, MessageDefinition, MessageDefinitionStore
42+
from pylint.reporters.text import TextReporter
4243
from pylint.reporters.ureports import nodes as report_nodes
4344
from pylint.typing import (
4445
FileItem,
@@ -517,13 +518,25 @@ def make_options():
517518
("Reports", "Options related to output formatting and reporting"),
518519
)
519520

520-
def __init__(self, options=(), reporter=None, option_groups=(), pylintrc=None):
521+
def __init__(
522+
self,
523+
options=(),
524+
reporter=None,
525+
option_groups=(),
526+
pylintrc=None,
527+
):
521528
"""Some stuff has to be done before ancestors initialization...
522529
messages store / checkers / reporter / astroid manager"""
523-
self.msgs_store = MessageDefinitionStore()
524-
self.reporter = None
530+
# Attributes for reporters
531+
self.reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
532+
if reporter:
533+
self.set_reporter(reporter)
534+
else:
535+
self.set_reporter(TextReporter())
525536
self._reporter_names = None
526537
self._reporters = {}
538+
539+
self.msgs_store = MessageDefinitionStore()
527540
self._checkers = collections.defaultdict(list)
528541
self._pragma_lineno = {}
529542
self._ignore_file = False
@@ -572,16 +585,10 @@ def __init__(self, options=(), reporter=None, option_groups=(), pylintrc=None):
572585
self._dynamic_plugins = set()
573586
self._error_mode = False
574587
self.load_provider_defaults()
575-
if reporter:
576-
self.set_reporter(reporter)
577588

578589
def load_default_plugins(self):
579590
checkers.initialize(self)
580591
reporters.initialize(self)
581-
# Make sure to load the default reporter, because
582-
# the option has been set before the plugins had been loaded.
583-
if not self.reporter:
584-
self._load_reporters()
585592

586593
def load_plugin_modules(self, modnames):
587594
"""take a list of module names which are pylint plugins and load
@@ -654,7 +661,9 @@ def _load_reporter_by_name(self, reporter_name: str) -> reporters.BaseReporter:
654661
else:
655662
return reporter_class()
656663

657-
def set_reporter(self, reporter):
664+
def set_reporter(
665+
self, reporter: Union[reporters.BaseReporter, reporters.MultiReporter]
666+
) -> None:
658667
"""set the reporter used to display messages and reports"""
659668
self.reporter = reporter
660669
reporter.linter = self

pylint/reporters/multi_reporter.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(
4141
self._path_strip_prefix = os.getcwd() + os.sep
4242
self._linter: Optional[PyLinter] = None
4343
self.out = output
44+
self.messages: List[Message] = []
4445

4546
@property
4647
def out(self):

tests/test_self.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,13 @@ def test_enable_all_extensions() -> None:
12601260
)
12611261
assert sorted(plugins) == sorted(runner.linter._dynamic_plugins)
12621262

1263+
@staticmethod
1264+
def test_load_text_repoter_if_not_provided() -> None:
1265+
"""Test if PyLinter.reporter is a TextReporter if no reporter is provided"""
1266+
linter = PyLinter()
1267+
1268+
assert isinstance(linter.reporter, TextReporter)
1269+
12631270
@staticmethod
12641271
def test_regex_paths_csv_validator() -> None:
12651272
"""Test to see if _regexp_paths_csv_validator works.

tests/unittest_reporting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,11 @@ def test_multi_format_output(tmp_path):
190190

191191
with redirect_stdout(text):
192192
linter = PyLinter()
193+
linter.load_default_plugins()
193194
linter.set_option("persistent", False)
194195
linter.set_option("output-format", formats)
195196
linter.set_option("reports", True)
196197
linter.set_option("score", True)
197-
linter.load_default_plugins()
198198

199199
assert linter.reporter.linter is linter
200200
with pytest.raises(NotImplementedError):

0 commit comments

Comments
 (0)