-
Notifications
You must be signed in to change notification settings - Fork 752
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
Fix false alarm in E203 rule (issue #373, black incompatibility) #914
base: main
Are you sure you want to change the base?
Conversation
E203 checks for extraneous whitespace before a colon. Yet the colon can be used as a "slice" operator, and as such PEP8 asks for 0 or 1 whitespaces on both sides of the operator.
does this handle: |
@asottile good point. |
does this handle: |
@asottile yup |
?
I expect the inner |
the PR modifies only the portion of code that treats extra whitespaces, it shouldn't change current behaviour about missing whitespaces. This seems to be a different issue in pycodestyle, indeed the current version on branch master doesn't detect anything in this case either :
|
Hi |
@asottile is it ok for you ? |
sorry I haven't had a chance to look at the updated code |
Hi @asottile, I hope you're ok in these troubled times. |
I don't see how this is a blocker, you can use the suggested flake8 configuration from black this change is quite complicated (adds a lot of edges and magic numbers) and I have not had time to look at every fine detail of it |
I understand that this is not trivial to deploy. We have a lot of projects dispersed in many teams, I can't promote a seamless integration of black if I ask to tweak flake8 configuration on every one of them. |
why not? you'll have to modify all of the code in the repository and add some configuration for black already |
not necessarily, black can be used out of the box without config |
In my opinion, the fact of how the current E203 rule deals with slices is an unintended consequence. So I agree that the fix is needed. Edit: to illustrate, our pipeline fails at
which is very obviously PEP8-compliant, even listed as a correct usage example here. |
It seems there is consensus that E203 is not a PEP8 style compliance error and should be disabled by default. The presence of the rule for E203 in the default configuration prevents flake8 and black, among other tools, from interoperating smoothly. Could the maintainer please consider merging this PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me! I had one very minor suggestion re: formatting of multi-line conditions, but it's not a big deal at all.
I'm also not quite sure it's for me to approve the PR, so I'm commenting instead of Approving so I don't step on anyone's toes.
elif (line[found - 1] != ',' and | ||
not (char == ':' and is_a_slice(line, found))): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
elif (line[found - 1] != ',' and | |
not (char == ':' and is_a_slice(line, found))): | |
elif (line[found - 1] != ',' | |
and not (char == ':' and is_a_slice(line, found))): |
I was under the impression that 'pythonic' style puts the binary operator after a line break, not before.
--- Personal preference / opinion only below this line. Disregard at your pleasure ---
I personally also prefer to format multi-line conditions like this:
elif (
line[found - 1] != ','
and not (char == ':' and is_a_slice(line, found))
):
because it puts co-ordinate conditions in a visually co-ordinate structure, and also has dedicated lines to mark the bounds of the compound condition. (i.e. the elif (
and ):
lines, visually positioned to encapsulate the conditions and indicate that they're subordinate to the elif
construct itself. It does take up a lot more vertical space though, so I can understand why some might dislike that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guess this is the right pep for this:
https://www.python.org/dev/peps/pep-0008/#should-a-line-break-before-or-after-a-binary-operator
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hoylemd I agree and prefer this coding style as well.
Yet W503 in TravisCI prevents me of doing so :
pycodestyle.py:489:13: W503 line break before binary operator
pycodestyle.py:512:13: W503 line break before binary operator
If @s-weigand suggestion to ignore W503 was accepted then we could proceed :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was under the impression that 'pythonic' style puts the binary operator after a line break, not before.
This was changed only a few years back (roughly when W504 was introduced and both were added to the default ignore list). pycodestyle itself does not use this style of line breaks before binary operators and you should always adhere to the project's style.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for the precision
Then I guess the PR is ok for review
Since Line 185 in 203041b
IMHO it should also be added to the tests config Line 9 in 6a56d22
Or maybe just use the default ignore. |
This is a temporary fix until the issue is resolved, see PyCQA/pycodestyle#914
This is a temporary fix until the issue is resolved, see PyCQA/pycodestyle#914
The bug is being discussed at: PyCQA/pycodestyle#914
What's the status of this? |
* Add Python Black format check and apply format changes Bring this repo in line with our standards to use Python Black formatting conventions, and enforce it in the lint checks. * Split lint from tests and only run on 3.9 (black requires 3.6+) * Ignore spurious E203 from flake8 (see PyCQA/pycodestyle#914)
Hi @asottile, |
Issue #373
Rule E203 checks for extraneous whitespace before a colon.
Yet there is a corner case when the colon is used as a "slice" operator. PEP8 says:
This behaviour isn't implemented and leads to conflict between pycodestyle and black.
Solution
This PR adds to pycodestyle the tolerance for the slice situation, without enforcing the "same amount of space on both sides of the colon" rule.
This way the current Python that code was pycodestyle compliant stays compliant.
Performance
On a Python code base of over 15k lines, a test with hyperfine shows no performance loss.