flake8_plugin.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "fixtures/" not in self.filename
  42. and "sentry/testutils/" not in self.filename
  43. and "sentry.testutils" in node.module
  44. ):
  45. self.errors.append((node.lineno, node.col_offset, S007_msg))
  46. self.generic_visit(node)
  47. def visit_Import(self, node: ast.Import) -> None:
  48. for alias in node.names:
  49. if alias.name.split(".")[0] in S003_modules:
  50. self.errors.append((node.lineno, node.col_offset, S003_msg))
  51. elif (
  52. "tests/" not in self.filename
  53. and "fixtures/" not in self.filename
  54. and "sentry/testutils/" not in self.filename
  55. and "sentry.testutils" in alias.name
  56. ):
  57. self.errors.append((node.lineno, node.col_offset, S007_msg))
  58. self.generic_visit(node)
  59. def visit_Attribute(self, node: ast.Attribute) -> None:
  60. if node.attr in S001_methods:
  61. self.errors.append((node.lineno, node.col_offset, S001_fmt.format(node.attr)))
  62. elif node.attr in S004_methods:
  63. self.errors.append((node.lineno, node.col_offset, S004_msg))
  64. self.generic_visit(node)
  65. def visit_Name(self, node: ast.Name) -> None:
  66. if node.id == "print":
  67. self.errors.append((node.lineno, node.col_offset, S002_msg))
  68. self.generic_visit(node)
  69. class SentryCheck:
  70. def __init__(self, tree: ast.AST, filename: str) -> None:
  71. self.tree = tree
  72. self.filename = filename
  73. def run(self) -> Generator[tuple[int, int, str, type[Any]], None, None]:
  74. visitor = SentryVisitor(self.filename)
  75. visitor.visit(self.tree)
  76. for e in visitor.errors:
  77. yield (*e, type(self))