flake8_plugin.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. elif node.module == "sentry.models":
  28. self.errors.append((node.lineno, node.col_offset, S005_msg))
  29. elif (
  30. "tests/" in self.filename
  31. and node.module == "django.utils.encoding"
  32. and any(x.name in {"force_bytes", "force_str"} for x in node.names)
  33. ):
  34. self.errors.append((node.lineno, node.col_offset, S006_msg))
  35. elif (
  36. "tests/" not in self.filename
  37. and "fixtures/" not in self.filename
  38. and "sentry/testutils/" not in self.filename
  39. and "sentry.testutils" in node.module
  40. ):
  41. self.errors.append((node.lineno, node.col_offset, S007_msg))
  42. if node.module == "pytz" and any(x.name.lower() == "utc" for x in node.names):
  43. self.errors.append((node.lineno, node.col_offset, S008_msg))
  44. self.generic_visit(node)
  45. def visit_Import(self, node: ast.Import) -> None:
  46. for alias in node.names:
  47. if alias.name.split(".")[0] in S003_modules:
  48. self.errors.append((node.lineno, node.col_offset, S003_msg))
  49. elif (
  50. "tests/" not in self.filename
  51. and "fixtures/" 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. 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))