|
11 | 11 | import traceback
|
12 | 12 | import warnings
|
13 | 13 | 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 |
15 | 15 |
|
16 | 16 | import astroid
|
17 | 17 | from astroid import AstroidError, nodes
|
@@ -533,8 +533,8 @@ def __init__(
|
533 | 533 | self.set_reporter(reporter)
|
534 | 534 | else:
|
535 | 535 | 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""" |
538 | 538 |
|
539 | 539 | self.msgs_store = MessageDefinitionStore()
|
540 | 540 | self._checkers = collections.defaultdict(list)
|
@@ -619,19 +619,21 @@ def load_plugin_configuration(self):
|
619 | 619 | except ModuleNotFoundError as e:
|
620 | 620 | self.add_message("bad-plugin-value", args=(modname, e), line=0)
|
621 | 621 |
|
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 |
623 | 626 | sub_reporters = []
|
624 | 627 | output_files = []
|
625 | 628 | with contextlib.ExitStack() as stack:
|
626 |
| - for reporter_name in self._reporter_names.split(","): |
| 629 | + for reporter_name in reporter_names.split(","): |
627 | 630 | reporter_name, *reporter_output = reporter_name.split(":", 1)
|
628 | 631 |
|
629 | 632 | reporter = self._load_reporter_by_name(reporter_name)
|
630 | 633 | sub_reporters.append(reporter)
|
631 | 634 | if reporter_output:
|
632 |
| - (reporter_output,) = reporter_output |
633 | 635 | output_file = stack.enter_context(
|
634 |
| - open(reporter_output, "w", encoding="utf-8") |
| 636 | + open(reporter_output[0], "w", encoding="utf-8") |
635 | 637 | )
|
636 | 638 | reporter.out = output_file
|
637 | 639 | output_files.append(output_file)
|
@@ -690,18 +692,17 @@ def set_option(self, optname, value, action=None, optdict=None):
|
690 | 692 | meth(value)
|
691 | 693 | return # no need to call set_option, disable/enable methods do it
|
692 | 694 | 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) |
699 | 699 | try:
|
700 | 700 | checkers.BaseTokenChecker.set_option(self, optname, value, action, optdict)
|
701 | 701 | except config.UnsupportedAction:
|
702 | 702 | print(f"option {optname} can't be read from config file", file=sys.stderr)
|
703 | 703 |
|
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.""" |
705 | 706 | self._reporters[reporter_class.name] = reporter_class
|
706 | 707 |
|
707 | 708 | def report_order(self):
|
|
0 commit comments