Skip to content

Commit ba94488

Browse files
Refs #33476 -- Adjusted docs and config files for Black.
Co-authored-by: Mariusz Felisiak <[email protected]>
1 parent 6f185a5 commit ba94488

File tree

9 files changed

+54
-45
lines changed

9 files changed

+54
-45
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ charset = utf-8
1212

1313
# Docstrings and comments use max_line_length = 79
1414
[*.py]
15-
max_line_length = 119
15+
max_line_length = 88
1616

1717
# Use 2 spaces for the HTML files
1818
[*.html]

.pre-commit-config.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
repos:
2+
- repo: https://github.com/psf/black
3+
rev: 22.1.0
4+
hooks:
5+
- id: black
26
- repo: https://github.com/PyCQA/isort
37
rev: 5.9.3
48
hooks:

docs/internals/contributing/writing-code/coding-style.txt

+14-28
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@ them.
3535
Python style
3636
============
3737

38-
* Please conform to the indentation style dictated in the ``.editorconfig``
39-
file. We recommend using a text editor with `EditorConfig`_ support to avoid
40-
indentation and whitespace issues. The Python files use 4 spaces for
41-
indentation and the HTML files use 2 spaces.
38+
* All files should be formatted using the `black`_ auto-formatter. This will be
39+
run by ``pre-commit`` if that is configured.
40+
41+
* The project repository includes an ``.editorconfig`` file. We recommend using
42+
a text editor with `EditorConfig`_ support to avoid indentation and
43+
whitespace issues. The Python files use 4 spaces for indentation and the HTML
44+
files use 2 spaces.
4245

4346
* Unless otherwise specified, follow :pep:`8`.
4447

@@ -51,33 +54,11 @@ Python style
5154

5255
An exception to :pep:`8` is our rules on line lengths. Don't limit lines of
5356
code to 79 characters if it means the code looks significantly uglier or is
54-
harder to read. We allow up to 119 characters as this is the width of GitHub
55-
code review; anything longer requires horizontal scrolling which makes review
56-
more difficult. This check is included when you run ``flake8``. Documentation,
57+
harder to read. We allow up to 88 characters as this is the line length used
58+
by ``black``. This check is included when you run ``flake8``. Documentation,
5759
comments, and docstrings should be wrapped at 79 characters, even though
5860
:pep:`8` suggests 72.
5961

60-
* Use four spaces for indentation.
61-
62-
* Use four space hanging indentation rather than vertical alignment::
63-
64-
raise AttributeError(
65-
'Here is a multiline error message '
66-
'shortened for clarity.'
67-
)
68-
69-
Instead of::
70-
71-
raise AttributeError('Here is a multiline error message '
72-
'shortened for clarity.')
73-
74-
This makes better use of space and avoids having to realign strings if the
75-
length of the first line changes.
76-
77-
* Use single quotes for strings, or a double quote if the string contains a
78-
single quote. Don't waste time doing unrelated refactoring of existing code
79-
to conform to this style.
80-
8162
* String variable interpolation may use
8263
:py:ref:`%-formatting <old-string-formatting>`, :py:ref:`f-strings
8364
<f-strings>`, or :py:meth:`str.format` as appropriate, with the goal of
@@ -146,6 +127,10 @@ Python style
146127
"""
147128
...
148129

130+
.. versionchanged:: 4.0.3
131+
132+
All Python code in Django was reformatted with `black`_.
133+
149134
.. _coding-style-imports:
150135

151136
Imports
@@ -397,5 +382,6 @@ JavaScript style
397382
For details about the JavaScript code style used by Django, see
398383
:doc:`javascript`.
399384

385+
.. _black: https://black.readthedocs.io/en/stable/
400386
.. _editorconfig: https://editorconfig.org/
401387
.. _flake8: https://pypi.org/project/flake8/

docs/internals/contributing/writing-code/submitting-patches.txt

+3-3
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ All code changes
290290

291291
* Does the :doc:`coding style
292292
</internals/contributing/writing-code/coding-style>` conform to our
293-
guidelines? Are there any ``flake8`` errors? You can install the
294-
:ref:`pre-commit <coding-style-pre-commit>` hooks to automatically catch
295-
these errors.
293+
guidelines? Are there any ``black``, ``flake8``, or ``isort`` errors? You
294+
can install the :ref:`pre-commit <coding-style-pre-commit>` hooks to
295+
automatically catch these errors.
296296
* If the change is backwards incompatible in any way, is there a note
297297
in the release notes (``docs/releases/A.B.txt``)?
298298
* Is Django's test suite passing?

docs/internals/contributing/writing-code/unit-tests.txt

+7-5
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,18 @@ command from any place in the Django source tree:
6969
$ tox
7070

7171
By default, ``tox`` runs the test suite with the bundled test settings file for
72-
SQLite, ``flake8``, ``isort``, and the documentation spelling checker. In
73-
addition to the system dependencies noted elsewhere in this documentation,
74-
the command ``python3`` must be on your path and linked to the appropriate
75-
version of Python. A list of default environments can be seen as follows:
72+
SQLite, ``black``, ``flake8``, ``isort``, and the documentation spelling
73+
checker. In addition to the system dependencies noted elsewhere in this
74+
documentation, the command ``python3`` must be on your path and linked to the
75+
appropriate version of Python. A list of default environments can be seen as
76+
follows:
7677

7778
.. console::
7879

7980
$ tox -l
8081
py3
81-
flake8
82+
black
83+
flake8>=3.7.0
8284
docs
8385
isort>=5.1.0
8486

docs/releases/4.0.3.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ Django 4.0.3 release notes
44

55
*Expected March 1, 2022*
66

7-
Django 4.0.3 fixes several bugs in 4.0.2.
7+
Django 4.0.3 fixes several bugs in 4.0.2. Also, all Python code in Django is
8+
reformatted with `black`_.
9+
10+
.. _black: https://pypi.org/project/black/
811

912
Bugfixes
1013
========

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
[build-system]
22
requires = ['setuptools>=40.8.0', 'wheel']
33
build-backend = 'setuptools.build_meta:__legacy__'
4+
5+
[tool.black]
6+
target-version = ['py38']
7+
extend-exclude = 'tests/test_runner_apps/tagged/tests_syntax_error.py'

setup.cfg

+8-6
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ install_script = scripts/rpm-install.sh
5757

5858
[flake8]
5959
exclude = build,.git,.tox,./tests/.env
60-
ignore = W504,W601
61-
max-line-length = 119
60+
extend-ignore = E203
61+
max-line-length = 88
62+
per-file-ignores =
63+
django/core/cache/backends/filebased.py:W601
64+
django/core/cache/backends/base.py:W601
65+
django/core/cache/backends/redis.py:W601
66+
tests/cache/tests.py:W601
6267

6368
[isort]
64-
combine_as_imports = true
69+
profile = black
6570
default_section = THIRDPARTY
66-
include_trailing_comma = true
6771
known_first_party = django
68-
line_length = 79
69-
multi_line_output = 5

tox.ini

+9-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ minversion = 3.18
88
skipsdist = true
99
envlist =
1010
py3
11-
flake8
11+
black
12+
flake8 >= 3.7.0
1213
docs
1314
isort >= 5.1.0
1415

@@ -31,6 +32,13 @@ changedir = tests
3132
commands =
3233
{envpython} runtests.py {posargs}
3334

35+
[testenv:black]
36+
basepython = python3
37+
usedevelop = false
38+
deps = black
39+
changedir = {toxinidir}
40+
commands = black --check --diff .
41+
3442
[testenv:flake8]
3543
basepython = python3
3644
usedevelop = false

0 commit comments

Comments
 (0)