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

Inconsistent spacing around top level functions #647

Closed
zmwangx opened this issue Dec 20, 2018 · 3 comments · Fixed by #2472
Closed

Inconsistent spacing around top level functions #647

zmwangx opened this issue Dec 20, 2018 · 3 comments · Fixed by #2472
Labels
F: empty lines Wasting vertical space efficiently. S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like? T: user support OP looking for assistance or answers to a question

Comments

@zmwangx
Copy link

zmwangx commented Dec 20, 2018

Operating system: macOS 10.14.2
Python version: 3.7.1
Black version: 18.9b0
Does also happen on master: yes


I have a bit of OS-specific code that looks like this (when formatted by black):

import os

if os.name == "posix":
    import termios

    def foo():
        pass


elif os.name == "nt":
    try:
        import msvcrt

        def foo():
            pass

    except ImportError:

        def foo():
            pass


else:

    def foo():
        pass


other_code()

Note now foo has two empty lines below it most of the time (including in if..elif..else), except in the try block of a try..except.

Is this intentional? Seems to me there's no need to waste an extra line when the function definition is in any block; one empty line should be enough.

@zsol zsol added F: empty lines Wasting vertical space efficiently. T: user support OP looking for assistance or answers to a question labels Feb 14, 2019
@ambv
Copy link
Collaborator

ambv commented Mar 4, 2020

This is indeed the case: https://black.now.sh/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4AFlAKtdAD2IimZxl1N_WlwxRhR7MSgE-ANVlfGy-Pf-urovcUul06DGIKtNJ_PI3I6sH4UsuvCCJK3cSn3O4qbqqEoVdC4RVidtOakKqE1C2PRTK-dEU4CIhv140rXetsz6FlHHNcogH0t3yNeHxqr6iAwuCmSu430fNaT3oxW598BtIbDYmvw9WdVHpVPmNBsa5a2N64u1JlxqaEp3vqccXdLeWyZdVl1aJp0nIQ-QAAAA7ohML_SstGsAAccB5gIAAOsZSAKxxGf7AgAAAAAEWVo=

This is caused by Black discovering that those functions are in fact top level and applying two blank lines before the next top-level statement (which is why it doesn't happen before the except ImportError line in the example).

We'd accept a fix for this but probably not something we'll be working on soon.

@ichard26 ichard26 added S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like? T: user support OP looking for assistance or answers to a question and removed T: user support OP looking for assistance or answers to a question labels Apr 28, 2021
@bukzor
Copy link

bukzor commented Oct 19, 2021

Is the only desired change to delete one blank line from each of those two double-blanks? Is that then the minimum amount of blank lines Black would allow?

@felix-hilden
Copy link
Collaborator

felix-hilden commented Oct 19, 2021

In my opinion a good style would be:

  • No blanks after indenting in or out
  • Exactly one blank line before and after defs and classes on the same level as other code

so the example provided would look like:

import os

if os.name == "posix":
    import termios

    def foo():
        pass
elif os.name == "nt":
    try:
        import msvcrt

        def foo():
            pass
    except ImportError:
        def foo():
            pass
else:
    def foo():
        pass

other_code()

I'm not 100 % sure about dropping the empty line after, I think leaving it up to the user would be okay. But this is fine in my opinion.


The linked PR addresses this to some extent, I think leaving the style at that would be okay too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: empty lines Wasting vertical space efficiently. S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: style What do we want Blackened code to look like? T: user support OP looking for assistance or answers to a question
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants