microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.79k stars 28.74k forks source link

Formatting notebook cells with Black leads to random blank lines #200503

Closed CanePlayz closed 4 months ago

CanePlayz commented 9 months ago

Type: Bug

I'm formatting the following notebook cell in VS Code:

import torchvision

from PIL import Image

from torch.utils.data import Dataset

class Simple_Dataloader(Dataset):
    def __init__(self, root_dir, loader_mode):
        self.root_dir = root_dir

        self.loader_mode = loader_mode
        self.transform = torchvision.transforms.Compose(
            [torchvision.transforms.ToTensor()]
        )

        self.paths = []
        for cat_folder in os.listdir(root_dir + loader_mode):
            for file in os.listdir(root_dir + loader_mode + os.sep + cat_folder):
                self.paths.append(
                    os.path.join(root_dir + loader_mode + os.sep + cat_folder, file)
                )

    def __len__(self):
        return len(self.paths)

    def __getitem__(self, idx):
        img_path: str = self.paths[idx]
        img: Image.Image = Image.open(img_path)

        img_torch = self.transform(img)

        label_idx = int(img_path.split(os.sep)[-2])

        return img_torch, label_idx

On some cells (including this one), the formatter does weird things.

In this case, I get:

import torchvision

from PIL import Image

from torch.utils.data import Dataset

class Simple_Dataloader(Dataset):

    def __init__(self, root_dir, loader_mode):

        self.root_dir = root_dir

        self.loader_mode = loader_mode

        self.transform = torchvision.transforms.Compose(

            [torchvision.transforms.ToTensor()]
        )

        self.paths = []

        for cat_folder in os.listdir(root_dir + loader_mode):

            for file in os.listdir(root_dir + loader_mode + os.sep + cat_folder):
                self.paths.append(

                    os.path.join(root_dir + loader_mode + os.sep + cat_folder, file)
                )

    def __len__(self):

        return len(self.paths)

    def __getitem__(self, idx):

        img_path: str = self.paths[idx]

        img: Image.Image = Image.open(img_path)

        img_torch = self.transform(img)

        label_idx = int(img_path.split(os.sep)[-2])

        return img_torch, label_idx

To Reproduce:

  1. Use VS Code with the Black extension
  2. Set it as the default editor for notebooks
  3. Format cell or notebook from the right-click menu
  4. Watch it either format correctly or weirdly

I've tried to format it by not using the right-click menu but black directly from the command line and then it worked properly.

VS Code version: Code - Insiders 1.86.0-insider (02b90714767f05c734ac29a64d0e5b4768f532d8, 2023-12-08T11:53:10.286Z) OS version: Windows_NT x64 10.0.26010 Modes:

System Info |Item|Value| |---|---| |CPUs|AMD Ryzen 7 5800X 8-Core Processor (16 x 3800)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|undefined| |Memory (System)|15.93GB (1.82GB free)| |Process Argv|--crash-reporter-id ccf2f68f-2487-40db-aa8c-09d8126d68c5| |Screen Reader|no| |VM|50%|
Extensions (50) Extension|Author (truncated)|Version ---|---|--- project-manager|ale|12.7.0 nugetpackagemanagergui|ali|2.0.6 insert-unicode|bru|0.15.1 vscode-markdownlint|Dav|0.53.0 githistory|don|0.6.20 gitlens|eam|2023.12.904 EditorConfig|Edi|0.16.4 git-project-manager|fel|1.8.2 copilot|Git|1.141.592 copilot-chat|Git|0.12.2023120801 remotehub|Git|0.62.0 vscode-github-actions|git|0.26.2 vscode-pull-request-github|Git|0.79.2023120711 gc-excelviewer|Gra|4.2.58 codesnap-plus|hui|1.4.1 Ionide-fsharp|Ion|7.16.1 vscode-docker|ms-|1.28.0 csdevkit|ms-|1.2.2 csharp|ms-|2.13.10 dotnet-maui|ms-|0.5.50 vscode-dotnet-runtime|ms-|2.0.0 vscodeintellicode-csharp|ms-|0.1.26 black-formatter|ms-|2023.7.13351006 isort|ms-|2023.11.13191009 python|ms-|2023.23.13391009 vscode-pylance|ms-|2023.11.104 jupyter|ms-|2023.11.1003402403 jupyter-keymap|ms-|1.1.2 jupyter-renderers|ms-|1.0.17 vscode-jupyter-cell-tags|ms-|0.1.8 vscode-jupyter-slideshow|ms-|0.1.5 remote-containers|ms-|0.328.0 remote-wsl|ms-|0.81.8 cpptools|ms-|1.19.1 powershell|ms-|2023.11.1 remote-repositories|ms-|0.40.0 vsliveshare|ms-|1.0.5900 java|red|1.26.2023120908 vscode-xml|red|0.26.2023120208 vscode-yaml|red|1.14.0 vscode-uppercase|rui|1.0.1 intellicode-api-usage-examples|Vis|0.2.8 vscodeintellicode|Vis|1.2.30 vscode-svg-previewer|vit|0.7.0 vscode-gradle|vsc|3.13.2023120800 vscode-java-debug|vsc|0.55.0 vscode-java-dependency|vsc|0.23.2023120100 vscode-java-test|vsc|0.40.2023101002 vscode-icons|vsc|12.6.0 markdown-all-in-one|yzh|3.5.1 (1 theme extensions excluded)
A/B Experiments ``` vsliv695:30137379 vsins829:30139715 vsliv368cf:30146710 vsreu685:30147344 python383cf:30185419 vspor879:30202332 vspor708:30202333 vspor363:30204092 vslsvsres303:30308271 pythontb:30258533 vsc_aacf:30263846 pythonptprofiler:30281269 vsdfh931:30280409 vshan820:30294714 vscod805:30301674 bridge0708:30335490 bridge0723:30353136 vsaa593:30376534 pythonvs932:30404738 py29gd2263:30784851 vscaac:30438845 vsclangdf:30492506 c4g48928:30535728 dsvsc012:30540252 vscrp:30624060 showlangstatbar:30737417 fixshowwlkth:30771523 showindicator:30805243 pythongtdpath:30726887 i26e3531:30792625 welcomedialog:30812478 pythonnosmt12:30779711 pythonidxpt:30768918 pythonnoceb:30776497 asynctok:30898717 dsvsc013:30777762 dsvsc014:30777825 pythonmhint1:30901775 dsvsc015:30821418 pythontestfixt:30866404 pythonregdiag2:30902439 pyreplss1:30879911 pythonmypyd1:30859725 pythoncet0:30859736 pythontbext0:30879054 accentitlementst:30870582 dsvsc016:30879898 dsvsc017:30880771 dsvsc018:30880772 aa_t_chat:30882232 ```
laser-vis commented 6 months ago

Experiencing same behavior. Could this be a clue as to the reason: when I initiated a git repo in the folder, received a warning, "in working copy of file.ipynb, LFs will be replaced by CRLFs the next time git touches the file"?

drmolix commented 5 months ago

I have the same problem. You can temporarily fix it by reloading the window.

laser-vis commented 5 months ago

@drmolix By "reloading the window" do you mean closing and reopening the notebook file, or... something else?

drmolix commented 5 months ago

@drmolix By "reloading the window" do you mean closing and reopening the notebook file, or... something else?

I use VSCode Command Palette (CTRL + Shift + P), then I select >Developer: Reload Window

Yoyokrazy commented 4 months ago

Circling back to this after some related work I've done with the notebook formatters. Recently fixed a bug (https://github.com/microsoft/vscode/issues/212124) that was resulting in more than the default formatter being run against the notebook in certain flows.

In tomorrow's (May 08) insiders, I would love to know if this is still reproducible through any of the following format hooks:

Thanks a lot everyone! Appreciate all the input here.

laser-vis commented 4 months ago

I could not reproduce the behavior using today's insiders build; however, the problem was always unpredictable and inconsistent, so this isn't a definitive test.

CanePlayz commented 4 months ago

I also cannot observe this bahavior anymore

Yoyokrazy commented 4 months ago

Thanks for the updates @laser-vis @CanePlayz. I'll close this then, please reopen if you encounter the issue again.

CanePlayz commented 4 months ago

Yeah thanks for fixing

yjcui commented 3 months ago

I kept running into this issue, not always, but like once a month or so. This is not predictable or reproducible. It just happens. I have no idea how that was triggered, which is the most annoying part.

CanePlayz commented 3 months ago

Alright, thanks for letting me know

laser-vis commented 3 months ago

Same here... happened like 2 weeks ago. Before that it did seem as though it had been fixed.

yjcui commented 3 months ago

I kept running into this issue, not always, but like once a month or so. This is not predictable or reproducible. It just happens. I have no idea how that was triggered, which is the most annoying part.

Also FYI, I have been using Jupyter Notebook in VS Code environment. This issue happens to certain cells only, especially when I have like >200 lines of code within one cell. Have not seen this issue with cells containing fewer lines of code, say <50.