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

Flake8 and PEP8 has different rule for the whitespace around colon in a slice. #257

Closed
asottile opened this issue Apr 3, 2021 · 8 comments

Comments

@asottile
Copy link
Member

asottile commented Apr 3, 2021

In GitLab by @ocavue on Sep 16, 2018, 09:31

description

I found that PEP8 has some complex rules for the whitespace around colon in a slice:

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.

Yes:

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]  # see this line

No:

ham[lower + offset:upper + offset]  # see this line
ham[1: 9], ham[1 :9], ham[1:9 :3]
ham[lower : : upper]
ham[ : upper]

But flake8's rule seems to be simpler:

$ cat ./test.py
from foo import ham, lower, upper, offset

ham[lower:upper]  # correct for both PEP8 and Flake8
ham[lower : upper]  # wrong fot both PEP8 and Flake8
ham[lower + offset : upper + offset]  # correct for PEP8, wrong for Flake8
ham[lower + offset:upper + offset]  # wrong for PEP8, correct for Flake8
$ flake8 ./test.py
./test.py:4:10: E203 whitespace before ':'
./test.py:5:19: E203 whitespace before ':'

I found this issue because black formats my code into PEP8 style, which causes the E203 error.

I don't know the relationship between Flake8 and PEP8, so I'm not sure if it's a bug.

version

$ flake8 --version
3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.7.0 on Darwin
@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @sigmavirus24 on Oct 20, 2018, 09:58

closed

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @cdetar on Oct 20, 2018, 13:52

Looks like PyCQA/pycodestyle#373 is the pertinent issue.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @aniforprez on Feb 6, 2019, 22:22

Isn't the issue with flake8 since it's signalling errors for the slice syntax when it's actually pep8 compliant? Flake8 should not show the error because pycodestyle is doing the pep8 compliant thing.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Feb 6, 2019, 23:30

@aniforprez (from filing a bug linked above) (emphasis mine)

If your bug is in reference to one of the checks that Flake8 reports by default, please do not report them to Flake8 unless Flake8 is doing something to prevent the check from running or you have some reason to believe Flake8 is inhibiting the effectiveness of the check.

flake8 out-of-the-box is a thin wrapper around pycodestyle / pyflakes / mccabe (+ many more if you involve plugins) and has little control over the implementation of their linting rules. If pycodestyle reports a code, generally flake8 will as well because it is a simple wrapper.

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @aniforprez on Feb 10, 2019, 21:45

Ah all right I was not aware of that. My mistake

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @ki4070ma on Jan 30, 2020, 18:51

In below case, should flake8 report E203 error, right?

➜  ~ pycodestyle --version
2.5.0
➜  ~ pycodestyle <(echo "spam[1 : 2]")
/proc/self/fd/11:1:7: E203 whitespace before ':'
➜  ~ flake8 --version
3.7.9 (mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.6.10 on Linux
➜  ~ flake8 <(echo "spam[1 : 2]")
➜  ~ 

@asottile
Copy link
Member Author

asottile commented Apr 3, 2021

In GitLab by @asottile on Jan 30, 2020, 19:18

@ki4070ma since there's not enough info I assume you have a configuration file in play (check tox.ini / setup.cfg / .flake8 )

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

1 participant