report_functions.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. import collections
  4. from typing import DefaultDict, Dict, Union
  5. from pylint import checkers, exceptions
  6. from pylint.reporters.ureports.nodes import Table
  7. from pylint.utils import LinterStats
  8. def report_total_messages_stats(
  9. sect,
  10. stats: LinterStats,
  11. previous_stats: LinterStats,
  12. ):
  13. """make total errors / warnings report"""
  14. lines = ["type", "number", "previous", "difference"]
  15. lines += checkers.table_lines_from_stats(stats, previous_stats, "message_types")
  16. sect.append(Table(children=lines, cols=4, rheaders=1))
  17. def report_messages_stats(
  18. sect,
  19. stats: LinterStats,
  20. _: LinterStats,
  21. ):
  22. """make messages type report"""
  23. by_msg_stats = stats.by_msg
  24. in_order = sorted(
  25. (value, msg_id)
  26. for msg_id, value in by_msg_stats.items()
  27. if not msg_id.startswith("I")
  28. )
  29. in_order.reverse()
  30. lines = ["message id", "occurrences"]
  31. for value, msg_id in in_order:
  32. lines += [msg_id, str(value)]
  33. sect.append(Table(children=lines, cols=2, rheaders=1))
  34. def report_messages_by_module_stats(
  35. sect,
  36. stats: LinterStats,
  37. _: LinterStats,
  38. ):
  39. """make errors / warnings by modules report"""
  40. module_stats = stats.by_module
  41. if len(module_stats) == 1:
  42. # don't print this report when we are analysing a single module
  43. raise exceptions.EmptyReportError()
  44. by_mod: DefaultDict[str, Dict[str, Union[int, float]]] = collections.defaultdict(
  45. dict
  46. )
  47. for m_type in ("fatal", "error", "warning", "refactor", "convention"):
  48. total = stats.get_global_message_count(m_type)
  49. for module in module_stats.keys():
  50. mod_total = stats.get_module_message_count(module, m_type)
  51. percent = 0 if total == 0 else float(mod_total * 100) / total
  52. by_mod[module][m_type] = percent
  53. sorted_result = []
  54. for module, mod_info in by_mod.items():
  55. sorted_result.append(
  56. (
  57. mod_info["error"],
  58. mod_info["warning"],
  59. mod_info["refactor"],
  60. mod_info["convention"],
  61. module,
  62. )
  63. )
  64. sorted_result.sort()
  65. sorted_result.reverse()
  66. lines = ["module", "error", "warning", "refactor", "convention"]
  67. for line in sorted_result:
  68. # Don't report clean modules.
  69. if all(entry == 0 for entry in line[:-1]):
  70. continue
  71. lines.append(line[-1])
  72. for val in line[:-1]:
  73. lines.append(f"{val:.2f}")
  74. if len(lines) == 5:
  75. raise exceptions.EmptyReportError()
  76. sect.append(Table(children=lines, cols=5, rheaders=1))