Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Saltsmart opened this issue Jun 2, 2024 · 2 comments
Assignees
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster triage-needed Issue is not triaged.

Comments

@Saltsmart
Copy link

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.

@Saltsmart Saltsmart added the bug Issue identified by VS Code Team member as probable bug label Jun 2, 2024
@github-actions github-actions bot added the triage-needed Issue is not triaged. label Jun 2, 2024
@karthiknadig
Copy link
Member

@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.

@karthiknadig karthiknadig added the info-needed Issue requires more information from poster label Jun 3, 2024
@karthiknadig karthiknadig self-assigned this Jun 3, 2024
@Saltsmart
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster triage-needed Issue is not triaged.
Projects
None yet
Development

No branches or pull requests

2 participants