raw_metrics.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. from __future__ import annotations
  5. import sys
  6. import tokenize
  7. from typing import TYPE_CHECKING, Any, cast
  8. from pylint.checkers import BaseTokenChecker
  9. from pylint.reporters.ureports.nodes import Paragraph, Section, Table, Text
  10. from pylint.utils import LinterStats, diff_string
  11. if sys.version_info >= (3, 8):
  12. from typing import Literal
  13. else:
  14. from typing_extensions import Literal
  15. if TYPE_CHECKING:
  16. from pylint.lint import PyLinter
  17. def report_raw_stats(
  18. sect: Section,
  19. stats: LinterStats,
  20. old_stats: LinterStats | None,
  21. ) -> None:
  22. """Calculate percentage of code / doc / comment / empty."""
  23. total_lines = stats.code_type_count["total"]
  24. sect.insert(0, Paragraph([Text(f"{total_lines} lines have been analyzed\n")]))
  25. lines = ["type", "number", "%", "previous", "difference"]
  26. for node_type in ("code", "docstring", "comment", "empty"):
  27. node_type = cast(Literal["code", "docstring", "comment", "empty"], node_type)
  28. total = stats.code_type_count[node_type]
  29. percent = float(total * 100) / total_lines if total_lines else None
  30. old = old_stats.code_type_count[node_type] if old_stats else None
  31. diff_str = diff_string(old, total) if old else None
  32. lines += [
  33. node_type,
  34. str(total),
  35. f"{percent:.2f}" if percent is not None else "NC",
  36. str(old) if old else "NC",
  37. diff_str if diff_str else "NC",
  38. ]
  39. sect.append(Table(children=lines, cols=5, rheaders=1))
  40. class RawMetricsChecker(BaseTokenChecker):
  41. """Checker that provides raw metrics instead of checking anything.
  42. Provides:
  43. * total number of lines
  44. * total number of code lines
  45. * total number of docstring lines
  46. * total number of comments lines
  47. * total number of empty lines
  48. """
  49. # configuration section name
  50. name = "metrics"
  51. # configuration options
  52. options = ()
  53. # messages
  54. msgs: Any = {}
  55. # reports
  56. reports = (("RP0701", "Raw metrics", report_raw_stats),)
  57. def open(self) -> None:
  58. """Init statistics."""
  59. self.linter.stats.reset_code_count()
  60. def process_tokens(self, tokens: list[tokenize.TokenInfo]) -> None:
  61. """Update stats."""
  62. i = 0
  63. tokens = list(tokens)
  64. while i < len(tokens):
  65. i, lines_number, line_type = get_type(tokens, i)
  66. self.linter.stats.code_type_count["total"] += lines_number
  67. self.linter.stats.code_type_count[line_type] += lines_number
  68. JUNK = (tokenize.NL, tokenize.INDENT, tokenize.NEWLINE, tokenize.ENDMARKER)
  69. def get_type(
  70. tokens: list[tokenize.TokenInfo], start_index: int
  71. ) -> tuple[int, int, Literal["code", "docstring", "comment", "empty"]]:
  72. """Return the line type : docstring, comment, code, empty."""
  73. i = start_index
  74. start = tokens[i][2]
  75. pos = start
  76. line_type = None
  77. while i < len(tokens) and tokens[i][2][0] == start[0]:
  78. tok_type = tokens[i][0]
  79. pos = tokens[i][3]
  80. if line_type is None:
  81. if tok_type == tokenize.STRING:
  82. line_type = "docstring"
  83. elif tok_type == tokenize.COMMENT:
  84. line_type = "comment"
  85. elif tok_type in JUNK:
  86. pass
  87. else:
  88. line_type = "code"
  89. i += 1
  90. if line_type is None:
  91. line_type = "empty"
  92. elif i < len(tokens) and tokens[i][0] == tokenize.NEWLINE:
  93. i += 1
  94. # Mypy fails to infer the literal of line_type
  95. return i, pos[0] - start[0] + 1, line_type # type: ignore[return-value]
  96. def register(linter: PyLinter) -> None:
  97. linter.register_checker(RawMetricsChecker(linter))