diff options
Diffstat (limited to 'Biz/Repl.py')
-rw-r--r-- | Biz/Repl.py | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/Biz/Repl.py b/Biz/Repl.py index 9cc0c35..cb9dfe7 100644 --- a/Biz/Repl.py +++ b/Biz/Repl.py @@ -2,14 +2,26 @@ Improve the standard Python REPL. This module attempts to emulate the workflow of ghci or lisp repls. It uses -importlib to load a namespace from the given path. It then binds 'r()' to a -function that reloads the same namespace. +importlib to load a namespace from the provided path, typechecks it with mypy, +and provides some tools for improving repl-driven development. + +This module is called in Biz/Ide/repl.sh like so: + + python -i Biz/Repl.py NS PATH + +where NS is the dot-partitioned namespace of the main module, and PATH is the +path to the same file. In the future this could be expanded to be a list of +additional files to load. """ import importlib +import importlib.util import logging +import os import sys +import mypy.api + from Biz import Log @@ -34,20 +46,31 @@ def use(ns: str, path: str) -> None: globals().update({k: getattr(module, k) for k in names}) +def typecheck(path: str) -> None: + """Typecheck this namespace.""" + # this envvar is undocumented, but it works + # https://github.com/python/mypy/issues/13815 + os.environ["MYPY_FORCE_COLOR"] = "1" + logging.info("typechecking %s", path) + stdout, stderr, _ = mypy.api.run([path]) + sys.stdout.write(stdout) + sys.stdout.flush() + sys.stderr.write(stderr) + sys.stderr.flush() + + if __name__ == "__main__": Log.setup() NS = sys.argv[1] PATH = sys.argv[2] use(NS, PATH) + typecheck(PATH) - logging.info("use reload() or _r() after making changes") + logging.info("use reload() after making changes") sys.ps1 = f"{NS}> " sys.ps2 = f"{NS}| " def reload() -> None: """Reload the namespace.""" - return use(NS, PATH) - - def _r() -> None: - """Shorthand: Reload the namespace.""" - return use(NS, PATH) + use(NS, PATH) + typecheck(PATH) |