make_module_ignores.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import annotations
  2. import shutil
  3. import subprocess
  4. import sys
  5. def main() -> int:
  6. shutil.rmtree(".mypy_cache", ignore_errors=True)
  7. filenames = set()
  8. out = subprocess.run(
  9. (sys.executable, "-m", "tools.mypy_helpers.mypy_without_ignores", *sys.argv[1:]),
  10. capture_output=True,
  11. )
  12. for line in out.stdout.decode().splitlines():
  13. filename, _, _ = line.partition(":")
  14. if filename.endswith(".py"):
  15. filenames.add(filename)
  16. mods = []
  17. for filename in sorted(filenames):
  18. # TODO: removeprefix / removesuffix python 3.9+
  19. if filename.endswith(".py"):
  20. filename = filename[: -len(".py")]
  21. if filename.startswith("src/"):
  22. filename = filename[len("src/") :]
  23. if filename.endswith("/__init__"):
  24. filename = filename[: -len("/__init__")]
  25. mods.append(filename.replace("/", "."))
  26. mods_s = "".join(f' "{mod}",\n' for mod in mods)
  27. generated = (
  28. f"# - remove the module from the list and fix the issues!\n"
  29. f"# - python3 -m tools.mypy_helpers.find_easiest_modules\n"
  30. f"[[tool.mypy.overrides]]\n"
  31. f"module = [\n{mods_s}]\n"
  32. f"ignore_errors = true\n"
  33. )
  34. with open("pyproject.toml") as f:
  35. src = f.read()
  36. msg = "sentry modules with typing issues"
  37. before, begin, rest = src.partition(f"# begin: {msg}\n")
  38. _, end, rest = rest.partition(f"# end: {msg}\n")
  39. with open("pyproject.toml", "w") as f:
  40. f.write(before + begin + generated + end + rest)
  41. return 0
  42. if __name__ == "__main__":
  43. raise SystemExit(main())