flake8_plugin.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from __future__ import annotations
  2. import ast
  3. from typing import Any, Generator
  4. S001_fmt = (
  5. "S001 Avoid using the {} mock call as it is "
  6. "confusing and prone to causing invalid test "
  7. "behavior."
  8. )
  9. S001_methods = frozenset(("not_called", "called_once", "called_once_with"))
  10. S002_msg = "S002 print functions or statements are not allowed."
  11. S003_msg = "S003 Use ``from sentry.utils import json`` instead."
  12. S003_modules = frozenset(("json", "simplejson"))
  13. S004_msg = "S004 Use `pytest.raises` instead for better debuggability."
  14. S004_methods = frozenset(("assertRaises", "assertRaisesRegex"))
  15. S005_msg = "S005 Do not import models from sentry.models but the actual module"
  16. S006_msg = "S006 Do not use force_bytes / force_str -- test the types directly"
  17. class SentryVisitor(ast.NodeVisitor):
  18. def __init__(self, filename: str) -> None:
  19. self.errors: list[tuple[int, int, str]] = []
  20. self.filename = filename
  21. def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
  22. if node.module and not node.level:
  23. if node.module.split(".")[0] in S003_modules:
  24. self.errors.append((node.lineno, node.col_offset, S003_msg))
  25. # for now only enforce this in getsentry
  26. elif (
  27. "getsentry/" in self.filename
  28. and node.module == "sentry.models"
  29. and any(x.name.isupper() or x.name.istitle() for x in node.names)
  30. ):
  31. self.errors.append((node.lineno, node.col_offset, S005_msg))
  32. elif (
  33. "tests/" in self.filename
  34. and node.module == "django.utils.encoding"
  35. and any(x.name in {"force_bytes", "force_str"} for x in node.names)
  36. ):
  37. self.errors.append((node.lineno, node.col_offset, S006_msg))
  38. self.generic_visit(node)
  39. def visit_Import(self, node: ast.Import) -> None:
  40. for alias in node.names:
  41. if alias.name.split(".")[0] in S003_modules:
  42. self.errors.append((node.lineno, node.col_offset, S003_msg))
  43. self.generic_visit(node)
  44. def visit_Attribute(self, node: ast.Attribute) -> None:
  45. if node.attr in S001_methods:
  46. self.errors.append((node.lineno, node.col_offset, S001_fmt.format(node.attr)))
  47. elif node.attr in S004_methods:
  48. self.errors.append((node.lineno, node.col_offset, S004_msg))
  49. self.generic_visit(node)
  50. def visit_Name(self, node: ast.Name) -> None:
  51. if node.id == "print":
  52. self.errors.append((node.lineno, node.col_offset, S002_msg))
  53. self.generic_visit(node)
  54. class SentryCheck:
  55. def __init__(self, tree: ast.AST, filename: str) -> None:
  56. self.tree = tree
  57. self.filename = filename
  58. def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
  59. visitor = SentryVisitor(self.filename)
  60. visitor.visit(self.tree)
  61. for e in visitor.errors:
  62. yield (*e, type(self))