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

Require two blank lines *after* a def/class too #400

Closed
gvanrossum opened this issue Apr 7, 2015 · 20 comments
Closed

Require two blank lines *after* a def/class too #400

gvanrossum opened this issue Apr 7, 2015 · 20 comments
Milestone

Comments

@gvanrossum
Copy link

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:

import stuff


def main():
    blah, blah

if __name__ == '__main__':
    main()

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!

@gvanrossum
Copy link
Author

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.)

@IanLee1521
Copy link
Member

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?

@gvanrossum
Copy link
Author

Only to top-level class and def.
On Apr 7, 2015 10:49 AM, "Ian Lee" notifications@github.com wrote:

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?


Reply to this email directly or view it on GitHub
#400 (comment).

@jayvdb
Copy link
Member

jayvdb commented Jun 13, 2016

There is ambiguity in the PEP, and differing opinions naturally, regarding whether 'top-level' refers to indentation or scope. e.g. should two blank lines be enforced when the def is within an if?
See #366 and #168 for discussions about that.

@gvanrossum
Copy link
Author

gvanrossum commented Jun 13, 2016 via email

@jayvdb
Copy link
Member

jayvdb commented Jun 13, 2016

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 and a classdef/def, there should be two lines? e.g.

if True:


    class Foo: pass


...

@gvanrossum
Copy link
Author

gvanrossum commented Jun 13, 2016 via email

@sigmavirus24
Copy link
Member

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

@sigmavirus24
Copy link
Member

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?

@gvanrossum
Copy link
Author

gvanrossum commented Jun 13, 2016 via email

@sigmavirus24
Copy link
Member

@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:
     #...

@gvanrossum
Copy link
Author

gvanrossum commented Jun 13, 2016 via email

@IanLee1521 IanLee1521 added this to the 2.1 milestone Jun 25, 2016
markpeek added a commit to markpeek/pycodestyle that referenced this issue Nov 6, 2016
markpeek added a commit to markpeek/pycodestyle that referenced this issue Nov 6, 2016
sigmavirus24 added a commit that referenced this issue Nov 7, 2016
Fix E305 regression in 2.1.0 due to fix for #400
sigmavirus24 pushed a commit to PyCQA/flake8 that referenced this issue Nov 9, 2016
This attribute is introduced in pycodestyle 2.1.0

Closes #246

See: PyCQA/pycodestyle#400
sigmavirus24 added a commit to PyCQA/flake8 that referenced this issue Nov 9, 2016
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
enzbang added a commit to enzbang/e3-core that referenced this issue Nov 12, 2016
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.
@blueyed
Copy link

blueyed commented Nov 14, 2016

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.

@jayvdb
Copy link
Member

jayvdb commented Nov 14, 2016

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 do agree your previous layout looks nice when it is only a pass or a docstring, but I think it would be mentally jarring if setting the attribute appeared after a large block of function code.

@sigmavirus24
Copy link
Member

Should this be special cased to allow for setting attributes of the function (and class?) defined above?

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.

@gvanrossum
Copy link
Author

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 classifiers i.e. starting with class?

@sigmavirus24
Copy link
Member

That was fixed in 2.2.0

Sent from my Android device with K-9 Mail. Please excuse my brevity.

@gvanrossum
Copy link
Author

I guess somebody should tell the flake8 project. :-) Their setup.cfg requires pycodestyle < 2.2.0...

@IanLee1521
Copy link
Member

In their (his) defense, it was only released about 3 hours ago... ;)

@sigmavirus24
Copy link
Member

Yeah, someone should tell flake8 that pycodestyle 2.2.0 was released. xD

yan12125 pushed a commit to ytdl-org/youtube-dl that referenced this issue Nov 17, 2016
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
mitya57 added a commit to Python-Markdown/markdown that referenced this issue Nov 18, 2016
This fixes warnings with pycodestyle ≥ 2.1, see PyCQA/pycodestyle#400.
enzbang added a commit to enzbang/e3-core that referenced this issue Nov 26, 2016
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.
calbrecht pushed a commit to calbrecht/youtube-dl that referenced this issue Dec 26, 2016
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
pointlessone added a commit to pointlessone/pep8 that referenced this issue Dec 29, 2016
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)
dirn added a commit to PyGotham/pygotham that referenced this issue Mar 30, 2017
An issue was opened with pycodestyle (PyCQA/pycodestyle#400) to address
a discrepancy with PEP 8. This blank line addresses that.
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

Successfully merging a pull request may close this issue.

5 participants