Skip to content

Commit 623b54d

Browse files
authored
Update typing of reporter attributes in PyLinter (#5525)
1 parent 26c9042 commit 623b54d

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

pylint/lint/pylinter.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import traceback
1212
import warnings
1313
from io import TextIOWrapper
14-
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Union
14+
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Type, Union
1515

1616
import astroid
1717
from astroid import AstroidError, nodes
@@ -533,8 +533,8 @@ def __init__(
533533
self.set_reporter(reporter)
534534
else:
535535
self.set_reporter(TextReporter())
536-
self._reporter_names = None
537-
self._reporters = {}
536+
self._reporters: Dict[str, Type[reporters.BaseReporter]] = {}
537+
"""Dictionary of possible but non-initialized reporters"""
538538

539539
self.msgs_store = MessageDefinitionStore()
540540
self._checkers = collections.defaultdict(list)
@@ -619,19 +619,21 @@ def load_plugin_configuration(self):
619619
except ModuleNotFoundError as e:
620620
self.add_message("bad-plugin-value", args=(modname, e), line=0)
621621

622-
def _load_reporters(self) -> None:
622+
def _load_reporters(self, reporter_names: str) -> None:
623+
"""Load the reporters if they are available on _reporters"""
624+
if not self._reporters:
625+
return
623626
sub_reporters = []
624627
output_files = []
625628
with contextlib.ExitStack() as stack:
626-
for reporter_name in self._reporter_names.split(","):
629+
for reporter_name in reporter_names.split(","):
627630
reporter_name, *reporter_output = reporter_name.split(":", 1)
628631

629632
reporter = self._load_reporter_by_name(reporter_name)
630633
sub_reporters.append(reporter)
631634
if reporter_output:
632-
(reporter_output,) = reporter_output
633635
output_file = stack.enter_context(
634-
open(reporter_output, "w", encoding="utf-8")
636+
open(reporter_output[0], "w", encoding="utf-8")
635637
)
636638
reporter.out = output_file
637639
output_files.append(output_file)
@@ -690,18 +692,17 @@ def set_option(self, optname, value, action=None, optdict=None):
690692
meth(value)
691693
return # no need to call set_option, disable/enable methods do it
692694
elif optname == "output-format":
693-
self._reporter_names = value
694-
# If the reporters are already available, load
695-
# the reporter class.
696-
if self._reporters:
697-
self._load_reporters()
698-
695+
assert isinstance(
696+
value, str
697+
), "'output-format' should be a comma separated string of reporters"
698+
self._load_reporters(value)
699699
try:
700700
checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
701701
except config.UnsupportedAction:
702702
print(f"option {optname} can't be read from config file", file=sys.stderr)
703703

704-
def register_reporter(self, reporter_class):
704+
def register_reporter(self, reporter_class: Type[reporters.BaseReporter]) -> None:
705+
"""Registers a reporter class on the _reporters attribute."""
705706
self._reporters[reporter_class.name] = reporter_class
706707

707708
def report_order(self):

pylint/reporters/base_reporter.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class BaseReporter:
2323

2424
extension = ""
2525

26+
name = "base"
27+
"""Name of the reporter"""
28+
2629
def __init__(self, output: Optional[TextIO] = None) -> None:
2730
self.linter: "PyLinter"
2831
self.section = 0

0 commit comments

Comments
 (0)