Skip to content

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

@Saltsmart

Description

@Saltsmart

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:

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

  • What other Python-related extensions are you using?
    image
  • 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:
    "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:

{
    "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.

Metadata

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable buginfo-neededIssue requires more information from postertriage-neededIssue is not triaged.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions