question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Black formatting leaves an extra line at the end of the file

See original GitHub issue

Environment data

  • VS Code version: 1.23.1
  • Extension version (available under the Extensions sidebar): 2018.5.0
  • OS and version: macOs High Sierra
  • Python version (& distribution if applicable, e.g. Anaconda): CPython 3.6.5
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): venv
  • Relevant/affected Python packages and their versions: black-18.6b1

Actual behavior

Formatting using black leaves two empty lines at the end of the file, if the file has more than 1 empty line already.

Expected behavior

Formatting should leave just one line, and this is what happens when I run black manually from the terminal.

Steps to reproduce:

  1. Open Python file
  2. Insert 10 empty lines at the end
  3. Activate “Format document” with black set as the formatter

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

##########Linting Output - flake8##########
20,1,W,W391:blank line at end of file

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:18
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
ashleybartlettcommented, Aug 28, 2019

I have just run into this issue. From the cli, black will change the file correctly. When saving, It will format the rest of the document, and leaves the extra blank line/ I even see the same flake8 picking this up as a formatting error.

If I have no blank lines, it adds one, if I have multiple it removes all but 2. I tried this out with a new file and found the same result.

This caught me out, as our CI pipeline runs black --check and fails. Which it did.

3reactions
roberthubblehqcommented, Jan 10, 2020

{ "files.trimFinalNewlines": true } in settings.json seems to fix this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

The Black code style - Black 22.12.0 documentation
The coding style used by Black can be viewed as a strict subset of PEP 8. Black reformats entire files in place. It...
Read more >
black formatter: How can I place one newline before and after ...
I want one newline before and one newline after function definition. Can i do it using black config file black --config FILE ....
Read more >
Line breaks are removed in posts made in plain text format
Method 1 - Disable the feature that removes extra line breaks · Open Outlook. · On the File tab, select Options. · In...
Read more >
Chapter 3 - Code Formatting with Black - Invent with Python
This chapter introduces you to Black, a code formatting tool that can ... If you never use space characters, your line can end...
Read more >
“Where did that line come from (and how do I get rid of it ...
Go to the File tab (versions 2010 and up) or or the Office Button in Word 2007, then go to Proofing (along the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found