Browse Source

Add and configure black

This:
- updates the pre-commit configuration and setup.py testing dependencies
- updates isort/flake8 configuration for black
- adds black linting to Makefile and CircleCI configuration
- updates editorconfig with the new line length (88) for py files
- updates python guidelines in docs
Dan Braghis 3 years ago
parent
commit
6dae6e5d07
7 changed files with 34 additions and 16 deletions
  1. 1 0
      .circleci/config.yml
  2. 7 2
      .editorconfig
  3. 6 0
      .pre-commit-config.yaml
  4. 2 1
      Makefile
  5. 10 4
      docs/contributing/python_guidelines.rst
  6. 7 9
      setup.cfg
  7. 1 0
      setup.py

+ 1 - 0
.circleci/config.yml

@@ -21,6 +21,7 @@ jobs:
             - .venv
       - run: pipenv run flake8
       - run: pipenv run isort --check-only --diff .
+      - run: pipenv run black --target-version py37 --check --diff .
       # Filter out known false positives, while preserving normal output and error codes.
       # See https://github.com/motet-a/jinjalint/issues/18.
       # And https://circleci.com/docs/2.0/configuration-reference/#default-shell-options.

+ 7 - 2
.editorconfig

@@ -9,10 +9,15 @@ indent_style = space
 [Makefile]
 indent_style = tab
 
-[*.{js,py}]
+[*.py]
+charset = utf-8
+indent_size = 4
+max_line_length = 88
+
+[*.js]
 charset = utf-8
 
-[*.{py,html,rst,md}]
+[*.{html,rst,md}]
 indent_size = 4
 
 [*.{js,ts,tsx,json,yml,yaml,css,scss}]

+ 6 - 0
.pre-commit-config.yaml

@@ -1,6 +1,12 @@
 default_language_version:
   node: system
 repos:
+  - repo: https://github.com/psf/black
+    rev: 22.1.0
+    hooks:
+      - id: black
+        language_version: python3
+        args: ['--target-version', 'py37']
   - repo: https://github.com/timothycrosley/isort
     # isort config is in setup.cfg
     rev: 5.6.4

+ 2 - 1
Makefile

@@ -3,7 +3,7 @@
 help:
 	@echo "clean-pyc - remove Python file artifacts"
 	@echo "develop - install development dependencies"
-	@echo "lint - check style with flake8, sort python with isort, indent html, and lint frontend css/js"
+	@echo "lint - check style with black, flake8, sort python with isort, indent html, and lint frontend css/js"
 	@echo "test - run tests"
 	@echo "coverage - check code coverage"
 
@@ -17,6 +17,7 @@ develop: clean-pyc
 	npm install --no-save && npm run build
 
 lint:
+	black --target-version py37 --check --diff .
 	flake8
 	isort --check-only --diff .
 	# Filter out known false positives, while preserving normal output and error codes.

+ 10 - 4
docs/contributing/python_guidelines.rst

@@ -4,11 +4,17 @@ Python coding guidelines
 PEP8
 ~~~~
 
-We ask that all Python contributions adhere to the `PEP8 <https://www.python.org/dev/peps/pep-0008/>`_ style guide, apart from the restriction on line length (E501) and some minor docstring-related issues.
-The list of PEP8 violations to ignore is in the ``setup.cfg`` file, under the ``[flake8]`` header.
-You might want to configure the flake8 linter in your editor/IDE to use the configuration in this file.
+We ask that all Python contributions adhere to the `PEP8 <https://www.python.org/dev/peps/pep-0008/>`_ style guide.
+All files should be formatted using the `black <https://github.com/psf/black>`_ auto-formatter. This will be
+run by ``pre-commit`` if that is configured.
 
-In addition, import lines should be sorted according to `isort <https://pycqa.github.io/isort/>`_ 5.6.4 rules. If you have installed Wagtail's testing dependencies (``pip install -e .[testing]``), you can check your code by running ``make lint``.
+* The project repository includes an ``.editorconfig`` file. We recommend using
+  a text editor with `EditorConfig <https://editorconfig.org/>`_ support to avoid indentation and
+  whitespace issues. Python and HTML files use 4 spaces for indentation.
+
+In addition, import lines should be sorted according to `isort <https://pycqa.github.io/isort/>`_ 5.6.4 rules.
+If you have installed Wagtail's testing dependencies (``pip install -e '.[testing]'``), you can check your code by
+running ``make lint``.
 
 Django compatibility
 ~~~~~~~~~~~~~~~~~~~~

+ 7 - 9
setup.cfg

@@ -7,26 +7,24 @@ python-tag = py3
 # D102: Missing docstring in public method
 # D103: Missing docstring in public function
 # D105: Missing docstring in magic method
-# E501: Line too long
 # W503: line break before binary operator (superseded by W504 line break after binary operator)
 # N806: Variable in function should be lowercase
-ignore = D100,D101,D102,D103,D105,E501,W503,N806
+# E203: Whitespace before ':'
+# E501: Line too long
+ignore = D100,D101,D102,D103,D105,W503,N806,E203,E501
 exclude = wagtail/project_template/*,node_modules,venv,.venv
-max-line-length = 120
+max-line-length = 88
 
 [doc8]
 ignore = D001
 ignore-path = _build,docs/_build
 
 [isort]
-line_length=100
-multi_line_output=4
-skip=migrations,project_template,node_modules,.git,__pycache__,LC_MESSAGES,venv,.venv
-blocked_extensions=rst,html,js,svg,txt,css,scss,png,snap,tsx
+profile = black
+skip=migrations,project_template,node_modules,.git,__pycache__,LC_MESSAGES,venv,.venv,.tox
+blocked_extensions=rst,html,js,svg,txt,css,scss,png,snap,ts,tsx
 known_first_party=wagtail
 default_section=THIRDPARTY
-lines_between_types=1
-lines_after_imports=2
 
 [tool:pytest]
 django_find_project = false

+ 1 - 0
setup.py

@@ -55,6 +55,7 @@ testing_extras = [
 
     # For coverage and PEP8 linting
     'coverage>=3.7.0',
+    'black==22.1.0',
     'flake8>=3.6.0',
     'isort==5.6.4',  # leave this pinned - it tends to change rules between patch releases
     'flake8-blind-except==0.1.1',