From fdd38d7498880939fa635d430ef70ef89bbfc895 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Wed, 15 Nov 2023 12:15:57 +0100 Subject: [PATCH] move flake8 and mypy to lints.sh; add fmts.sh; use more linters --- .pycodestyle | 11 ++++++ flake8 | 19 --------- fmt.sh | 7 ++++ lints.sh | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ mypy | 42 -------------------- pyproject.toml | 6 +++ 6 files changed, 129 insertions(+), 61 deletions(-) create mode 100644 .pycodestyle delete mode 100755 flake8 create mode 100755 fmt.sh create mode 100755 lints.sh delete mode 100755 mypy diff --git a/.pycodestyle b/.pycodestyle new file mode 100644 index 0000000..5a71e47 --- /dev/null +++ b/.pycodestyle @@ -0,0 +1,11 @@ +[pycodestyle] +# E203 not pep8 compliant (https://github.com/psf/black/issues/280) +# E266 too many leading '#' for block comment [ I like marking disabled code blocks with '### ' ] +# E402 module level import not at top of file [ usually on purpose. might use individual overrides instead? ] +# E701 multiple statements on one line [ still quite readable in short forms ] +# E713 test for membership should be ‘not in’ [ disagree: want `not a in x` ] +# E714 test for object identity should be 'is not' [ disagree: want `not a is x` ] +# W503 line break before binary operator [ gotta pick one way ] +ignore = E203,E266,E402,E701,E713,E714,W503 +max-line-length = 120 +exclude = *_pb2.py diff --git a/flake8 b/flake8 deleted file mode 100755 index 3ae725b..0000000 --- a/flake8 +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/sh - -### check type annotations with mypy - -set -e - -base=$(dirname "$(readlink -f "$0")") -cd "${base}" - -if [ ! -d "venv" -o ! -x "venv/bin/python" ]; then - echo >&2 "Missing virtualenv in 'venv'; maybe run setup-venv.sh first!" - exit 1 -fi - -if [ ! -x ./venv/bin/flake8 ]; then - ./venv/bin/pip install flake8 flake8-import-order -fi - -./venv/bin/flake8 src diff --git a/fmt.sh b/fmt.sh new file mode 100755 index 0000000..9b9a5bb --- /dev/null +++ b/fmt.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +self=$(dirname "$(readlink -f "$0")") +cd "${self}" + +python3 -m black src +python3 -m isort src diff --git a/lints.sh b/lints.sh new file mode 100755 index 0000000..4a9caea --- /dev/null +++ b/lints.sh @@ -0,0 +1,105 @@ +#!/bin/bash + +set -e + +cd "$(dirname "$(readlink "$0")")" + +sources=($@) +if [ "${#sources[@]}" -eq 0 ]; then + sources=(src) +fi + +rc=0 + +run() { + # remember last failure + if "$@"; then :; else rc=$?; fi +} + +venv_installs=() +venv_check() { + # make sure a python package is installed in venv (to get latest/matching version) + local pkg_file=$1 + local package=$2 + + for path in "${venv_pkgs[@]}"; do + if [ -e "${path}/${pkg_file}" ]; then + return + fi + done + + echo "Missing ${pkg_file} in venv, installing ${package}" + venv_installs+=("${package}") +} +py_check() { + # make sure a python package is installed + local pkg_file=$1 + local package=$2 + + for path in "${site_pkgs[@]}"; do + if [ -e "${path}/${pkg_file}" ]; then + return + fi + done + + echo "Missing ${pkg_file}, installing ${package}" + venv_installs+=("${package}") +} +venv_load_paths() { + local venv_base=$(readlink -f "venv") + local venv_base_l=${#venv_base} + local abs_path + local -a all_pkgs + + readarray -d '' -t all_pkgs < <(./venv/bin/python3 -c 'import site; print("\0".join(site.getsitepackages()), end="\0")') + venv_pkgs=() + site_pkgs=() + for path in "${all_pkgs[@]}"; do + if [ -d "${path}" ]; then + site_pkgs+=("${path}") + abs_path=$(readlink -f "${path}") + if [ "${abs_path::${venv_base_l}}" == "${venv_base}" ]; then + venv_pkgs+=("${path}") + fi + fi + done +} + +if [ -d venv ]; then + python=venv/bin/python3 + venv_load_paths + + py_check "pycodestyle.py" "pycodestyle" + py_check "flake8" "flake8" + + venv_check "mypy" "mypy" + venv_check "trio_typing" "trio-typing[mypy]" + venv_check "yaml-stubs" "types-PyYAML" + venv_check "aiofiles-stubs" "types-aiofiles" + venv_check "colorama-stubs" "types-colorama" + venv_check "cryptography-stubs" "types-cryptography" + venv_check "google-stubs" "types-protobuf" + venv_check "toml-stubs" "types-toml" + + py_check "black" "black" + py_check "isort" "isort" + + if [ "${#venv_installs[@]}" -gt 0 ]; then + ./venv/bin/pip install --upgrade "${venv_installs[@]}" + fi +else + python=python3 +fi + +echo "[pycodestyle]" +run "${python}" -m pycodestyle --config=.pycodestyle "${sources[@]}" +echo "[flake8]" +run "${python}" -m flake8 "${sources[@]}" +echo "[mypy]" +run "${python}" -m mypy "${sources[@]}" +echo "[black]" +run "${python}" -m black --check "${sources[@]}" +echo "[isort]" +run "${python}" -m isort --check-only "${sources[@]}" + +exit $rc diff --git a/mypy b/mypy deleted file mode 100755 index c50d79b..0000000 --- a/mypy +++ /dev/null @@ -1,42 +0,0 @@ -#!/bin/sh - -### check type annotations with mypy - -set -e - -base=$(dirname "$(readlink -f "$0")") -cd "${base}" - -if [ ! -d "venv" -o ! -x "venv/bin/python" ]; then - echo >&2 "Missing virtualenv in 'venv'; maybe run setup-venv.sh first!" - exit 1 -fi - -if [ ! -x ./venv/bin/mypy ]; then - ./venv/bin/pip install mypy trio-typing[mypy] types-PyYAML types-aiofiles types-colorama types-cryptography types-protobuf types-toml -fi - -site_pkgs=$(./venv/bin/python -c 'import site; print(site.getsitepackages()[0])') -if [ ! -d "${site_pkgs}/trio_typing" ]; then - ./venv/bin/pip install trio-typing[mypy] -fi -if [ ! -d "${site_pkgs}/yaml-stubs" ]; then - ./venv/bin/pip install types-PyYAML -fi -if [ ! -d "${site_pkgs}/aiofiles-stubs" ]; then - ./venv/bin/pip install types-aiofiles -fi -if [ ! -d "${site_pkgs}/colorama-stubs" ]; then - ./venv/bin/pip install types-colorama -fi -if [ ! -d "${site_pkgs}/cryptography-stubs" ]; then - ./venv/bin/pip install types-cryptography -fi -if [ ! -d "${site_pkgs}/google-stubs" ]; then - ./venv/bin/pip install types-protobuf -fi -if [ ! -d "${site_pkgs}/toml-stubs" ]; then - ./venv/bin/pip install types-toml -fi - -./venv/bin/mypy --install-types src diff --git a/pyproject.toml b/pyproject.toml index 6add2c6..2b70394 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,8 +7,13 @@ build-backend = "setuptools.build_meta" [tool.mypy] python_version = "3.11" +# disallow_any_generics = true +# disallow_untyped_defs = true +# warn_redundant_casts = true # warn_return_any = true warn_unused_configs = true +# warn_unused_ignores = true +# warn_unreachable = true exclude = [ '_pb2\.py$', # TOML literal string (single-quotes, no escaping necessary) ] @@ -20,3 +25,4 @@ exclude = '_pb2.py' [tool.isort] profile = "black" +skip_glob = ['*_pb2.py']