__init__.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # Copyright (c) 2006-2014 LOGILAB S.A. (Paris, FRANCE) <contact@logilab.fr>
  2. # Copyright (c) 2013-2014 Google, Inc.
  3. # Copyright (c) 2013 buck@yelp.com <buck@yelp.com>
  4. # Copyright (c) 2014-2018, 2020 Claudiu Popa <pcmanticore@gmail.com>
  5. # Copyright (c) 2014 Brett Cannon <brett@python.org>
  6. # Copyright (c) 2014 Arun Persaud <arun@nubati.net>
  7. # Copyright (c) 2015 Ionel Cristian Maries <contact@ionelmc.ro>
  8. # Copyright (c) 2016 Moises Lopez <moylop260@vauxoo.com>
  9. # Copyright (c) 2017-2018 Bryce Guinta <bryce.paul.guinta@gmail.com>
  10. # Copyright (c) 2018-2021 Pierre Sassoulas <pierre.sassoulas@gmail.com>
  11. # Copyright (c) 2018 ssolanki <sushobhitsolanki@gmail.com>
  12. # Copyright (c) 2019 Bruno P. Kinoshita <kinow@users.noreply.github.com>
  13. # Copyright (c) 2020-2021 hippo91 <guillaume.peillex@gmail.com>
  14. # Copyright (c) 2020 Frank Harrison <frank@doublethefish.com>
  15. # Copyright (c) 2021 Daniël van Noord <13665637+DanielNoord@users.noreply.github.com>
  16. # Copyright (c) 2021 Marc Mueller <30130371+cdce8p@users.noreply.github.com>
  17. # Copyright (c) 2021 Matus Valo <matusvalo@users.noreply.github.com>
  18. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. # For details: https://github.com/PyCQA/pylint/blob/main/LICENSE
  20. """utilities methods and classes for checkers
  21. Base id of standard checkers (used in msg and report ids):
  22. 01: base
  23. 02: classes
  24. 03: format
  25. 04: import
  26. 05: misc
  27. 06: variables
  28. 07: exceptions
  29. 08: similar
  30. 09: design_analysis
  31. 10: newstyle
  32. 11: typecheck
  33. 12: logging
  34. 13: string_format
  35. 14: string_constant
  36. 15: stdlib
  37. 16: python3
  38. 17: refactoring
  39. 18-50: not yet used: reserved for future internal checkers.
  40. 51-99: perhaps used: reserved for external checkers
  41. The raw_metrics checker has no number associated since it doesn't emit any
  42. messages nor reports. XXX not true, emit a 07 report !
  43. """
  44. import sys
  45. from typing import List, Optional, Tuple, Union
  46. from pylint.checkers.base_checker import BaseChecker, BaseTokenChecker
  47. from pylint.checkers.deprecated import DeprecatedMixin
  48. from pylint.checkers.mapreduce_checker import MapReduceMixin
  49. from pylint.utils import LinterStats, diff_string, register_plugins
  50. if sys.version_info >= (3, 8):
  51. from typing import Literal
  52. else:
  53. from typing_extensions import Literal
  54. def table_lines_from_stats(
  55. stats: LinterStats,
  56. old_stats: Optional[LinterStats],
  57. stat_type: Literal["duplicated_lines", "message_types"],
  58. ) -> List[str]:
  59. """get values listed in <columns> from <stats> and <old_stats>,
  60. and return a formatted list of values, designed to be given to a
  61. ureport.Table object
  62. """
  63. lines: List[str] = []
  64. if stat_type == "duplicated_lines":
  65. new: List[Tuple[str, Union[str, int, float]]] = [
  66. ("nb_duplicated_lines", stats.duplicated_lines["nb_duplicated_lines"]),
  67. (
  68. "percent_duplicated_lines",
  69. stats.duplicated_lines["percent_duplicated_lines"],
  70. ),
  71. ]
  72. if old_stats:
  73. old: List[Tuple[str, Union[str, int, float]]] = [
  74. (
  75. "nb_duplicated_lines",
  76. old_stats.duplicated_lines["nb_duplicated_lines"],
  77. ),
  78. (
  79. "percent_duplicated_lines",
  80. old_stats.duplicated_lines["percent_duplicated_lines"],
  81. ),
  82. ]
  83. else:
  84. old = [("nb_duplicated_lines", "NC"), ("percent_duplicated_lines", "NC")]
  85. elif stat_type == "message_types":
  86. new = [
  87. ("convention", stats.convention),
  88. ("refactor", stats.refactor),
  89. ("warning", stats.warning),
  90. ("error", stats.error),
  91. ]
  92. if old_stats:
  93. old = [
  94. ("convention", old_stats.convention),
  95. ("refactor", old_stats.refactor),
  96. ("warning", old_stats.warning),
  97. ("error", old_stats.error),
  98. ]
  99. else:
  100. old = [
  101. ("convention", "NC"),
  102. ("refactor", "NC"),
  103. ("warning", "NC"),
  104. ("error", "NC"),
  105. ]
  106. for index, _ in enumerate(new):
  107. new_value = new[index][1]
  108. old_value = old[index][1]
  109. diff_str = (
  110. diff_string(old_value, new_value)
  111. if isinstance(old_value, float)
  112. else old_value
  113. )
  114. new_str = f"{new_value:.3f}" if isinstance(new_value, float) else str(new_value)
  115. old_str = f"{old_value:.3f}" if isinstance(old_value, float) else str(old_value)
  116. lines.extend((new[index][0].replace("_", " "), new_str, old_str, diff_str))
  117. return lines
  118. def initialize(linter):
  119. """initialize linter with checkers in this package"""
  120. register_plugins(linter, __path__[0])
  121. __all__ = [
  122. "BaseChecker",
  123. "BaseTokenChecker",
  124. "initialize",
  125. "MapReduceMixin",
  126. "DeprecatedMixin",
  127. "register_plugins",
  128. ]