microsoft / vscode-black-formatter

Formatting support for Python using the Black formatter
https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter
MIT License
158 stars 36 forks source link

black produces unnecessary line breaks in a cell of jupyter notebook if there are one or more import statements #516

Open Saltsmart opened 5 months ago

Saltsmart commented 5 months ago

Diagnostic Data

Behaviour

Expected Behavior

Just like these code in one cell:

from matplotlib.patches import Circle
data = {
    "MACs": [0],
    "MAPE": 1,
    "Params": 2,
    "Category": [
        "Seq",
        "Attn",
        "Dyn",
        "Dyn",
        "Conv",
        "Seq",
        "Attn",
        "Dyn",
        "Dyn",
        "Conv",
        "Seq",
        "Attn",
        "Dyn",
        "Conv",
    ],
}

When formatting, it should produce a line break after the import statement. Other line breaks are unnecessary and annoying.

Actual Behavior

It Sometimes produce formatted output like this:

from matplotlib.patches import Circle

data = {
    "MACs": [0],

    "MAPE": 1,
    "Params": 2,
    "Category": [
        "Seq",
        "Attn",
        "Dyn",
        "Dyn",

        "Conv",
        "Seq",
        "Attn",
        "Dyn",

        "Dyn",
        "Conv",
        "Seq",

        "Attn",
        "Dyn",
        "Conv",
    ],
}

Reproduction Steps:

It will Sometimes happen in large jupyter notebooks with many cells. I find this issue in several notebooks containing private content. Here are an example file that could be shared:

main.zip

If it's hard to reproduce, I'm willing to help.

Logs:

I have set log level to trace but this issue disappears now. Logs will be given when it happens again.

Outcome When Attempting Debugging Steps:

Did running it from the command line work?

The command is something like D:/anaconda3/Scripts/black.exe --stdin-filename e:\Project\battery\datapreprocessing_and_plots.py - It will get stuck since the file is datapreprocessing_and_plots.ipynb and there is no such file as datapreprocessing_and_plots.py.

Extra Details

karthiknadig commented 5 months ago

@Saltsmart it would really be helpful to get the logs for this. The way this works is we pass each cell to black via stdin, and we use .py extension to tell black to treat the contents sent via stdin as Python text. This was a workaround as VS Code previously did not have a way to trigger formatting on the ipynb itself. The notebook formatting command triggered formatting on individual cells in parallel.

The upcoming release of VS Code, introduced a new way to register formatters as ipynb formatters. We will adopt that here when it is in stable.

As for this issue, if you want to try and replicate it vial command line, then do this:

  1. Copy contents of the cell to a file with extension .py
  2. Then use this command: type <file-path> | black —stdin-filename <file-path> -

That simulates what happens inside the extension. We don’t create a file, but feed each cell content in.

Saltsmart commented 3 months ago

@Saltsmart it would really be helpful to get the logs for this. The way this works is we pass each cell to black via stdin, and we use .py extension to tell black to treat the contents sent via stdin as Python text. This was a workaround as VS Code previously did not have a way to trigger formatting on the ipynb itself. The notebook formatting command triggered formatting on individual cells in parallel.

The upcoming release of VS Code, introduced a new way to register formatters as ipynb formatters. We will adopt that here when it is in stable.

As for this issue, if you want to try and replicate it vial command line, then do this:

  1. Copy contents of the cell to a file with extension .py
  2. Then use this command: type <file-path> | black —stdin-filename <file-path> -

That simulates what happens inside the extension. We don’t create a file, but feed each cell content in.

This issue only happens in Jupyter so I can't reproduce it in py file, and it's not always happening. Even a large file with no-import-statement cell sometimes triggers this bug.

issue

This is what happens to my code (I switch 'format cell on execution' on and running a cell is just the same as formating it, for this issue). I will be willing to upload the log if this happens the next time.