flake8_plugin.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. S008_msg = "S008 Use stdlib datetime.timezone.utc instead of pytz.utc / pytz.UTC"
  19. S009_msg = "S009 Do not import requirements-dev packages into production code."
  20. S009_modules = frozenset(
  21. ("django_stubs_ext", "pytest", "responses", "time_machine", "selenium", "psutil")
  22. )
  23. def is_test_file(path: str) -> bool:
  24. return "tests/" in path or "fixtures/" in path or "sentry/testutils/" in path
  25. class SentryVisitor(ast.NodeVisitor):
  26. def __init__(self, filename: str) -> None:
  27. self.errors: list[tuple[int, int, str]] = []
  28. self.filename = filename
  29. def visit_ImportFrom(self, node: ast.ImportFrom) -> None:
  30. if node.module and not node.level:
  31. if node.module.split(".")[0] in S003_modules:
  32. self.errors.append((node.lineno, node.col_offset, S003_msg))
  33. elif node.module == "sentry.models":
  34. self.errors.append((node.lineno, node.col_offset, S005_msg))
  35. elif (
  36. "tests/" in self.filename
  37. and node.module == "django.utils.encoding"
  38. and any(x.name in {"force_bytes", "force_str"} for x in node.names)
  39. ):
  40. self.errors.append((node.lineno, node.col_offset, S006_msg))
  41. elif not is_test_file(self.filename) and "sentry.testutils" in node.module:
  42. self.errors.append((node.lineno, node.col_offset, S007_msg))
  43. elif not is_test_file(self.filename) and node.module in S009_modules:
  44. self.errors.append((node.lineno, node.col_offset, S009_msg))
  45. if node.module == "pytz" and any(x.name.lower() == "utc" for x in node.names):
  46. self.errors.append((node.lineno, node.col_offset, S008_msg))
  47. self.generic_visit(node)
  48. def visit_Import(self, node: ast.Import) -> None:
  49. for alias in node.names:
  50. if alias.name.split(".")[0] in S003_modules:
  51. self.errors.append((node.lineno, node.col_offset, S003_msg))
  52. elif not is_test_file(self.filename) and "sentry.testutils" in alias.name:
  53. self.errors.append((node.lineno, node.col_offset, S007_msg))
  54. elif not is_test_file(self.filename) and alias.name in S009_modules:
  55. self.errors.append((node.lineno, node.col_offset, S009_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. elif (
  63. isinstance(node.value, ast.Name)
  64. and node.value.id == "pytz"
  65. and node.attr.lower() == "utc"
  66. ):
  67. self.errors.append((node.lineno, node.col_offset, S008_msg))
  68. self.generic_visit(node)
  69. def visit_Name(self, node: ast.Name) -> None:
  70. if node.id == "print":
  71. self.errors.append((node.lineno, node.col_offset, S002_msg))
  72. self.generic_visit(node)
  73. class SentryCheck:
  74. def __init__(self, tree: ast.AST, filename: str) -> None:
  75. self.tree = tree
  76. self.filename = filename
  77. def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
  78. visitor = SentryVisitor(self.filename)
  79. visitor.visit(self.tree)
  80. for e in visitor.errors:
  81. yield (*e, type(self))