Apply code styles via pre-commit

- Add black and isort for Python styling
- Add doc8 for reStructuredText styling
- Add prettier for css/html/js/md/yaml styling
- Run all styling tools from pre-commit hooks
- Adjust flake8 config for compatibility with black
  (and current Django style)
- Add some other helpful pre-commit hooks
- Update editorconfig to match Django
  (with a few necessary adjustments for Anymail)
- Update `tox -e lint` to run all pre-commit hooks
- Update contributing docs
This commit is contained in:
medmunds
2023-02-06 12:26:46 -08:00
committed by Mike Edmunds
parent 52c7c11312
commit 40891fcb4a
8 changed files with 150 additions and 20 deletions

View File

@@ -1,25 +1,55 @@
# https://editorconfig.org
# https://editorconfig.org/
# This is adapted from Django's .editorconfig:
# https://github.com/django/django/blob/main/.editorconfig
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true
[*.py]
indent_size = 4
insert_final_newline = true
max_line_length = 88
trim_trailing_whitespace = true
end_of_line = lf
charset = utf-8
# Match pyproject.toml [tool.black] config:
[*.py]
max_line_length = 88
# Match pyproject.toml [tool.doc8] config:
[*.rst]
max_line_length = 120
[*.{ini,lock}]
indent_size = 4
[*.md]
indent_size = 2
[{Makefile,*.bat}]
[*.html]
indent_size = 2
# Anymail uses smaller indents than Django in css and js sources
[*.css]
indent_size = 2
[*.js]
indent_size = 2
[*.json]
indent_size = 2
# Minified files shouldn't be changed
[**.min.{css,js}]
indent_style = ignore
insert_final_newline = ignore
# Makefiles always use tabs for indentation
[Makefile]
indent_style = tab
indent_size = 4
# Batch files use tabs for indentation
[*.bat]
end_of_line = crlf
indent_style = tab
[*.{yml,yaml}]
indent_size = 2

11
.flake8
View File

@@ -1,2 +1,11 @@
[flake8]
max-line-length = 120
extend-exclude =
build,
tests/test_settings/settings_*.py
# Black compatibility:
# - E203 (spaces around slice operators) is not PEP-8 compliant (and Black _is_)
# - Black sometimes deliberately overruns max-line-length by a small amount
# (97 is Black's max-line-length of 88 + 10%)
extend-ignore = E203
max-line-length = 97

52
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,52 @@
repos:
- repo: https://github.com/psf/black
rev: 23.1.0
hooks:
- id: black
- repo: https://github.com/pycqa/isort
rev: 5.12.0
hooks:
- id: isort
- repo: https://github.com/pycqa/flake8
rev: 6.0.0
hooks:
- id: flake8
- repo: https://github.com/pycqa/doc8
rev: v1.1.1
hooks:
- id: doc8
- repo: https://github.com/pre-commit/mirrors-prettier
# rev: see
# https://github.com/pre-commit/mirrors-prettier/issues/29#issuecomment-1332667344
rev: v2.7.1
hooks:
- id: prettier
files: '\.(css|html|jsx?|md|tsx?|ya?ml)$'
additional_dependencies:
- prettier@2.8.3
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: python-check-blanket-noqa
- id: python-check-blanket-type-ignore
- id: python-no-eval
- id: python-no-log-warn
- id: python-use-type-annotations
# - id: rst-backticks
# (no: some docs source uses single backticks expecting Sphinx default_role)
- id: rst-directive-colons
- id: rst-inline-touching-normal
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: check-json
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
- id: fix-byte-order-marker
- id: fix-encoding-pragma
args: [--remove]
- id: forbid-submodules
- id: mixed-line-ending
- id: requirements-txt-fixer
- id: trailing-whitespace

View File

@@ -25,6 +25,17 @@ Release history
^^^^^^^^^^^^^^^
.. This extra heading level keeps the ToC from becoming unmanageably long
vNext
-----
*Unreleased changes*
Other
~~~~~
* Use black, isort and doc8 to format code,
enforced via pre-commit. (Thanks to `@tim-schilling`_.)
v9.0
----

View File

@@ -51,8 +51,8 @@ Pull requests
Pull requests are always welcome to fix bugs and improve support for ESP and Django features.
* Please include test cases.
* We try to follow the `Django coding style`_
(basically, :pep:`8` with longer lines OK).
* We try to follow the `Django coding style`_.
* If you install `pre-commit`_, most of the style guidelines will be handled automatically.
* By submitting a pull request, you're agreeing to release your changes under under
the same BSD license as the rest of this project.
* Documentation is appreciated, but not required.
@@ -61,6 +61,8 @@ Pull requests are always welcome to fix bugs and improve support for ESP and Dja
.. Intentionally point to Django dev branch for coding docs (rather than Django stable):
.. _Django coding style:
https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/coding-style/
.. _pre-commit:
https://pre-commit.com/
Testing

19
pyproject.toml Normal file
View File

@@ -0,0 +1,19 @@
[tool.black]
force-exclude = '^/tests/test_settings/settings_.*\.py'
max-line-length = 88
target-version = ["py36"]
[tool.doc8]
# ignore very long lines in ESP support table:
ignore-path-errors = ["docs/esps/index.rst;D001"]
# for now, Anymail allows longer lines in docs source:
max-line-length = 120
[tool.flake8]
# See .flake8 file in project root
[tool.isort]
combine_as_imports = true
known_first_party = "anymail"
profile = "black"
py_version = "36"

View File

@@ -32,7 +32,7 @@ with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
# Additional requirements for development/build/release
requirements_dev = [
"flake8",
"pre-commit",
"sphinx",
"sphinx-rtd-theme",
"tox",

13
tox.ini
View File

@@ -74,14 +74,21 @@ basepython = python3.8
skip_install = true
passenv =
CONTINUOUS_INTEGRATION
# Make sure pre-commit can clone hook repos over ssh or http proxy.
# https://pre-commit.com/#usage-with-tox
SSH_AUTH_SOCK
http_proxy
https_proxy
no_proxy
# (but not any of the live test API keys)
deps =
flake8
pre-commit
commands_pre =
python -VV
flake8 --version
pre-commit --version
commands =
flake8
pre-commit validate-config
pre-commit run --all-files
[testenv:docs]
basepython = python3.8