I suggest moving Pywikibot's linters/code formatters to pre-commit.
pre-commit runs configured hooks (.pre-commit-config.yaml) against files changed in a commit when you git commit .... (No more forgetting to run checks before sending for review.) It would also be run against all files in CI in case someone doesn't install it locally.
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/684050 is a proof of concept patch.
git-review -d 684050 # checkout the POC patch pip install pre-commit # Install pre-commit pre-commit install # Install pre-commit's pre-commit hook git commit ... # pre-commit will run on the files changed pre-commit uninstall # Unnstall pre-commit's pre-commit hook pre-commit run --all-files # Run pre-commit on all files
Hooks included in the POC patch to demonstrate functionality:
Include? | Hook | Description |
---|---|---|
Yes | check-hooks-apply | ensures that the configured hooks apply to at least one file in the repository. |
Yes | check-useless-excludes | ensures that exclude directives apply to any file in the repository. |
check-added-large-files | Prevent giant files from being committed. | |
check-ast | Simply check whether files parse as valid python. | |
check-builtin-literals | Require literal syntax when initializing empty or zero Python builtin types. | |
check-docstring-first | Checks for a common error of placing code before the docstring. | |
check-executables-have-shebangs | Checks that non-binary executables have a proper shebang. | |
check-json | Attempts to load all json files to verify syntax. | |
check-merge-conflict | Check for files that contain merge conflict strings. | |
check-shebang-scripts-are-executable | Checks that scripts with shebangs are executable. | |
check-symlinks | Checks for symlinks which do not point to anything. | |
check-toml | Attempts to load all TOML files to verify syntax. | |
check-vcs-permalinks | Ensures that links to vcs websites are permalinks. | |
check-xml | Attempts to load all xml files to verify syntax. | |
check-yaml | Attempts to load all yaml files to verify syntax. | |
debug-statements | Check for debugger imports and py37+ breakpoint() calls in python source. | |
destroyed-symlinks | Detects symlinks which are changed to regular files with a content of a path which that symlink was pointing to. This usually happens on Windows when a user clones a repository that has symlinks but they do not have the permission to create symlinks. | |
end-of-file-fixer | Makes sure files end in a newline and only a newline. | |
fix-byte-order-marker | removes UTF-8 byte order marker | |
fix-encoding-pragma | Remove # -*- coding: utf-8 -*- from the top of python files. | |
forbid-new-submodules | Prevent addition of new git submodules. | |
mixed-line-ending | Replaces or checks mixed line ending. | |
pretty-format-json | Checks that all your JSON files are pretty. "Pretty" here means that keys are sorted and indented. | |
trailing-whitespace | Trims trailing whitespace. | |
python-check-blanket-noqa | Enforce that noqa annotations always occur with specific codes. Sample annotations: # noqa: F401, # noqa: F401,W203 | |
python-check-blanket-type-ignore | Enforce that # type: ignore annotations always occur with specific codes. Sample annotations: # type: ignore[attr-defined], # type: ignore[attr-defined, name-defined] | |
python-check-mock-methods | Prevent common mistakes of assert mck.not_called(), assert mck.called_once_with(...) and mck.assert_called. | |
python-use-type-annotations | Enforce that python3.6+ type annotations are used instead of type comments | |
pyupgrade | Run pyupgrade | |
autoflake | Run autoflake | |
isort | Run isort | |
Yes | flake8 | Run flake8 |
Please take a look and share your thoughts about using pre-commit in Pywikibot development.
If we want to use it, which hooks do we want to include?