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 uses a lot of blank lines for the "conditionally define a different version of a function" idiom #1691

Closed
chrish42 opened this issue Sep 9, 2020 · 2 comments
Labels
R: duplicate This issue or pull request already exists T: enhancement New feature or request

Comments

@chrish42
Copy link

chrish42 commented Sep 9, 2020

I'm writing a piece of code that defines a function differently depending on something in the environment. I feel this is typically formatted as:

if some_condition:
    def foo():
        do_something()
else:
    def foo():
        do_something_else()

without whitespace. However, Black (even on master) adds whitespace there before and after the function definition. This certainly makes sense when the function is defined at the top level, but less so inside an if statement, I think.

Here's a full module that would actually parse and run, after formatting by Black:

from contextlib import contextmanager

# This would typically be computed by a function inspecting the environment...
ENABLED = True

if ENABLED:

    @contextmanager
    def foo(msg, *args, **kwargs):
        print(msg.format(*args, **kwargs))
        yield
        print("Done!")


else:

    @contextmanager
    def foo(msg, *args, **kwargs):
        yield

Again, I would personally have expected this to be written without blank lines after the if and around the else. Thoughts?

@chrish42 chrish42 added the T: enhancement New feature or request label Sep 9, 2020
@asottile
Copy link
Contributor

duplicate of #450 (via #377)

@ichard26
Copy link
Collaborator

ichard26 commented Oct 1, 2020

Thank you @asottile; closing!

@ichard26 ichard26 closed this as completed Oct 1, 2020
@ichard26 ichard26 added the R: duplicate This issue or pull request already exists label Oct 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
R: duplicate This issue or pull request already exists T: enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants