__init__.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. """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. from typing import TYPE_CHECKING, Literal
  40. from pylint.checkers.base_checker import (
  41. BaseChecker,
  42. BaseRawFileChecker,
  43. BaseTokenChecker,
  44. )
  45. from pylint.checkers.deprecated import DeprecatedMixin
  46. from pylint.utils import LinterStats, diff_string, register_plugins
  47. if TYPE_CHECKING:
  48. from pylint.lint import PyLinter
  49. def table_lines_from_stats(
  50. stats: LinterStats,
  51. old_stats: LinterStats | None,
  52. stat_type: Literal["duplicated_lines", "message_types"],
  53. ) -> list[str]:
  54. """Get values listed in <columns> from <stats> and <old_stats>,
  55. and return a formatted list of values.
  56. The return value is designed to be given to a ureport.Table object
  57. """
  58. lines: list[str] = []
  59. if stat_type == "duplicated_lines":
  60. new: list[tuple[str, int | float]] = [
  61. ("nb_duplicated_lines", stats.duplicated_lines["nb_duplicated_lines"]),
  62. (
  63. "percent_duplicated_lines",
  64. stats.duplicated_lines["percent_duplicated_lines"],
  65. ),
  66. ]
  67. if old_stats:
  68. old: list[tuple[str, str | int | float]] = [
  69. (
  70. "nb_duplicated_lines",
  71. old_stats.duplicated_lines["nb_duplicated_lines"],
  72. ),
  73. (
  74. "percent_duplicated_lines",
  75. old_stats.duplicated_lines["percent_duplicated_lines"],
  76. ),
  77. ]
  78. else:
  79. old = [("nb_duplicated_lines", "NC"), ("percent_duplicated_lines", "NC")]
  80. elif stat_type == "message_types":
  81. new = [
  82. ("convention", stats.convention),
  83. ("refactor", stats.refactor),
  84. ("warning", stats.warning),
  85. ("error", stats.error),
  86. ]
  87. if old_stats:
  88. old = [
  89. ("convention", old_stats.convention),
  90. ("refactor", old_stats.refactor),
  91. ("warning", old_stats.warning),
  92. ("error", old_stats.error),
  93. ]
  94. else:
  95. old = [
  96. ("convention", "NC"),
  97. ("refactor", "NC"),
  98. ("warning", "NC"),
  99. ("error", "NC"),
  100. ]
  101. # pylint: disable=possibly-used-before-assignment
  102. for index, value in enumerate(new):
  103. new_value = value[1]
  104. old_value = old[index][1]
  105. diff_str = (
  106. diff_string(old_value, new_value)
  107. if isinstance(old_value, float)
  108. else old_value
  109. )
  110. new_str = f"{new_value:.3f}" if isinstance(new_value, float) else str(new_value)
  111. old_str = f"{old_value:.3f}" if isinstance(old_value, float) else str(old_value)
  112. lines.extend((value[0].replace("_", " "), new_str, old_str, diff_str)) # type: ignore[arg-type]
  113. return lines
  114. def initialize(linter: PyLinter) -> None:
  115. """Initialize linter with checkers in this package."""
  116. register_plugins(linter, __path__[0])
  117. __all__ = [
  118. "BaseChecker",
  119. "BaseRawFileChecker",
  120. "BaseTokenChecker",
  121. "DeprecatedMixin",
  122. "initialize",
  123. "register_plugins",
  124. ]