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

added whitespace in ranged sequence access #279

Closed
RonnyPfannschmidt opened this issue May 30, 2018 · 7 comments
Closed

added whitespace in ranged sequence access #279

RonnyPfannschmidt opened this issue May 30, 2018 · 7 comments

Comments

@RonnyPfannschmidt
Copy link

from https://github.com/tox-dev/tox/pull/833/files/f30411399372e65016772b5da8c38cd39620ef1f#diff-4c32e51992275934b9b1b0b3838a021e

$ python --version
Python 3.6.5
$ cat /etc/os-release |grep VERSION
VERSION="27 (Workstation Edition)"
$ (cd black && git rev-parse HEAD)
608019dffb1f933299f5682498548d375faf152e
$ pip install -e black
$ black --version
black, version 18.5b1
$ echo 'a[len(b):]' > example.py
$ black --diff example.py 
--- example.py  (original)
+++ example.py  (formatted)
@@ -1,2 +1,2 @@
-a[len(b):]
+a[len(b) :]
 
reformatted example.py
@ambv
Copy link
Collaborator

ambv commented May 30, 2018

Thanks for your report. This stems from our implementation of PEP 8-compliant slicing (see #178). I'm not sure what to think about this example. It looks weird at first but if you accept that colon is now treated as a low-priority operator (if operands are complex), it makes sense.

PEP 8 gives following examples as "Yes":

ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]

@RonnyPfannschmidt
Copy link
Author

RonnyPfannschmidt commented May 30, 2018

@ambv thanks for the note, in that case flake8 is wrong

as

$ cat example.py
a = []
b = []
a[len(b) :]
$ flake8 example.py 
example.py:3:9: E203 whitespace before ':'

@zsol
Copy link
Collaborator

zsol commented May 30, 2018 via email

@ambv
Copy link
Collaborator

ambv commented May 30, 2018

Yes, @RonnyPfannschmidt, that warning was originally designed to avoid spaces before colons in defs, if statements, and so on. It's an unfortunate coincidence that it also covers slicing (and wrong!).

@bittner
Copy link

bittner commented Dec 12, 2020

It's an unfortunate coincidence that it also covers slicing (and wrong!).

Can you please prove your claim that this is wrong?

The official Python tutorial doesn't use spaces for slicing. Is it because PEP8 says, "should have equal amounts on either side"?

@ichard26
Copy link
Collaborator

Here's our explanation in our docs:

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.

https://github.com/psf/black/blob/master/docs/the_black_code_style.md#slices

And here's the wording from PEP 8 our explanation is based off:

However, in a slice the colon acts like a binary operator, and should have equal amounts on either side (treating it as the operator with the lowest priority). In an extended slice, both colons must have the same amount of spacing applied. Exception: when a slice parameter is omitted, the space is omitted:

And here's an example PEP 8 explicitly says "yes" to:

ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]

https://www.python.org/dev/peps/pep-0008/#whitespace-in-expressions-and-statements (scroll after 3 bullet points and you should see it)

See also: PyCQA/pycodestyle#373

@sgclacke
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants