__init__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. """Utilities methods and classes for checkers.
  5. Base id of standard checkers (used in msg and report ids):
  6. 01: base
  7. 02: classes
  8. 03: format
  9. 04: import
  10. 05: misc
  11. 06: variables
  12. 07: exceptions
  13. 08: similar
  14. 09: design_analysis
  15. 10: newstyle
  16. 11: typecheck
  17. 12: logging
  18. 13: string_format
  19. 14: string_constant
  20. 15: stdlib
  21. 16: python3 (This one was deleted but needs to be reserved for consistency with old messages)
  22. 17: refactoring
  23. .
  24. .
  25. .
  26. 24: non-ascii-names
  27. 25: unicode
  28. 26: unsupported_version
  29. 27: private-import
  30. 28-50: not yet used: reserved for future internal checkers.
  31. This file is not updated. Use
  32. script/get_unused_message_id_category.py
  33. to get the next free checker id.
  34. 51-99: perhaps used: reserved for external checkers
  35. The raw_metrics checker has no number associated since it doesn't emit any
  36. messages nor reports. XXX not true, emit a 07 report !
  37. """
  38. from __future__ import annotations
  39. import sys
  40. from typing import TYPE_CHECKING
  41. from pylint.checkers.base_checker import (
  42. BaseChecker,
  43. BaseRawFileChecker,
  44. BaseTokenChecker,
  45. )
  46. from pylint.checkers.deprecated import DeprecatedMixin
  47. from pylint.checkers.mapreduce_checker import MapReduceMixin
  48. from pylint.utils import LinterStats, diff_string, register_plugins
  49. if sys.version_info >= (3, 8):
  50. from typing import Literal
  51. else:
  52. from typing_extensions import Literal
  53. if TYPE_CHECKING:
  54. from pylint.lint import PyLinter
  55. def table_lines_from_stats(
  56. stats: LinterStats,
  57. old_stats: LinterStats | None,
  58. stat_type: Literal["duplicated_lines", "message_types"],
  59. ) -> list[str]:
  60. """Get values listed in <columns> from <stats> and <old_stats>,
  61. and return a formatted list of values.
  62. The return value is designed to be given to a ureport.Table object
  63. """
  64. lines: list[str] = []
  65. if stat_type == "duplicated_lines":
  66. new: list[tuple[str, int | float]] = [
  67. ("nb_duplicated_lines", stats.duplicated_lines["nb_duplicated_lines"]),
  68. (
  69. "percent_duplicated_lines",
  70. stats.duplicated_lines["percent_duplicated_lines"],
  71. ),
  72. ]
  73. if old_stats:
  74. old: list[tuple[str, str | int | float]] = [
  75. (
  76. "nb_duplicated_lines",
  77. old_stats.duplicated_lines["nb_duplicated_lines"],
  78. ),
  79. (
  80. "percent_duplicated_lines",
  81. old_stats.duplicated_lines["percent_duplicated_lines"],
  82. ),
  83. ]
  84. else:
  85. old = [("nb_duplicated_lines", "NC"), ("percent_duplicated_lines", "NC")]
  86. elif stat_type == "message_types":
  87. new = [
  88. ("convention", stats.convention),
  89. ("refactor", stats.refactor),
  90. ("warning", stats.warning),
  91. ("error", stats.error),
  92. ]
  93. if old_stats:
  94. old = [
  95. ("convention", old_stats.convention),
  96. ("refactor", old_stats.refactor),
  97. ("warning", old_stats.warning),
  98. ("error", old_stats.error),
  99. ]
  100. else:
  101. old = [
  102. ("convention", "NC"),
  103. ("refactor", "NC"),
  104. ("warning", "NC"),
  105. ("error", "NC"),
  106. ]
  107. for index, value in enumerate(new):
  108. new_value = value[1]
  109. old_value = old[index][1]
  110. diff_str = (
  111. diff_string(old_value, new_value)
  112. if isinstance(old_value, float)
  113. else old_value
  114. )
  115. new_str = f"{new_value:.3f}" if isinstance(new_value, float) else str(new_value)
  116. old_str = f"{old_value:.3f}" if isinstance(old_value, float) else str(old_value)
  117. lines.extend((value[0].replace("_", " "), new_str, old_str, diff_str)) # type: ignore[arg-type]
  118. return lines
  119. def initialize(linter: PyLinter) -> None:
  120. """Initialize linter with checkers in this package."""
  121. register_plugins(linter, __path__[0])
  122. __all__ = [
  123. "BaseChecker",
  124. "BaseTokenChecker",
  125. "BaseRawFileChecker",
  126. "initialize",
  127. "MapReduceMixin",
  128. "DeprecatedMixin",
  129. "register_plugins",
  130. ]