google / google/yapf

yapf is not compatible with PEP 8

Open
#909 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
14k
Forks
904
PR merge metrics
No merged PRs in 30d

Description

Hi, thanks for the great tool.

In PEP 8, there is a style guide for whitespace in expressions when using [slice operators](https://www.python.org/dev/peps/pep-0008/#pet-peeves). It says:

> 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:

``` python
# Correct:
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]

# Wrong:
ham[lower + offset:upper + offset]
ham[1: 9], ham[1 :9], ham[1:9 :3]
ham[lower : : upper]
ham[ : upper]
```

However, even I gave an option `--style pep8`, yapf fix the above correct code snippet into the following:
(by running `diff <(cat t.py) <(yapf --style pep8 t.py)`

``` diff
--- /proc/self/fd/14 2021-03-31 15:05:58.938191854 +0900
+++ /proc/self/fd/16 2021-03-31 15:05:58.934191830 +0900
@@ -1,5 +1,5 @@
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
-ham[lower+offset : upper+offset]
-ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
-ham[lower + offset : upper + offset]
+ham[lower + offset:upper + offset]
+ham[:upper_fn(x):step_fn(x)], ham[::step_fn(x)]
+ham[lower + offset:upper + offset]
```

I think this is not compatible with PEP 8.
Could you please let me understand why this is not compatible with PEP 8? Maybe there are some reasons for this, or maybe this is a bug?
Thank you.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.