-
Notifications
You must be signed in to change notification settings - Fork 749
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
E303: Does not report, that a blank line between classes is needed, if they're defined inside an if statement #366
Comments
That will define the bindings |
@sigmavirus24 Hmm, I wonder about that as well. Whatever the PEP8 actually means, the self.y = y
class Lifetime:
def __init__(self, lifetime): Even without the blank line after the class Lifetime:
def __init__(self, lifetime):
if lifetime < 0:
raise ValueError('lifetime must be above 0')
self.lifetime = lifetime
def do_stuff(self):
pass
class Name:
def __init__(self, name):
self.name = '"' + name + '"' |
So that's fine if that's your preference. Does the latter raise a warning or error from pep8 though? If not, just go ahead and do it until we can clear up what the document is actually suggesting. I personally don't feel too strongly about this matter so either outcome is fine by me. I just want to get it right and not add additional checks that will upset developers. |
For this if True:
class Lifetime:
def __init__(self, lifetime):
if lifetime < 0:
raise ValueError('lifetime must be above 0')
self.lifetime = lifetime
def do_stuff(self):
pass
class Name:
def __init__(self, name):
self.name = '"' + name + '"'
|
I would tend to think that the document is referring to "top-level" as an indentation, and therefore this previous example is "global" but not "top-level". I agree this is something that could be clarified in the document. |
@IanLee1521 I'm no authority on this nor am I criticising you, but I don't think this makes sense (and it looks weird with one blank line). How do we know if it is referring to indentation and not scope? And what could the logic behind "only one blank line between classes within an |
For cases like the one described here, "do whatever seems most readable in the specific situation" is about as good as the PEP can do - there are too many variables affecting readability once you move the opening header line for class and function definitions away from the left hand margin. In particular, the PEP guidelines on vertical whitespace are designed to create coherency within class and function definitions by creating additional space between them. That's desirable at the module top level, but when a suite is involved, excessive use of vertical whitespace means you end up reducing the coherency of the suite. So once you get beyond unindented code, the overarching guidelines on when to ignore other PEP guidelines are far more likely to come into play. In particular, this one: "[Don't apply PEP 8 guidelines when] ... applying the guideline would make the code less readable, even for someone who is used to reading code that follows this PEP". |
From a pep8 perspective, I think "no opinion" is the right approach here - folks that want a tool that also covers cases where PEP 8 defers to the developer's judgement would likely be better served by starting with pylint's expansive default settings, and tuning those down to a subset they like. |
@ncoghlan Hmm, do I get it right that by "no opinion" you mean, that pep8 shouldn't return |
Yes, I think if a function or class definition is nested inside another suite, pep8 would be better off ignoring the vertical whitespace guidelines. I don't know how practical that is to implement, though. |
I'll let @IanLee1521 comment on the feasibility |
pep8 should ignore whitespace errors E30 when evaluating non-top level code.
Ok, I just made the pull request for this fix. Only had to re-order some of the if statement cases to have the def / class checked first before the blank lines. Note this changes some E303 errors into E302 errors, e.g.: def a():
pass
def b():
pass
|
The fact that E303 and E302 is swapped in aome cases now seems unacceptable to me. If people are ignoring E303 in their configs, they're going to be extraordinarily angry that they now have to ignore E302 |
To me, it seems like this was actually a bug before, and that pep8 always should have been reporting |
It may have been a bug. That aside, people are still going to be rather annoyed. I don't have evidence if people are ignoring E303 and not E302 though, so take this with a grain of salt. |
@IanLee1521, I want to make sure I understand the changes correctly. Is this expected behavior for
class Foo(object):
def a(self):
pass
def b(self):
pass
if True:
pass
pass
pass
pass
Thanks |
Good catch, looks like there wasn't a test case for the case you listed in foo.py. I've created a new branch that has that in E30.py (d8441d1). I'll work on a patch for it that continues to report it as an error. |
I've rolled back this branch from e57e293 to 5100035 while I take some time to figure out best how to handle this. @myint -- This issue comes up primarily due to some of the user freedoms provided by the PEP:
I don't think that is really whats happening here, but it is one of the tricky points to handle in code. I'll re-open the issue and work on a better way to handle this. |
Is this a dup of #168 |
Note that on #400 clarification on the PEP was given that "top-level function and class definitions" refers to scope, not indent level. There was a little discussion about this on #536 , which might be of interest to someone intending to implement this. #536 was only a partial solution, introducing |
Hi folks, So what is the current status of this issue? At the moment, PyCharm formatter with default settings inserts double blank line also if classes are defined in an if, which is apparently the desired behavior after all the clarifications over here, but then this triggers a I've reported this problem against PyCharm here: https://youtrack.jetbrains.com/issue/PY-28615 and was indirectly referred to this issue. Are you planning to fix this anytime soon, or else I should push for PyCharm folks to adjust the formatter? Many thanks! |
As stated in PEP8:
The text was updated successfully, but these errors were encountered: