check_stronglist.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import argparse
  2. import re
  3. from collections.abc import Sequence
  4. import tomllib
  5. def _glob_to_re(s: str) -> str:
  6. if s.endswith(".*"):
  7. pat = rf'{re.escape(s.removesuffix(".*"))}(?:|\..*+)'
  8. else:
  9. pat = re.escape(s)
  10. return f"^{pat}$"
  11. def main(argv: Sequence[str] | None = None) -> int:
  12. parser = argparse.ArgumentParser()
  13. parser.add_argument("filenames", nargs="*")
  14. args = parser.parse_args(argv)
  15. retv = 0
  16. for filename in args.filenames:
  17. with open(filename, "rb") as f:
  18. overrides = tomllib.load(f)["tool"]["mypy"]["overrides"]
  19. (allowlist,) = (cfg for cfg in overrides if "disable_error_code" in cfg)
  20. (stronglist,) = (cfg for cfg in overrides if "disallow_untyped_defs" in cfg)
  21. stronglist_re = re.compile("|".join(_glob_to_re(g) for g in stronglist["module"]))
  22. for mod in allowlist["module"]:
  23. if stronglist_re.fullmatch(mod):
  24. print(f"{filename}: {mod} is in the typing errors allowlist *and* stronglist")
  25. retv = 1
  26. return retv
  27. if __name__ == "__main__":
  28. raise SystemExit(main())