Skip to content

Commit 632f44b

Browse files
cobaltt7hauntsaninjaJelleZijlstra
authored
docs: Refactor pycodestyle/Flake8 compatibility docs (psf#4194)
Signed-off-by: RedGuy12 <[email protected]> Co-authored-by: Shantanu <[email protected]> Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 9728b8e commit 632f44b

File tree

10 files changed

+94
-97
lines changed

10 files changed

+94
-97
lines changed

.flake8

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[flake8]
22
# B905 should be enabled when we drop support for 3.9
3-
ignore = E203, E266, E501, E704, W503, B905, B907
3+
ignore = E203, E266, E501, E701, E704, W503, B905, B907
44
# line length is intentionally set to 80 here because black uses Bugbear
5-
# See https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#line-length for more details
5+
# See https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#bugbear for more details
66
max-line-length = 80
77
max-complexity = 18
88
select = B,C,E,F,W,T4,B9
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[flake8]
22
max-line-length = 88
3-
extend-ignore = E203
3+
extend-ignore = E203,E701
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[flake8]
22
max-line-length = 88
3-
extend-ignore = E203
3+
extend-ignore = E203,E701
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[flake8]
22
max-line-length = 88
3-
extend-ignore = E203
3+
extend-ignore = E203,E701
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pycodestyle]
2+
max-line-length = 88
3+
ignore = E203,E701
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pycodestyle]
2+
max-line-length = 88
3+
ignore = E203,E701
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pycodestyle]
2+
max-line-length = 88
3+
ignore = E203,E701

docs/faq.md

+3-6
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,10 @@ following will not be formatted:
7777
- invalid syntax, as it can't be safely distinguished from automagics in the absence of
7878
a running `IPython` kernel.
7979

80-
## Why are Flake8's E203 and W503 violated?
80+
## Why does Flake8 report warnings?
8181

82-
Because they go against PEP 8. E203 falsely triggers on list
83-
[slices](the_black_code_style/current_style.md#slices), and adhering to W503 hinders
84-
readability because operators are misaligned. Disable W503 and enable the
85-
disabled-by-default counterpart W504. E203 should be disabled while changes are still
86-
[discussed](https://github.com/PyCQA/pycodestyle/issues/373).
82+
Some of Flake8's rules conflict with Black's style. We recommend disabling these rules.
83+
See [Using _Black_ with other tools](labels/why-pycodestyle-warnings).
8784

8885
## Which Python versions does Black support?
8986

docs/guides/using_black_with_other_tools.md

+73-57
Original file line numberDiff line numberDiff line change
@@ -134,78 +134,126 @@ profile = black
134134

135135
</details>
136136

137-
### Flake8
137+
### pycodestyle
138138

139-
[Flake8](https://pypi.org/p/flake8/) is a code linter. It warns you of syntax errors,
140-
possible bugs, stylistic errors, etc. For the most part, Flake8 follows
139+
[pycodestyle](https://pycodestyle.pycqa.org/) is a code linter. It warns you of syntax
140+
errors, possible bugs, stylistic errors, etc. For the most part, pycodestyle follows
141141
[PEP 8](https://www.python.org/dev/peps/pep-0008/) when warning about stylistic errors.
142142
There are a few deviations that cause incompatibilities with _Black_.
143143

144144
#### Configuration
145145

146146
```
147147
max-line-length = 88
148-
extend-ignore = E203, E704
148+
ignore = E203,E701
149149
```
150150

151+
(labels/why-pycodestyle-warnings)=
152+
151153
#### Why those options above?
152154

155+
##### `max-line-length`
156+
157+
As with isort, pycodestyle should be configured to allow lines up to the length limit of
158+
`88`, _Black_'s default.
159+
160+
##### `E203`
161+
153162
In some cases, as determined by PEP 8, _Black_ will enforce an equal amount of
154-
whitespace around slice operators. Due to this, Flake8 will raise
155-
`E203 whitespace before ':'` warnings. Since this warning is not PEP 8 compliant, Flake8
156-
should be configured to ignore it via `extend-ignore = E203`.
163+
whitespace around slice operators. Due to this, pycodestyle will raise
164+
`E203 whitespace before ':'` warnings. Since this warning is not PEP 8 compliant, it
165+
should be disabled.
166+
167+
##### `E701` / `E704`
168+
169+
_Black_ will collapse implementations of classes and functions consisting solely of `..`
170+
to a single line. This matches how such examples are formatted in PEP 8. It remains true
171+
that in all other cases Black will prevent multiple statements on the same line, in
172+
accordance with PEP 8 generally discouraging this.
173+
174+
However, `pycodestyle` does not mirror this logic and may raise
175+
`E701 multiple statements on one line (colon)` in this situation. Its
176+
disabled-by-default `E704 multiple statements on one line (def)` rule may also raise
177+
warnings and should not be enabled.
178+
179+
##### `W503`
157180

158181
When breaking a line, _Black_ will break it before a binary operator. This is compliant
159182
with PEP 8 as of
160183
[April 2016](https://github.com/python/peps/commit/c59c4376ad233a62ca4b3a6060c81368bd21e85b#diff-64ec08cc46db7540f18f2af46037f599).
161184
There's a disabled-by-default warning in Flake8 which goes against this PEP 8
162185
recommendation called `W503 line break before binary operator`. It should not be enabled
163-
in your configuration.
164-
165-
Also, as like with isort, flake8 should be configured to allow lines up to the length
166-
limit of `88`, _Black_'s default. This explains `max-line-length = 88`.
186+
in your configuration. You can use its counterpart
187+
`W504 line break after binary operator` instead.
167188

168189
#### Formats
169190

170191
<details>
171-
<summary>.flake8</summary>
192+
<summary>setup.cfg, .pycodestyle, tox.ini</summary>
172193

173194
```ini
174-
[flake8]
195+
[pycodestyle]
175196
max-line-length = 88
176-
extend-ignore = E203, E704
197+
ignore = E203,E701
177198
```
178199

179200
</details>
180201

181-
<details>
182-
<summary>setup.cfg</summary>
202+
### Flake8
183203

184-
```ini
204+
[Flake8](https://pypi.org/p/flake8/) is a wrapper around multiple linters, including
205+
pycodestyle. As such, it has many of the same issues.
206+
207+
#### Bugbear
208+
209+
It's recommended to use [the Bugbear plugin](https://github.com/PyCQA/flake8-bugbear)
210+
and enable
211+
[its B950 check](https://github.com/PyCQA/flake8-bugbear#opinionated-warnings#:~:text=you%20expect%20it.-,B950,-%3A%20Line%20too%20long)
212+
instead of using Flake8's E501, because it aligns with
213+
[Black's 10% rule](labels/line-length).
214+
215+
Install Bugbear and use the following config:
216+
217+
```
218+
[flake8]
219+
max-line-length = 80
220+
extend-select = B950
221+
extend-ignore = E203,E501,E701
222+
```
223+
224+
#### Minimal Configuration
225+
226+
In cases where you can't or don't want to install Bugbear, you can use this minimally
227+
compatible config:
228+
229+
```
185230
[flake8]
186231
max-line-length = 88
187-
extend-ignore = E203, E704
232+
extend-ignore = E203,E701
188233
```
189234

190-
</details>
235+
#### Why those options above?
236+
237+
See [the pycodestyle section](labels/why-pycodestyle-warnings) above.
238+
239+
#### Formats
191240

192241
<details>
193-
<summary>tox.ini</summary>
242+
<summary>.flake8, setup.cfg, tox.ini</summary>
194243

195244
```ini
196245
[flake8]
197246
max-line-length = 88
198-
extend-ignore = E203, E704
247+
extend-ignore = E203,E701
199248
```
200249

201250
</details>
202251

203252
### Pylint
204253

205-
[Pylint](https://pypi.org/p/pylint/) is also a code linter like Flake8. It has the same
206-
checks as flake8 and more. In particular, it has more formatting checks regarding style
207-
conventions like variable naming. With so many checks, Pylint is bound to have some
208-
mixed feelings about _Black_'s formatting style.
254+
[Pylint](https://pypi.org/p/pylint/) is also a code linter like Flake8. It has many of
255+
the same checks as Flake8 and more. It particularly has more formatting checks regarding
256+
style conventions like variable naming.
209257

210258
#### Configuration
211259

@@ -252,35 +300,3 @@ max-line-length = "88"
252300
```
253301

254302
</details>
255-
256-
### pycodestyle
257-
258-
[pycodestyle](https://pycodestyle.pycqa.org/) is also a code linter like Flake8.
259-
260-
#### Configuration
261-
262-
```
263-
max-line-length = 88
264-
ignore = E203
265-
```
266-
267-
#### Why those options above?
268-
269-
pycodestyle should be configured to only complain about lines that surpass `88`
270-
characters via `max_line_length = 88`.
271-
272-
See
273-
[Why are Flake8’s E203 and W503 violated?](https://black.readthedocs.io/en/stable/faq.html#why-are-flake8-s-e203-and-w503-violated)
274-
275-
#### Formats
276-
277-
<details>
278-
<summary>setup.cfg</summary>
279-
280-
```cfg
281-
[pycodestyle]
282-
ignore = E203
283-
max_line_length = 88
284-
```
285-
286-
</details>

docs/the_black_code_style/current_style.md

+4-29
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ significantly shorter files than sticking with 80 (the most popular), or even 79
143143
by the standard library). In general,
144144
[90-ish seems like the wise choice](https://youtu.be/wf-BqAjZb8M?t=260).
145145

146-
If you're paid by the line of code you write, you can pass `--line-length` with a lower
146+
If you're paid by the lines of code you write, you can pass `--line-length` with a lower
147147
number. _Black_ will try to respect that. However, sometimes it won't be able to without
148148
breaking other rules. In those rare cases, auto-formatted code will exceed your allotted
149149
limit.
@@ -153,35 +153,10 @@ harder to work with line lengths exceeding 100 characters. It also adversely aff
153153
side-by-side diff review on typical screen resolutions. Long lines also make it harder
154154
to present code neatly in documentation or talk slides.
155155

156-
#### Flake8
156+
#### Flake8 and other linters
157157

158-
If you use Flake8, you have a few options:
159-
160-
1. Recommended is using [Bugbear](https://github.com/PyCQA/flake8-bugbear) and enabling
161-
its B950 check instead of using Flake8's E501, because it aligns with Black's 10%
162-
rule. Install Bugbear and use the following config:
163-
164-
```ini
165-
[flake8]
166-
max-line-length = 80
167-
...
168-
select = C,E,F,W,B,B950
169-
extend-ignore = E203, E501, E704
170-
```
171-
172-
The rationale for B950 is explained in
173-
[Bugbear's documentation](https://github.com/PyCQA/flake8-bugbear#opinionated-warnings).
174-
175-
2. For a minimally compatible config:
176-
177-
```ini
178-
[flake8]
179-
max-line-length = 88
180-
extend-ignore = E203, E704
181-
```
182-
183-
An explanation of why E203 is disabled can be found in the [Slices section](#slices) of
184-
this page.
158+
See [Using _Black_ with other tools](../guides/using_black_with_other_tools.md) about
159+
linter compatibility.
185160

186161
### Empty lines
187162

0 commit comments

Comments
 (0)