2
0

move flake8 and mypy to lints.sh; add fmts.sh; use more linters

This commit is contained in:
Stefan Bühler 2023-11-15 12:15:57 +01:00
parent 09ddea939c
commit fdd38d7498
6 changed files with 129 additions and 61 deletions

11
.pycodestyle Normal file
View File

@ -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

19
flake8
View File

@ -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

7
fmt.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
self=$(dirname "$(readlink -f "$0")")
cd "${self}"
python3 -m black src
python3 -m isort src

105
lints.sh Executable file
View File

@ -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

42
mypy
View File

@ -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

View File

@ -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']