flake8_plugin.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. S007_msg = "S007 Do not import sentry.testutils into production code."
  18. class SentryVisitor(ast.NodeVisitor):
  19. def __init__(self, filename: str) -> None:
  20. self.errors: list[tuple[int, int, str]] = []
  21. self.filename = filename
  22. def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
  23. if node.module and not node.level:
  24. if node.module.split(".")[0] in S003_modules:
  25. self.errors.append((node.lineno, node.col_offset, S003_msg))
  26. # for now only enforce this in getsentry
  27. elif (
  28. "getsentry/" in self.filename
  29. and node.module == "sentry.models"
  30. and any(x.name.isupper() or x.name.istitle() for x in node.names)
  31. ):
  32. self.errors.append((node.lineno, node.col_offset, S005_msg))
  33. elif (
  34. "tests/" in self.filename
  35. and node.module == "django.utils.encoding"
  36. and any(x.name in {"force_bytes", "force_str"} for x in node.names)
  37. ):
  38. self.errors.append((node.lineno, node.col_offset, S006_msg))
  39. elif (
  40. "tests/" not in self.filename
  41. and "sentry/testutils/" not in self.filename
  42. and "sentry.testutils" in node.module
  43. ):
  44. self.errors.append((node.lineno, node.col_offset, S007_msg))
  45. self.generic_visit(node)
  46. def visit_Import(self, node: ast.Import) -> None:
  47. for alias in node.names:
  48. if alias.name.split(".")[0] in S003_modules:
  49. self.errors.append((node.lineno, node.col_offset, S003_msg))
  50. elif (
  51. "tests/" not in self.filename
  52. and "sentry/testutils/" not in self.filename
  53. and "sentry.testutils" in alias.name
  54. ):
  55. self.errors.append((node.lineno, node.col_offset, S007_msg))
  56. self.generic_visit(node)
  57. def visit_Attribute(self, node: ast.Attribute) -> None:
  58. if node.attr in S001_methods:
  59. self.errors.append((node.lineno, node.col_offset, S001_fmt.format(node.attr)))
  60. elif node.attr in S004_methods:
  61. self.errors.append((node.lineno, node.col_offset, S004_msg))
  62. self.generic_visit(node)
  63. def visit_Name(self, node: ast.Name) -> None:
  64. if node.id == "print":
  65. self.errors.append((node.lineno, node.col_offset, S002_msg))
  66. self.generic_visit(node)
  67. class SentryCheck:
  68. def __init__(self, tree: ast.AST, filename: str) -> None:
  69. self.tree = tree
  70. self.filename = filename
  71. def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
  72. visitor = SentryVisitor(self.filename)
  73. visitor.visit(self.tree)
  74. for e in visitor.errors:
  75. yield (*e, type(self))