statistics.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. """Statistic collection logic for Flake8."""
  2. from typing import Dict
  3. from typing import Generator
  4. from typing import List
  5. from typing import NamedTuple
  6. from typing import Optional
  7. from flake8.violation import Violation
  8. class Statistics:
  9. """Manager of aggregated statistics for a run of Flake8."""
  10. def __init__(self) -> None:
  11. """Initialize the underlying dictionary for our statistics."""
  12. self._store: Dict[Key, "Statistic"] = {}
  13. def error_codes(self) -> List[str]:
  14. """Return all unique error codes stored.
  15. :returns:
  16. Sorted list of error codes.
  17. """
  18. return sorted({key.code for key in self._store})
  19. def record(self, error: "Violation") -> None:
  20. """Add the fact that the error was seen in the file.
  21. :param error:
  22. The Violation instance containing the information about the
  23. violation.
  24. """
  25. key = Key.create_from(error)
  26. if key not in self._store:
  27. self._store[key] = Statistic.create_from(error)
  28. self._store[key].increment()
  29. def statistics_for(
  30. self, prefix: str, filename: Optional[str] = None
  31. ) -> Generator["Statistic", None, None]:
  32. """Generate statistics for the prefix and filename.
  33. If you have a :class:`Statistics` object that has recorded errors,
  34. you can generate the statistics for a prefix (e.g., ``E``, ``E1``,
  35. ``W50``, ``W503``) with the optional filter of a filename as well.
  36. .. code-block:: python
  37. >>> stats = Statistics()
  38. >>> stats.statistics_for('E12',
  39. filename='src/flake8/statistics.py')
  40. <generator ...>
  41. >>> stats.statistics_for('W')
  42. <generator ...>
  43. :param prefix:
  44. The error class or specific error code to find statistics for.
  45. :param filename:
  46. (Optional) The filename to further filter results by.
  47. :returns:
  48. Generator of instances of :class:`Statistic`
  49. """
  50. matching_errors = sorted(
  51. key for key in self._store if key.matches(prefix, filename)
  52. )
  53. for error_code in matching_errors:
  54. yield self._store[error_code]
  55. class Key(NamedTuple):
  56. """Simple key structure for the Statistics dictionary.
  57. To make things clearer, easier to read, and more understandable, we use a
  58. namedtuple here for all Keys in the underlying dictionary for the
  59. Statistics object.
  60. """
  61. filename: str
  62. code: str
  63. @classmethod
  64. def create_from(cls, error: "Violation") -> "Key":
  65. """Create a Key from :class:`flake8.violation.Violation`."""
  66. return cls(filename=error.filename, code=error.code)
  67. def matches(self, prefix: str, filename: Optional[str]) -> bool:
  68. """Determine if this key matches some constraints.
  69. :param prefix:
  70. The error code prefix that this key's error code should start with.
  71. :param filename:
  72. The filename that we potentially want to match on. This can be
  73. None to only match on error prefix.
  74. :returns:
  75. True if the Key's code starts with the prefix and either filename
  76. is None, or the Key's filename matches the value passed in.
  77. """
  78. return self.code.startswith(prefix) and (
  79. filename is None or self.filename == filename
  80. )
  81. class Statistic:
  82. """Simple wrapper around the logic of each statistic.
  83. Instead of maintaining a simple but potentially hard to reason about
  84. tuple, we create a class which has attributes and a couple
  85. convenience methods on it.
  86. """
  87. def __init__(
  88. self, error_code: str, filename: str, message: str, count: int
  89. ) -> None:
  90. """Initialize our Statistic."""
  91. self.error_code = error_code
  92. self.filename = filename
  93. self.message = message
  94. self.count = count
  95. @classmethod
  96. def create_from(cls, error: "Violation") -> "Statistic":
  97. """Create a Statistic from a :class:`flake8.violation.Violation`."""
  98. return cls(
  99. error_code=error.code,
  100. filename=error.filename,
  101. message=error.text,
  102. count=0,
  103. )
  104. def increment(self) -> None:
  105. """Increment the number of times we've seen this error in this file."""
  106. self.count += 1