Black: Black and Flake8 don't agree on one particular formatting

Created on 20 May 2020  路  2Comments  路  Source: psf/black

There seems to be one instance in our code that triggers a reformat in Black which is not approved by flake8. Given the line

yield lst[i:i + n]

Black will reformat this to

yield lst[i : i + n]

This will trigger an error in flake8 however:

E203 whitespace before ':'

This is with black version 19.10b0 and flake 3.8.1 on Python 3.6.10

bug

Most helpful comment

This is intentional:

(And a duplicate: https://github.com/psf/black/issues?q=is%3Aissue+sort%3Aupdated-desc+is%3Aclosed+E203 :)

All 2 comments

This is intentional:

(And a duplicate: https://github.com/psf/black/issues?q=is%3Aissue+sort%3Aupdated-desc+is%3Aclosed+E203 :)

note that the above link is no longer valid, I believe this is the new url: https://black.readthedocs.io/en/stable/the_black_code_style.html#slices

Slices
PEP 8 recommends to treat : in slices as a binary operator with the lowest priority, and to leave an equal amount of space on either side, except if a parameter is omitted (e.g. ham[1 + 1 :]). It also states that for extended slices, both : operators have to have the same amount of spacing, except if a parameter is omitted (ham[1 + 1 ::]). Black enforces these rules consistently.
This behaviour may raise E203 whitespace before ':' warnings in style guide enforcement tools like Flake8. Since E203 is not PEP 8 compliant, you should tell Flake8 to ignore these warnings.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

brettcannon picture brettcannon  路  3Comments

dgnsrekt picture dgnsrekt  路  3Comments

kissgyorgy picture kissgyorgy  路  3Comments

dusty-phillips picture dusty-phillips  路  3Comments

J0 picture J0  路  3Comments