report_functions.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  2. # For details: https://github.com/pylint-dev/pylint/blob/main/LICENSE
  3. # Copyright (c) https://github.com/pylint-dev/pylint/blob/main/CONTRIBUTORS.txt
  4. from __future__ import annotations
  5. import collections
  6. from collections import defaultdict
  7. from pylint import checkers, exceptions
  8. from pylint.reporters.ureports.nodes import Section, Table
  9. from pylint.utils import LinterStats
  10. def report_total_messages_stats(
  11. sect: Section,
  12. stats: LinterStats,
  13. previous_stats: LinterStats | None,
  14. ) -> None:
  15. """Make total errors / warnings report."""
  16. lines = ["type", "number", "previous", "difference"]
  17. lines += checkers.table_lines_from_stats(stats, previous_stats, "message_types")
  18. sect.append(Table(children=lines, cols=4, rheaders=1))
  19. def report_messages_stats(
  20. sect: Section,
  21. stats: LinterStats,
  22. _: LinterStats | None,
  23. ) -> None:
  24. """Make messages type report."""
  25. by_msg_stats = stats.by_msg
  26. in_order = sorted(
  27. (value, msg_id)
  28. for msg_id, value in by_msg_stats.items()
  29. if not msg_id.startswith("I")
  30. )
  31. in_order.reverse()
  32. lines = ["message id", "occurrences"]
  33. for value, msg_id in in_order:
  34. lines += [msg_id, str(value)]
  35. sect.append(Table(children=lines, cols=2, rheaders=1))
  36. def report_messages_by_module_stats(
  37. sect: Section,
  38. stats: LinterStats,
  39. _: LinterStats | None,
  40. ) -> None:
  41. """Make errors / warnings by modules report."""
  42. module_stats = stats.by_module
  43. if len(module_stats) == 1:
  44. # don't print this report when we are analysing a single module
  45. raise exceptions.EmptyReportError()
  46. by_mod: defaultdict[str, dict[str, int | float]] = collections.defaultdict(dict)
  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))