make_module_ignores.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from __future__ import annotations
  2. import json
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. def main() -> int:
  8. shutil.rmtree(".mypy_cache", ignore_errors=True)
  9. out = subprocess.run(
  10. (sys.executable, "-m", "tools.mypy_helpers.mypy_without_ignores", "--output=json"),
  11. capture_output=True,
  12. text=True,
  13. )
  14. codes = set()
  15. filenames = set()
  16. os.makedirs(".artfifacts", exist_ok=True)
  17. with open(".artifacts/mypy-all", "w") as f:
  18. errors = 0
  19. for line in out.stdout.splitlines():
  20. e = json.loads(line)
  21. if e["file"].endswith(".py"):
  22. filenames.add(e["file"])
  23. if e["severity"] == "error":
  24. errors += 1
  25. codes.add(e["code"])
  26. codepart = f' [{e["code"]}]'
  27. else:
  28. codepart = ""
  29. f.write(f'{e["file"]}:{e["line"]}: {e["severity"]}: {e["message"]}{codepart}\n')
  30. f.write(f"Found {errors} in {len(filenames)} files\n")
  31. mods = []
  32. for filename in sorted(filenames):
  33. filename = filename.removesuffix(".py").removesuffix("/__init__").removeprefix("src/")
  34. mods.append(filename.replace("/", "."))
  35. mods_s = "".join(f' "{mod}",\n' for mod in mods)
  36. codes_s = "".join(f' "{code}",\n' for code in sorted(codes))
  37. generated = (
  38. f"# - remove the module from the list and fix the issues!\n"
  39. f"# - python3 -m tools.mypy_helpers.find_easiest_modules\n"
  40. f"[[tool.mypy.overrides]]\n"
  41. f"module = [\n{mods_s}]\n"
  42. f"disable_error_code = [\n{codes_s}]\n"
  43. )
  44. with open("pyproject.toml") as f:
  45. src = f.read()
  46. msg = "sentry modules with typing issues"
  47. before, begin, rest = src.partition(f"# begin: {msg}\n")
  48. _, end, rest = rest.partition(f"# end: {msg}\n")
  49. with open("pyproject.toml", "w") as f:
  50. f.write(before + begin + generated + end + rest)
  51. return 0
  52. if __name__ == "__main__":
  53. raise SystemExit(main())