flake8_plugin.py 4.1 KB

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