microsoft / microsoft/vscode-black-formatter

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

Open
#516 5 comments 5 reactions 1 assignee Claimed by @karthiknadig View on GitHub
bug info-needed triage-needed
Dominant language
Python
Stars
196
Forks
49
Avg merge
1d 5h
Merged PRs (30d)
7

Description

## Diagnostic Data
- Python version (& distribution if applicable, e.g., Anaconda): 3.11.5 (Anaconda 2023.07)
- Type of virtual environment used (e.g., conda, venv, virtualenv, etc.): conda
- Operating system (and version): Windows 11 22000.2538
- Version of tool extension you are using: v2024.2.0 with black 24.4.2

## Behaviour
### Expected Behavior

Just like these code in one cell:
```python
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:

```python
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](https://github.com/user-attachments/files/15524459/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

- What other Python-related extensions are you using?
![image](https://github.com/microsoft/vscode-black-formatter/assets/40828952/2331bc0a-470f-45af-904a-360abf022ef0)
- Are you working in a multiroot workspace?
I have been in a workspace, but with only one root `e:\Project\battery`.
- Any extra settings from your workspace or user settings.json files?
Here are the settings from the workspace:
```json
"settings": {
"python.analysis.inlayHints.functionReturnTypes": false,
"python.analysis.inlayHints.variableTypes": false,
"python.analysis.inlayHints.callArgumentNames": "off",
"python.analysis.extraPaths": [
"D:/ibl/brainnet"
],
"python.autoComplete.extraPaths": [
"D:/ibl/brainnet"
],
"python.analysis.logLevel": "Trace"
```
These are from settings.json of the profile:
```json
{
"jupyter.askForKernelRestart": false,
"flake8.args": [
"--ignore=E203,W503,F841,E501,F401,E741",
"--max-line-length=88"
],
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"diffEditor.ignoreTrimWhitespace": false
},
"flake8.severity": {
"E": "Information",
"F": "Information",
"W": "Information"
},
"jupyter.debugJustMyCode": false,
"tensorBoard.logDirectory": "runs",
"python.terminal.executeInFileDir": true,
"terminal.integrated.env.windows": {
"PYDEVD_WARN_SLOW_RESOLVE_TIMEOUT": "5"
},
"python.analysis.autoFormatStrings": true,
"python.analysis.completeFunctionParens": true,
"python.analysis.inlayHints.variableTypes": true,
"python.analysis.inlayHints.functionReturnTypes": true,
"python.analysis.inlayHints.callArgumentNames": "partial",
"jupyter.kernels.excludePythonEnvironments": [
"D:/GPTAcademic/installer_files/conda/python.exe",
"D:/GPTAcademic/installer_files/env/python.exe"
],
"flake8.importStrategy": "fromEnvironment",
"editor.unicodeHighlight.includeStrings": false,
"black-formatter.path": [
"D:/anaconda3/Scripts/black.exe"
],
"diffEditor.maxComputationTime": 0,
"python.analysis.exclude": [
"**/.backup",
"**/.vscode"
],
"python.analysis.ignore": [
"**/.backup",
"**/.vscode"
],
"python.analysis.diagnosticSeverityOverrides": {
"reportMissingImports": "none",
"reportMissingModuleSource": "none"
},
"editor.unicodeHighlight.allowedCharacters": {
"−": true
},
"python.envFile": "${workspaceFolder}/.vscode/.env",
}
```
There is no specific setting related to black or python in user settings.json.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.