Coverage for C:\Repos\ekr-pylint\pylint\reporters\reports_handler_mix_in.py: 42%

43 statements  

« prev     ^ index     » next       coverage.py v6.4, created at 2022-05-24 10:21 -0500

1# Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html 

2# For details: https://github.com/PyCQA/pylint/blob/main/LICENSE 

3# Copyright (c) https://github.com/PyCQA/pylint/blob/main/CONTRIBUTORS.txt 

4 

5from __future__ import annotations 

6 

7import collections 

8from collections.abc import MutableSequence 

9from typing import TYPE_CHECKING, DefaultDict, List, Tuple 

10 

11from pylint.exceptions import EmptyReportError 

12from pylint.reporters.ureports.nodes import Section 

13from pylint.typing import ReportsCallable 

14from pylint.utils import LinterStats 

15 

16if TYPE_CHECKING: 

17 from pylint.checkers import BaseChecker 

18 from pylint.lint.pylinter import PyLinter 

19 

20ReportsDict = DefaultDict["BaseChecker", List[Tuple[str, str, ReportsCallable]]] 

21 

22 

23class ReportsHandlerMixIn: 

24 """A mix-in class containing all the reports and stats manipulation 

25 related methods for the main lint class. 

26 """ 

27 

28 def __init__(self) -> None: 

29 self._reports: ReportsDict = collections.defaultdict(list) 

30 self._reports_state: dict[str, bool] = {} 

31 

32 def report_order(self) -> MutableSequence[BaseChecker]: 

33 """Return a list of reporters.""" 

34 return list(self._reports) 

35 

36 def register_report( 

37 self, reportid: str, r_title: str, r_cb: ReportsCallable, checker: BaseChecker 

38 ) -> None: 

39 """Register a report. 

40 

41 :param reportid: The unique identifier for the report 

42 :param r_title: The report's title 

43 :param r_cb: The method to call to make the report 

44 :param checker: The checker defining the report 

45 """ 

46 reportid = reportid.upper() 

47 self._reports[checker].append((reportid, r_title, r_cb)) 

48 

49 def enable_report(self, reportid: str) -> None: 

50 """Enable the report of the given id.""" 

51 reportid = reportid.upper() 

52 self._reports_state[reportid] = True 

53 

54 def disable_report(self, reportid: str) -> None: 

55 """Disable the report of the given id.""" 

56 reportid = reportid.upper() 

57 self._reports_state[reportid] = False 

58 

59 def report_is_enabled(self, reportid: str) -> bool: 

60 """Is the report associated to the given identifier enabled ?""" 

61 return self._reports_state.get(reportid, True) 

62 

63 def make_reports( # type: ignore[misc] # ReportsHandlerMixIn is always mixed with PyLinter 

64 self: PyLinter, 

65 stats: LinterStats, 

66 old_stats: LinterStats | None, 

67 ) -> Section: 

68 """Render registered reports.""" 

69 sect = Section("Report", f"{self.stats.statement} statements analysed.") 

70 for checker in self.report_order(): 

71 for reportid, r_title, r_cb in self._reports[checker]: 

72 if not self.report_is_enabled(reportid): 

73 continue 

74 report_sect = Section(r_title) 

75 try: 

76 r_cb(report_sect, stats, old_stats) 

77 except EmptyReportError: 

78 continue 

79 report_sect.report_id = reportid 

80 sect.append(report_sect) 

81 return sect