From a404be15b9a3d511b2c7ab685db729e9c67b24b2 Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Fri, 10 Apr 2020 12:13:12 +0000 Subject: [PATCH] make, scripts: Add support for static Python typechecking This commit adds support for static typechecking with mypy. Notable additions: - A new target "check" which does the type checking - Py-mods.mk, meant to be included from a directory containing python modules in subdirectories, but not being a python module itself. It makes the all target depend on check only if PY_RUN_CHECK_AFTER_BUILD is defined and true. That's because pypy is under heavy development, and the Ubuntu 18.04 version is too old to work for lots of the code. Signed-off-by: Jan Lindemann --- make/py-defs.mk | 6 +++++- make/py-mods.mk | 14 ++++++++++++++ make/py-rules.mk | 5 ++++- scripts/python-tools.sh | 3 ++- 4 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 make/py-mods.mk diff --git a/make/py-defs.mk b/make/py-defs.mk index 08ab51a4..8d6bbcc4 100644 --- a/make/py-defs.mk +++ b/make/py-defs.mk @@ -21,6 +21,8 @@ ifndef PY_PREREQ_BUILD_DIRS PY_PREREQ_BUILD_DIRS := $(shell $(proj_query_cmd) proj-dir $(PY_PREREQ_BUILD)) endif +PY_MYPY ?= mypy --ignore-missing-imports + PY_SRC_PY ?= $(wildcard *.py) PY_ALL_PY = $(PY_SRC_PY) @@ -66,7 +68,8 @@ PY_INSTALLED_REG = $(PY_INSTALLED_PY) $(PY_INSTALLED_PYC) PYTHONPATH ?= $(call proj_query, pythonpath $(PROJECT) $(PY_PREREQ_RUN)) #PYTHONPATH = $(subst $(space),,$(ENV_PYTHONPATH)$(foreach d,$(PY_MODULE_DIRS),:$(d))) -export PYTHONPATH +MYPYPATH = $(PYTHONPATH) +export PYTHONPATH MYPYPATH # not used anywhere #PY_PROJ_MODULE_DIRS ?= $(wildcard $(foreach subdir,/tools/python /src/python,$(addsuffix $(subdir),$(TOPDIR) $(PY_PREREQ_BUILD_DIRS)))) @@ -84,6 +87,7 @@ echo-py: @echo "PY_SITE_PACKAGES_PATH = $(PY_SITE_PACKAGES_PATH)" @echo "PY_MODULES = $(PY_MODULES)" @echo "PYTHONPATH = $(PYTHONPATH)" + @echo "MYPYPATH = $(MYPYPATH)" @echo "PY_INSTALL_MOD = $(PY_INSTALL_MOD)" @echo "PY_INSTALL_SUB_MOD = $(PY_INSTALL_SUB_MOD)" # not used anywhere" diff --git a/make/py-mods.mk b/make/py-mods.mk new file mode 100644 index 00000000..14766e62 --- /dev/null +++ b/make/py-mods.mk @@ -0,0 +1,14 @@ +include $(JWBDIR)/make/defs.mk +include $(JWBDIR)/make/py-defs.mk +include $(JWBDIR)/make/dirs.mk +include $(JWBDIR)/make/dev-utils.mk +include $(JWBDIR)/make/py-rules.mk + +all: +install: +clean: +distclean: + +ifeq ($(PY_RUN_CHECK_AFTER_BUILD),true) +all: check +endif diff --git a/make/py-rules.mk b/make/py-rules.mk index e3f4ae2c..44179362 100644 --- a/make/py-rules.mk +++ b/make/py-rules.mk @@ -3,7 +3,7 @@ all: py.clean: $(RM) -f *.done *.pyc *.rep /bin/bash $(JWB_SCRIPT_DIR)/scm.sh clean -f __init__.py - $(RM) -rf __pycache__ + $(RM) -rf __pycache__ .mypy_cache install-dirs.done: $(INSTALL) -d -m $(PYJWBDIRMODE) -o $(PYJWBDIROWNER) -g $(PYJWBDIRGROUP) $(PY_INSTALL_DIRS) @@ -28,3 +28,6 @@ else $(PY_INSTALL_DIR_PY)/%.pyc: %.pyc endif $(INSTALL) -p -m $(PYMODMODE) -o $(PYMODOWNER) -g $(PYMODGROUP) $< $@ + +check: + $(PY_MYPY) $(shell /bin/bash $(JWB_SCRIPT_DIR)/scm.sh ls-files | grep '\.py$$') diff --git a/scripts/python-tools.sh b/scripts/python-tools.sh index bc1a0a72..47ba373d 100644 --- a/scripts/python-tools.sh +++ b/scripts/python-tools.sh @@ -26,7 +26,8 @@ cmd_create_init() local del="-------------------------- generated by $myname" echo "# >> $del >>" echo "from pkgutil import extend_path" - echo "__path__ = extend_path(__path__, __name__)" + echo "from typing import Iterable" + echo "__path__ = extend_path(__path__, __name__) # type: Iterable[str]" files="$*" for f in $files; do test -d $f && continue