style_guide.py 15 KB

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