From 8e6b89d399cb8aaad0c221e8802ce31b38f1efac Mon Sep 17 00:00:00 2001 From: Alex Burke Date: Wed, 11 Mar 2026 14:03:54 +0100 Subject: [PATCH] Integrate black into the Makefile replacing previous autopep8 attempt. We are currently trying to have consistent formatting in the codebase but here is currently no standardised way to do this. Based on some growing consensus bring in black and switch local formatting and linting to it. A new `make fmt` target will format files with the appropriate style. The `make lint` target (broken/unused) invokes the same but in checking mode and thus will report violations. Given we are likely to adopt this in stages we define a variable in the Makefile listing the directories we wish to enforce cleanliness for. --- Makefile | 37 ++++++++++++++++++++++++++++++++++++- local-requirements.txt | 2 ++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index cd4f81d89..f06bb85e6 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,11 @@ ifndef PY PY = 3 endif +FORMAT_ENFORCE_DIRS = state/ +FORMAT_EXCLUDE_REGEX = '.*' +FORMAT_EXCLUDE_GLOB = '*' +FORMAT_LINE_LENGTH = 80 + LOCAL_PYTHON_BIN = './envhelp/lpython' ifdef PYTHON_BIN @@ -35,7 +40,37 @@ ifneq ($(MIG_ENV),'local') @echo "unavailable outside local development environment" @exit 1 endif - $(LOCAL_PYTHON_BIN) -m autopep8 --ignore E402 -i + @make format-python + +.PHONY:format-python +format-python: + @$(LOCAL_PYTHON_BIN) -m black $(FORMAT_ENFORCE_DIRS) \ + --line-length=$(FORMAT_LINE_LENGTH) \ + --exclude=$(FORMAT_EXCLUDE_REGEX) + @$(LOCAL_PYTHON_BIN) -m isort $(FORMAT_ENFORCE_DIRS) \ + --profile=black \ + --line-length=$(FORMAT_LINE_LENGTH) \ + --skip-glob=$(FORMAT_EXCLUDE_GLOB) + +.PHONY: lint +lint: +ifneq ($(MIG_ENV),'local') + @echo "unavailable outside local development environment" + @exit 1 +endif + @make lint-python + +.PHONY: lint-python +lint-python: + @$(LOCAL_PYTHON_BIN) -m black $(FORMAT_ENFORCE_DIRS) \ + --check \ + --line-length=$(FORMAT_LINE_LENGTH) \ + --exclude $(FORMAT_EXCLUDE_REGEX) + @$(LOCAL_PYTHON_BIN) -m isort $(FORMAT_ENFORCE_DIRS) \ + --check-only \ + --profile=black \ + --line-length=$(FORMAT_LINE_LENGTH) \ + --skip-glob=$(FORMAT_EXCLUDE_GLOB) .PHONY: clean clean: diff --git a/local-requirements.txt b/local-requirements.txt index 7faf3b026..4941e1a6b 100644 --- a/local-requirements.txt +++ b/local-requirements.txt @@ -3,6 +3,8 @@ # This list is mainly used to specify addons needed for the unit tests. # We only need autopep8 on py 3 as it's used in 'make fmt' (with py3) autopep8;python_version >= "3" +black +isort # We need paramiko for the ssh unit tests # NOTE: paramiko-3.0.0 dropped python2 and python3.6 support paramiko;python_version >= "3.7"