-
Notifications
You must be signed in to change notification settings - Fork 750
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
Require two blank lines *after* a def/class too #400
Comments
Oh, and clarification: it isn't the 'if' that makes the requirement for two lines. It is the end of the 'def'. The rule I meant to describe is that top-level class and def blocks must be surrounded by two blank lines. (But we need to have an allowance for comments too -- I'll let you figure that out by looking at actual code.) |
I believe, though I'm not able to verify this right now, that comments are ignored if before top level definitions. I will double check the exact rules though. As far as a rule goes, I'm concerned with files such as (specifically lines 98 - 120): https://hg.python.org/cpython/file/a48e76252952/Lib/json/__init__.py Where there are ***** assignments and imports that are not two newline separated. Should the two line rule apply only to between top level "class ...", "def ...", and "if name ..." definitions? |
Only to top-level class and def.
|
By top level I meant scope-wise, not I dentation-wise.
…--Guido (mobile)
|
Thank you for the clarification. When "top-level" is scope wise, there is one area where it becomes a little ugly IMO ... between the if True:
class Foo: pass
... |
Yes, go with what feels right to you there.
…--Guido (mobile)
|
By defining top-level as scope-wise, won't this also mean something like: def func0(foo, bar, bogus):
biz = ...
baz = ...
def inner_func(other_param):
# ...
return inner |
Also, I'm guessing we don't consider class definitions as scope in this definition of 'top-level' because then the PEP actively contradicts itself, no? |
What about top level is so hard to understand? OF COURSE the inner function
is not top level.
…--Guido (mobile)
|
@jayvdb's example is misleading then. Perhaps what you're talking about is if cond:
class Foo: pass
class Bar: pass
def foo(): pass
else:
#... |
I'm on vacation you have to figure it out by yourself sorry.
…--Guido (mobile)
|
Fix E305 regression in 2.1.0 due to fix for #400
This attribute is introduced in pycodestyle 2.1.0 Closes #246 See: PyCQA/pycodestyle#400
Add previous_unindented_logical_line attribute This attribute is introduced in pycodestyle 2.1.0 Closes #246 See: PyCQA/pycodestyle#400 See merge request !134
Fix for PyCQA/pycodestyle#400 created some false positives for E305 The pull-request in PyCQA/pycodestyle#593 fixing this issue has been done after 2.1.0.
I was using code like the following, which now causes "E305 expected 2 blank lines after class or function definition, found 0": def foo():
pass
foo.name = 'bar' Should this be special cased to allow for setting attributes of the function (and class?) defined above? The following looks not good: def foo():
pass
foo.name = 'bar' Please let me know if I should create a new issue for this. |
I think it should be tracked on a separate issue (unless you're intending to do a pull request soon), as I expect a bit of debate about large that special case should be, if at all as pep8 doesnt make an exception for that. |
I would argue that the PEP is rather unequivocal that there should be 2 empty lines around functions and class definitions. That said, it also advocates rather strongly for people to come to their own conclusions. Just because pycodestyle 2.1 adds this check, does not mean you have to use it. |
I do wonder if there's perhaps a buglet left in the implementation. I was prompted this morning to add a blank line somewhere in the middle of a block of global assignments below a class, here: python/mypy@8c6ba06#diff-2eeaed663bd0d25b7e608891384b7298R87. Note that there already is a double blank line below the class. I almost suspect that it has something to do with the name of the previous assignment being |
That was fixed in 2.2.0Sent from my Android device with K-9 Mail. Please excuse my brevity. |
I guess somebody should tell the flake8 project. :-) Their setup.cfg requires pycodestyle < 2.2.0... |
In their (his) defense, it was only released about 3 hours ago... ;) |
Yeah, someone should tell flake8 that pycodestyle 2.2.0 was released. xD |
In pycodestyle 2.1.0, E305 was introduced, which requires two blank lines after top level declarations, too. See PyCQA/pycodestyle#400 See also #10689; thanks @stepshal for first mentioning this issue and initial patches
This fixes warnings with pycodestyle ≥ 2.1, see PyCQA/pycodestyle#400.
Fix for PyCQA/pycodestyle#400 created some false positives for E305 The pull-request in PyCQA/pycodestyle#593 fixing this issue has been done after 2.1.0.
In pycodestyle 2.1.0, E305 was introduced, which requires two blank lines after top level declarations, too. See PyCQA/pycodestyle#400 See also #10689; thanks @stepshal for first mentioning this issue and initial patches
2.2.0 (2016-11-14) ------------------ Bugs: * Fixed E305 regression caused by PyCQA#400; PyCQA#593 2.1.0 (2016-11-04) ------------------ Changes: * Report E302 for blank lines before an "async def"; PyCQA#556 * Update our list of tested and supported Python versions which are 2.6, 2.7, 3.2, 3.3, 3.4 and 3.5 as well as the nightly Python build and PyPy. * Report E742 and E743 for functions and classes badly named 'l', 'O', or 'I'. * Report E741 on 'global' and 'nonlocal' statements, as well as prohibited single-letter variables. * Deprecated use of `[pep8]` section name in favor of `[pycodestyle]`; PyCQA#591 Bugs: * Fix opt_type AssertionError when using Flake8 2.6.2 and pycodestyle; PyCQA#561 * Require two blank lines after toplevel def, class; PyCQA#536 * Remove accidentally quadratic computation based on the number of colons. This will make pycodestyle faster in some cases; PyCQA#314 2.0.0 (2016-05-31) ------------------ Changes: * Added tox test support for Python 3.5 and pypy3 * Added check E275 for whitespace on `from ... import ...` lines; PyCQA#489 / PyCQA#491 * Added W503 to the list of codes ignored by default ignore list; PyCQA#498 * Removed use of project level `.pep8` configuration file; PyCQA#364 Bugs: * Fixed bug with treating `~` operator as binary; PyCQA#383 / PyCQA#384 * Identify binary operators as unary; PyCQA#484 / PyCQA#485 1.7.0 (2016-01-12) ------------------ Changes: * Reverted the fix in PyCQA#368, "options passed on command line are only ones accepted" feature. This has many unintended consequences in pep8 and flake8 and needs to be reworked when I have more time. * Added support for Python 3.5. (Issue PyCQA#420 & PyCQA#459) * Added support for multi-line config_file option parsing. (Issue PyCQA#429) * Improved parameter parsing. (Issues PyCQA#420 & PyCQA#456) Bugs: * Fixed BytesWarning on Python 3. (Issue PyCQA#459)
An issue was opened with pycodestyle (PyCQA/pycodestyle#400) to address a discrepancy with PEP 8. This blank line addresses that.
The pep8 tool insist on two blank lines before a top-level class or def, but not after one. So you get the following asymmetry:
The intention of PEP 8 is to require two blank lines before the 'if' too. I realize the PEP isn't fully clear, since it only says "separate [...] with two blank lines", and if you want an authoritative rule I can rewrite it. Just let me know!
The text was updated successfully, but these errors were encountered: