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

Conflict in blank lines after module docstring and before function #4027

Closed
peterjc opened this issue Nov 6, 2023 · 2 comments · Fixed by #4028
Closed

Conflict in blank lines after module docstring and before function #4027

peterjc opened this issue Nov 6, 2023 · 2 comments · Fixed by #4028
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. T: bug Something isn't working

Comments

@peterjc
Copy link

peterjc commented Nov 6, 2023

Using macOS:

$ black --version
black, 23.10.1 (compiled: yes)
Python (CPython) 3.10.12

Take this code:

"""This is a test case, note blank lines next."""
def go():
    print("Do stuff!")
go()

And run this:

$ black --diff blanks.py 
--- blanks.py	2023-11-06 18:04:21.775563+00:00
+++ blanks.py	2023-11-06 18:04:37.405865+00:00
@@ -1,4 +1,8 @@
 """This is a test case, note blank lines next."""
+
+
 def go():
     print("Do stuff!")
+
+
 go()
would reformat blanks.py

All done! ✨ 🍰 ✨
1 file would be reformatted.

Note current default behavior is to put two blank lines between the module docstring and following function. This is as expected, quoting https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html

It will also insert proper spacing before and after function definitions. It’s one line before and after inner functions and two lines before and after module-level functions and classes.

Now in preview mode:

$ black --diff blanks.py --preview
--- blanks.py	2023-11-06 18:04:21.775563+00:00
+++ blanks.py	2023-11-06 18:04:42.146632+00:00
@@ -1,4 +1,7 @@
 """This is a test case, note blank lines next."""
+
 def go():
     print("Do stuff!")
+
+
 go()

This now only has one line between the module docstring and following function. This seems like an unintended consequence of #1872.

@peterjc peterjc added the T: bug Something isn't working label Nov 6, 2023
@JelleZijlstra JelleZijlstra added the C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. label Nov 6, 2023
@JelleZijlstra
Copy link
Collaborator

Agree we should keep two lines here.

This is somewhat unlikely in practice as usually there will be imports between the docstring and the first function. We should keep two blank lines though if the first statement after the docstring is a class or function definition.

@yilei
Copy link
Contributor

yilei commented Nov 6, 2023

Unless someone is already working on it, I'll send a PR since we are also affected by this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: preview style Issues with the preview and unstable style. Add the name of the responsible feature in the title. T: bug Something isn't working
Projects
None yet
3 participants