Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrectly adds space in slice #1859

Closed
RadixSeven opened this issue Dec 4, 2020 · 7 comments
Closed

Incorrectly adds space in slice #1859

RadixSeven opened this issue Dec 4, 2020 · 7 comments
Labels
R: duplicate This issue or pull request already exists R: not a bug This is deliberate behavior of Black.

Comments

@RadixSeven
Copy link

Describe the bug
Black adds a space before the colon in a string slice with no upper bound
To Reproduce

a = "foo"
b = a[len(a) - 2:]

when run on black with no arguments produces

a = "foo"
b = a[len(a) - 2 :]

Expected behavior A clear and concise description of what you expected to happen.
The correct behavior would be to output

a = "foo"
b = a[len(a) - 2:]

Environment (please complete the following information):

  • Version: 20.8b1
  • OS and Python version: Linux/Python 3.8.1

Does this bug also happen on master? To answer this, you have two options:
Yes

Additional context Add any other context about the problem here.
flake8 and PyCharm both report "E203 whitespace before ':'" with Black's current output

The correct behavior is inferred from this example from the pep8 document ham[1:9:]

I'm currently working around this with # fmt: off since our files have to pass flake8.

@RadixSeven RadixSeven added the T: bug Something isn't working label Dec 4, 2020
@ichard26 ichard26 added R: not a bug This is deliberate behavior of Black. and removed T: bug Something isn't working labels Dec 4, 2020
@ichard26
Copy link
Collaborator

ichard26 commented Dec 4, 2020

There is no end to this torment of E203 bug reports ... :D

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

Since I honestly don't care about these issues anymore, imma copy and paste what's in our docs (emphasis mine):

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 recommends no spaces around : operators for “simple expressions” (ham[lower:upper]), and extra space for “complex expressions” (ham[lower : upper + offset]). Black treats anything more than variable names as “complex” (ham[lower : upper + 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.

https://black.readthedocs.io/en/stable/the_black_code_style.html#slices

Have a good day! At this point it's just plain funny (minus the notification)!

@ichard26 ichard26 closed this as completed Dec 4, 2020
@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label Dec 4, 2020
@RadixSeven
Copy link
Author

Sorry, I didn't search for E203. :(

mambelli added a commit to mambelli/decisionengine that referenced this issue Oct 25, 2021
mambelli added a commit to mambelli/decisionengine_modules that referenced this issue Oct 25, 2021
mambelli added a commit to HEPCloud/decisionengine_modules that referenced this issue Oct 25, 2021
mambelli added a commit to HEPCloud/decisionengine that referenced this issue Oct 25, 2021
TaiSakuma added a commit to simonsobs/nextline that referenced this issue Feb 1, 2022
@SmartManoj
Copy link

SmartManoj commented Apr 9, 2022

https://black.readthedocs.io/en/stable/the_black_code_style.html#slices

https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#slices

and extra space for “complex expressions”
Is it now?

For (ham[lower : upper + offset]), (ham[lower : upper+offset]) is valid.
Sample ham[lower+offset : upper+offset]

@SmartManoj
Copy link

@SmartManoj
Copy link

SmartManoj commented Apr 9, 2022

b = a[len(a) - 2:]

The expected behavior now is
b = a[len(a)-2 :]

Related issue in pycodestyle

@drorata
Copy link

drorata commented May 19, 2022

For our future-selves, adding:

[flake8]
ignore = E203

to setup.cfg configures flake8 to ignore the error (ref)

@hugovk
Copy link
Contributor

hugovk commented May 19, 2022

@drorata Note ignore overwrites the default list (currently E226, E24, W504, E704, E123, E126, W503, E121).

You may want to use extend-ignore which appends to to the default list.

mgxd added a commit to mgxd/fmriprep that referenced this issue Sep 22, 2022
shyuldashov referenced this issue in shyuldashov/algorithmic_patterns Sep 27, 2024
- "Subarrays with Product Less than a Target"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R: duplicate This issue or pull request already exists R: not a bug This is deliberate behavior of Black.
Projects
None yet
Development

No branches or pull requests

5 participants