style_guide.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. """Implementation of the StyleGuide used by Flake8."""
  2. from __future__ import annotations
  3. import argparse
  4. import contextlib
  5. import copy
  6. import enum
  7. import functools
  8. import logging
  9. from typing import Generator
  10. from typing import Sequence
  11. from flake8 import defaults
  12. from flake8 import statistics
  13. from flake8 import utils
  14. from flake8.formatting import base as base_formatter
  15. from flake8.violation import Violation
  16. __all__ = ("StyleGuide",)
  17. LOG = logging.getLogger(__name__)
  18. class Selected(enum.Enum):
  19. """Enum representing an explicitly or implicitly selected code."""
  20. Explicitly = "explicitly selected"
  21. Implicitly = "implicitly selected"
  22. class Ignored(enum.Enum):
  23. """Enum representing an explicitly or implicitly ignored code."""
  24. Explicitly = "explicitly ignored"
  25. Implicitly = "implicitly ignored"
  26. class Decision(enum.Enum):
  27. """Enum representing whether a code should be ignored or selected."""
  28. Ignored = "ignored error"
  29. Selected = "selected error"
  30. def _explicitly_chosen(
  31. *,
  32. option: list[str] | None,
  33. extend: list[str] | None,
  34. ) -> tuple[str, ...]:
  35. ret = [*(option or []), *(extend or [])]
  36. return tuple(sorted(ret, reverse=True))
  37. def _select_ignore(
  38. *,
  39. option: list[str] | None,
  40. default: tuple[str, ...],
  41. extended_default: list[str],
  42. extend: list[str] | None,
  43. ) -> tuple[str, ...]:
  44. # option was explicitly set, ignore the default and extended default
  45. if option is not None:
  46. ret = [*option, *(extend or [])]
  47. else:
  48. ret = [*default, *extended_default, *(extend or [])]
  49. return tuple(sorted(ret, reverse=True))
  50. class DecisionEngine:
  51. """A class for managing the decision process around violations.
  52. This contains the logic for whether a violation should be reported or
  53. ignored.
  54. """
  55. def __init__(self, options: argparse.Namespace) -> None:
  56. """Initialize the engine."""
  57. self.cache: dict[str, Decision] = {}
  58. self.selected_explicitly = _explicitly_chosen(
  59. option=options.select,
  60. extend=options.extend_select,
  61. )
  62. self.ignored_explicitly = _explicitly_chosen(
  63. option=options.ignore,
  64. extend=options.extend_ignore,
  65. )
  66. self.selected = _select_ignore(
  67. option=options.select,
  68. default=(),
  69. extended_default=options.extended_default_select,
  70. extend=options.extend_select,
  71. )
  72. self.ignored = _select_ignore(
  73. option=options.ignore,
  74. default=defaults.IGNORE,
  75. extended_default=options.extended_default_ignore,
  76. extend=options.extend_ignore,
  77. )
  78. def was_selected(self, code: str) -> Selected | Ignored:
  79. """Determine if the code has been selected by the user.
  80. :param code: The code for the check that has been run.
  81. :returns:
  82. Selected.Implicitly if the selected list is empty,
  83. Selected.Explicitly if the selected list is not empty and a match
  84. was found,
  85. Ignored.Implicitly if the selected list is not empty but no match
  86. was found.
  87. """
  88. if code.startswith(self.selected_explicitly):
  89. return Selected.Explicitly
  90. elif code.startswith(self.selected):
  91. return Selected.Implicitly
  92. else:
  93. return Ignored.Implicitly
  94. def was_ignored(self, code: str) -> Selected | Ignored:
  95. """Determine if the code has been ignored by the user.
  96. :param code:
  97. The code for the check that has been run.
  98. :returns:
  99. Selected.Implicitly if the ignored list is empty,
  100. Ignored.Explicitly if the ignored list is not empty and a match was
  101. found,
  102. Selected.Implicitly if the ignored list is not empty but no match
  103. was found.
  104. """
  105. if code.startswith(self.ignored_explicitly):
  106. return Ignored.Explicitly
  107. elif code.startswith(self.ignored):
  108. return Ignored.Implicitly
  109. else:
  110. return Selected.Implicitly
  111. def make_decision(self, code: str) -> Decision:
  112. """Decide if code should be ignored or selected."""
  113. selected = self.was_selected(code)
  114. ignored = self.was_ignored(code)
  115. LOG.debug(
  116. "The user configured %r to be %r, %r",
  117. code,
  118. selected,
  119. ignored,
  120. )
  121. if isinstance(selected, Selected) and isinstance(ignored, Selected):
  122. return Decision.Selected
  123. elif isinstance(selected, Ignored) and isinstance(ignored, Ignored):
  124. return Decision.Ignored
  125. elif (
  126. selected is Selected.Explicitly
  127. and ignored is not Ignored.Explicitly
  128. ):
  129. return Decision.Selected
  130. elif (
  131. selected is not Selected.Explicitly
  132. and ignored is Ignored.Explicitly
  133. ):
  134. return Decision.Ignored
  135. elif selected is Ignored.Implicitly and ignored is Selected.Implicitly:
  136. return Decision.Ignored
  137. elif (
  138. selected is Selected.Explicitly and ignored is Ignored.Explicitly
  139. ) or (
  140. selected is Selected.Implicitly and ignored is Ignored.Implicitly
  141. ):
  142. # we only get here if it was in both lists: longest prefix wins
  143. select = next(s for s in self.selected if code.startswith(s))
  144. ignore = next(s for s in self.ignored if code.startswith(s))
  145. if len(select) > len(ignore):
  146. return Decision.Selected
  147. else:
  148. return Decision.Ignored
  149. else:
  150. raise AssertionError(f"unreachable {code} {selected} {ignored}")
  151. def decision_for(self, code: str) -> Decision:
  152. """Return the decision for a specific code.
  153. This method caches the decisions for codes to avoid retracing the same
  154. logic over and over again. We only care about the select and ignore
  155. rules as specified by the user in their configuration files and
  156. command-line flags.
  157. This method does not look at whether the specific line is being
  158. ignored in the file itself.
  159. :param code: The code for the check that has been run.
  160. """
  161. decision = self.cache.get(code)
  162. if decision is None:
  163. decision = self.make_decision(code)
  164. self.cache[code] = decision
  165. LOG.debug('"%s" will be "%s"', code, decision)
  166. return decision
  167. class StyleGuideManager:
  168. """Manage multiple style guides for a single run."""
  169. def __init__(
  170. self,
  171. options: argparse.Namespace,
  172. formatter: base_formatter.BaseFormatter,
  173. decider: DecisionEngine | None = None,
  174. ) -> None:
  175. """Initialize our StyleGuide.
  176. .. todo:: Add parameter documentation.
  177. """
  178. self.options = options
  179. self.formatter = formatter
  180. self.stats = statistics.Statistics()
  181. self.decider = decider or DecisionEngine(options)
  182. self.style_guides: list[StyleGuide] = []
  183. self.default_style_guide = StyleGuide(
  184. options, formatter, self.stats, decider=decider
  185. )
  186. self.style_guides = [
  187. self.default_style_guide,
  188. *self.populate_style_guides_with(options),
  189. ]
  190. self.style_guide_for = functools.lru_cache(maxsize=None)(
  191. self._style_guide_for
  192. )
  193. def populate_style_guides_with(
  194. self, options: argparse.Namespace
  195. ) -> Generator[StyleGuide, None, None]:
  196. """Generate style guides from the per-file-ignores option.
  197. :param options:
  198. The original options parsed from the CLI and config file.
  199. :returns:
  200. A copy of the default style guide with overridden values.
  201. """
  202. per_file = utils.parse_files_to_codes_mapping(options.per_file_ignores)
  203. for filename, violations in per_file:
  204. yield self.default_style_guide.copy(
  205. filename=filename, extend_ignore_with=violations
  206. )
  207. def _style_guide_for(self, filename: str) -> StyleGuide:
  208. """Find the StyleGuide for the filename in particular."""
  209. return max(
  210. (g for g in self.style_guides if g.applies_to(filename)),
  211. key=lambda g: len(g.filename or ""),
  212. )
  213. @contextlib.contextmanager
  214. def processing_file(
  215. self, filename: str
  216. ) -> Generator[StyleGuide, None, None]:
  217. """Record the fact that we're processing the file's results."""
  218. guide = self.style_guide_for(filename)
  219. with guide.processing_file(filename):
  220. yield guide
  221. def handle_error(
  222. self,
  223. code: str,
  224. filename: str,
  225. line_number: int,
  226. column_number: int,
  227. text: str,
  228. physical_line: str | None = None,
  229. ) -> int:
  230. """Handle an error reported by a check.
  231. :param code:
  232. The error code found, e.g., E123.
  233. :param filename:
  234. The file in which the error was found.
  235. :param line_number:
  236. The line number (where counting starts at 1) at which the error
  237. occurs.
  238. :param column_number:
  239. The column number (where counting starts at 1) at which the error
  240. occurs.
  241. :param text:
  242. The text of the error message.
  243. :param physical_line:
  244. The actual physical line causing the error.
  245. :returns:
  246. 1 if the error was reported. 0 if it was ignored. This is to allow
  247. for counting of the number of errors found that were not ignored.
  248. """
  249. guide = self.style_guide_for(filename)
  250. return guide.handle_error(
  251. code, filename, line_number, column_number, text, physical_line
  252. )
  253. class StyleGuide:
  254. """Manage a Flake8 user's style guide."""
  255. def __init__(
  256. self,
  257. options: argparse.Namespace,
  258. formatter: base_formatter.BaseFormatter,
  259. stats: statistics.Statistics,
  260. filename: str | None = None,
  261. decider: DecisionEngine | None = None,
  262. ):
  263. """Initialize our StyleGuide.
  264. .. todo:: Add parameter documentation.
  265. """
  266. self.options = options
  267. self.formatter = formatter
  268. self.stats = stats
  269. self.decider = decider or DecisionEngine(options)
  270. self.filename = filename
  271. if self.filename:
  272. self.filename = utils.normalize_path(self.filename)
  273. def __repr__(self) -> str:
  274. """Make it easier to debug which StyleGuide we're using."""
  275. return f"<StyleGuide [{self.filename}]>"
  276. def copy(
  277. self,
  278. filename: str | None = None,
  279. extend_ignore_with: Sequence[str] | None = None,
  280. ) -> StyleGuide:
  281. """Create a copy of this style guide with different values."""
  282. filename = filename or self.filename
  283. options = copy.deepcopy(self.options)
  284. options.extend_ignore = options.extend_ignore or []
  285. options.extend_ignore.extend(extend_ignore_with or [])
  286. return StyleGuide(
  287. options, self.formatter, self.stats, filename=filename
  288. )
  289. @contextlib.contextmanager
  290. def processing_file(
  291. self, filename: str
  292. ) -> Generator[StyleGuide, None, None]:
  293. """Record the fact that we're processing the file's results."""
  294. self.formatter.beginning(filename)
  295. yield self
  296. self.formatter.finished(filename)
  297. def applies_to(self, filename: str) -> bool:
  298. """Check if this StyleGuide applies to the file.
  299. :param filename:
  300. The name of the file with violations that we're potentially
  301. applying this StyleGuide to.
  302. :returns:
  303. True if this applies, False otherwise
  304. """
  305. if self.filename is None:
  306. return True
  307. return utils.matches_filename(
  308. filename,
  309. patterns=[self.filename],
  310. log_message=f'{self!r} does %(whether)smatch "%(path)s"',
  311. logger=LOG,
  312. )
  313. def should_report_error(self, code: str) -> Decision:
  314. """Determine if the error code should be reported or ignored.
  315. This method only cares about the select and ignore rules as specified
  316. by the user in their configuration files and command-line flags.
  317. This method does not look at whether the specific line is being
  318. ignored in the file itself.
  319. :param code:
  320. The code for the check that has been run.
  321. """
  322. return self.decider.decision_for(code)
  323. def handle_error(
  324. self,
  325. code: str,
  326. filename: str,
  327. line_number: int,
  328. column_number: int,
  329. text: str,
  330. physical_line: str | None = None,
  331. ) -> int:
  332. """Handle an error reported by a check.
  333. :param code:
  334. The error code found, e.g., E123.
  335. :param filename:
  336. The file in which the error was found.
  337. :param line_number:
  338. The line number (where counting starts at 1) at which the error
  339. occurs.
  340. :param column_number:
  341. The column number (where counting starts at 1) at which the error
  342. occurs.
  343. :param text:
  344. The text of the error message.
  345. :param physical_line:
  346. The actual physical line causing the error.
  347. :returns:
  348. 1 if the error was reported. 0 if it was ignored. This is to allow
  349. for counting of the number of errors found that were not ignored.
  350. """
  351. disable_noqa = self.options.disable_noqa
  352. # NOTE(sigmavirus24): Apparently we're provided with 0-indexed column
  353. # numbers so we have to offset that here.
  354. if not column_number:
  355. column_number = 0
  356. error = Violation(
  357. code,
  358. filename,
  359. line_number,
  360. column_number + 1,
  361. text,
  362. physical_line,
  363. )
  364. error_is_selected = (
  365. self.should_report_error(error.code) is Decision.Selected
  366. )
  367. is_not_inline_ignored = error.is_inline_ignored(disable_noqa) is False
  368. if error_is_selected and is_not_inline_ignored:
  369. self.formatter.handle(error)
  370. self.stats.record(error)
  371. return 1
  372. return 0