From 40891fcb4a0edeb5c2e495286d117f256a3ba522 Mon Sep 17 00:00:00 2001 From: medmunds Date: Mon, 6 Feb 2023 12:26:46 -0800 Subject: [PATCH] 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 --- .editorconfig | 54 ++++++++++++++++++++++++++++++++--------- .flake8 | 11 ++++++++- .pre-commit-config.yaml | 52 +++++++++++++++++++++++++++++++++++++++ CHANGELOG.rst | 11 +++++++++ docs/contributing.rst | 6 +++-- pyproject.toml | 19 +++++++++++++++ setup.py | 2 +- tox.ini | 15 +++++++++--- 8 files changed, 150 insertions(+), 20 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 pyproject.toml diff --git a/.editorconfig b/.editorconfig index 93c5188..296192b 100644 --- a/.editorconfig +++ b/.editorconfig @@ -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 diff --git a/.flake8 b/.flake8 index 6deafc2..cc68351 100644 --- a/.flake8 +++ b/.flake8 @@ -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 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..c2504f8 --- /dev/null +++ b/.pre-commit-config.yaml @@ -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 diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c78eadb..320312d 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 ---- diff --git a/docs/contributing.rst b/docs/contributing.rst index 3cf0bae..bb0a3a2 100644 --- a/docs/contributing.rst +++ b/docs/contributing.rst @@ -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 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..caffbd9 --- /dev/null +++ b/pyproject.toml @@ -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" diff --git a/setup.py b/setup.py index ea73e58..60c1c41 100644 --- a/setup.py +++ b/setup.py @@ -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", diff --git a/tox.ini b/tox.ini index c800dad..7a2d568 100644 --- a/tox.ini +++ b/tox.ini @@ -74,14 +74,21 @@ basepython = python3.8 skip_install = true passenv = CONTINUOUS_INTEGRATION - # (but not any of the live test API keys) + # 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