Skip to content

Instantly share code, notes, and snippets.

@abersheeran
Last active February 3, 2023 10:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abersheeran/abfe6fa1d731affc5ec050408b7c5d4d to your computer and use it in GitHub Desktop.
Save abersheeran/abfe6fa1d731affc5ec050408b7c5d4d to your computer and use it in GitHub Desktop.
Ensure that all Python modules in the specified directory are turned on pep563
from pathlib import Path
def ensure_pep563(dirpath: Path) -> None:
self_path = Path(__file__).absolute()
future_import = "from __future__ import annotations"
for pypath in dirpath.glob("**/*.py"):
if pypath.absolute() == self_path:
continue
if future_import not in pypath.read_text():
pypath.write_text(f"{future_import}\n\n" + pypath.read_text())
if __name__ == "__main__":
import sys
ensure_pep563(Path(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment