0

I am using black formatter. I have two newlines before and two newlines after function definition. I want one newline before and one newline after function definition.

Can i do it using black config file black --config FILE. If so,how.

SuperStormer
  • 4,997
  • 5
  • 25
  • 35
Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87
  • From the description: "Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting.". – 9769953 Sep 27 '21 at 05:48

2 Answers2

3

OP here, I ended up using yapf.

The command looks like:

yapf --style={blank_lines_around_top_level_definition=1} file_name.py

for multiple arguments:

yapf --style={based_on_style=pep8} --style={blank_lines_around_top_level_definition=1} file_name.py

In vscode you can use the yapf styles like:

"python.formatting.yapfArgs": [
    "--style",
    "{based_on_style: pep8, blank_lines_around_top_level_definition: 1}"
],
Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87
-1

Using black formatter this is NOT possible. From it's documentation:

By using it, you agree to cede control over minutiae of hand-formatting._

Current style:

It uses 2 lines to visually distinguish methods from empty lines in code structure. From the docs: Black will allow single empty lines inside functions, [...] 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.

From: the docs of black.readthedocs.io

Customization:

Command line options Black has quite a few knobs these days, although Black is opinionated so style configuration options are deliberately limited and rarely added. You can list them by running black --help.

You need to change the formatter you use to something different to get your effect.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • I don't understand why this answer was down voted. It seems like a reasonable answer based on the documentation. What's more, the author even seems to have switched tooling instead of continue with the requested software. – slbass Jul 21 '23 at 18:40
  • @slbass That is SO in a nutshell - it is just silly points and I got enough so ::shrug:: – Patrick Artner Jul 23 '23 at 08:16