psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
38.85k stars 2.45k forks source link

Incorrectly adding space in slice #1701

Closed Keou0007 closed 4 years ago

Keou0007 commented 4 years ago

I have a simple one liner that replaces the characters at the beginning of a string with a different (shorter) string. To achieve this I do a slice of the second string using a len call on the first string.

string2 = string1 + string2[len(string1):]

black incorrectly adds a space into the slice, like so

string2 = string1 + string2[len(string1) :]

PyCharm then highlights this as incorrect (PEP8: E203 whitespace before ':').

I'm using blackd server, version 20.8b1.

hugovk commented 4 years ago

This is by design, please see https://black.readthedocs.io/en/stable/the_black_code_style.html?highlight=E203#slices

ichard26 commented 4 years ago

Also a duplicate of #1541, #1448, #1437, #1413, #1343, #1332, #1323, #1029, #889, #785, #565, #544, #492, #403, #354, #350, #311, #279, #264, and #227...

contemplating why I decided to become a triager for Black

Anyway, please disable flake8's E203 as it is not PEP 8 compliant.

indepndnt commented 3 years ago

This is one of the places I ended up trying to figure this out for myself. So, for future travelers, at least as the time of this writing, here is how you disable E203 in PyCharm (2021.1.3):

  1. Open Settings (File>Settings, or Ctrl-Alt-S)
  2. Editor>Inspections>PEP 8 coding style violation (or search "pycodestyle")
  3. Under Options click "+" and add "E203"
  4. Click OK

image

jjbankert commented 2 years ago

This is by design, please see https://black.readthedocs.io/en/stable/the_black_code_style.html?highlight=E203#slices

That link was broken for me, this link works: https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html?highlight=E203#slices

codeskipper commented 3 months ago

See also Flake8 ignore list for Black compatibility #38