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

Black doesn't enforce two empty lines before functions on module level when they have a sticky leading comment #246

Closed
hugovk opened this issue May 22, 2018 · 5 comments · Fixed by #3302
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. F: empty lines Wasting vertical space efficiently. T: enhancement New feature or request

Comments

@hugovk
Copy link
Contributor

hugovk commented May 22, 2018

Operating system: macOS High Sierra
Python version: 3.6.5
Black version: 18.5b0
pycodestyle version: 2.4.0
Does also happen on master: yes

Should Black insert a newline between these variables and the function?

pycodestyle complains about test.py but Black doesn't change it

$ cat test.py
from __future__ import print_function, unicode_literals

all_data = []
all_versions = set()

# Comment
def thing():
    pass
$ pycodestyle test.py
test.py:7:1: E302 expected 2 blank lines, found 1
$ black test.py
/private/tmp/test.py already well formatted, good job.

Both pycodestyle and Black are fine with test2.py

$ cat test2.py
from __future__ import print_function, unicode_literals

all_data = []
all_versions = set()


# Comment
def thing():
    pass
$ pycodestyle test2.py
$ black test2.py
/private/tmp/test2.py already well formatted, good job.

Black reformats test3.py, with no comment

$ cat test3.py
from __future__ import print_function, unicode_literals

all_data = []
all_versions = set()

def thing():
    pass
$ pycodestyle test3.py
test3.py:6:1: E302 expected 2 blank lines, found 1
$ black test3.py
reformatted /private/tmp/test3.py
$ pycodestyle test3.py
$ cat test3.py
from __future__ import print_function, unicode_literals

all_data = []
all_versions = set()


def thing():
    pass
@Sterbic
Copy link
Contributor

Sterbic commented May 23, 2018

Judging by the comments in comments5.py it's not trivial to support this case with the current implementation.

@ambv, would the EmptyLineTracker need to have access to both the previous as well as the next line to be able to handle this case?

@ambv
Copy link
Collaborator

ambv commented May 23, 2018

@Sterbic, yes. The way that would work is to modify format_str() to buffer one line before appending it to dst_contents, and only doing that after the next line (or the end of the file) comes in. This complicates things a fair bit.

@hugovk, as a workaround in the mean time, add the extra newline yourself, Black will keep it (as you found out in test2.py).

@ambv ambv added T: enhancement New feature or request F: empty lines Wasting vertical space efficiently. labels May 23, 2018
@ambv ambv changed the title Black doesn't add space before function with comment Black doesn't enforce two empty lines before functions on module level when they have a sticky leading comment May 23, 2018
@Sterbic
Copy link
Contributor

Sterbic commented May 25, 2018

Interesting, with give it a shot if I find some free time next week.

@tommyjcarpenter
Copy link

I was just going to post the same issue! Confirmed on my end too

_rmr_set_stimeout = rmr_c_lib.rmr_set_rtimeout
_rmr_set_stimeout.argtypes = [c_void_p, c_int]
_rmr_set_stimeout.restype = c_int
# extern int rmr_set_stimeout(void* vctx, int time)
def rmr_set_timeout(vctx, time):
    """
    Refer to the rmr C documentation for rmr_set_timeout
    """
    return _rmr_set_stimeout(vctx, time)

leads to E302; it doesn't change this code at all given the comment

@ichard26
Copy link
Collaborator

Can confirm that this is still an issue on main. Also even this code is untouched which isn't great:

all_data = []
all_versions = set()
# Comment
def thing():
    pass

jneo8 pushed a commit to canonical/charm-duplicity that referenced this issue Dec 21, 2023
black 23.1.0 introduces a new style that disagrees with flake8 in nested
functions:

  Enforce empty lines before classes and functions with sticky leading
  comments

In this case we can get rid of the simple comment to make both of them
happy.

See the release note[0] and related issue[1]

[0] https://github.com/psf/black/releases/tag/23.1.0
[1] psf/black#246

Signed-off-by: Mert Kırpıcı <[email protected]>
ashugauttam225 pushed a commit to ashugauttam225/charm-duplicity that referenced this issue Aug 11, 2024
black 23.1.0 introduces a new style that disagrees with flake8 in nested
functions:

  Enforce empty lines before classes and functions with sticky leading
  comments

In this case we can get rid of the simple comment to make both of them
happy.

See the release note[0] and related issue[1]

[0] https://github.com/psf/black/releases/tag/23.1.0
[1] psf/black#246

Signed-off-by: Mert Kırpıcı <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. F: empty lines Wasting vertical space efficiently. T: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants