Skip to content

Auto dedent else in an if..else block not working if user is not in experiment #20479

Closed

Description

@rchiodo

Discussed in https://github.com/microsoft/pylance-release/discussions/3804

Originally posted by drorata December 22, 2022

Following the request from #481 by Brett Cannon (@brettcannon)

Environment data

  • Language Server version: v2022.12.20
  • OS and version: macos 13.1
  • Python version (& distribution if applicable, e.g. Anaconda): 3.11

Code Snippet

if __name__=="__main__":
    print("foo")  # 1
else:  # 2
    pass

After No.1, typing else starts right below the p. Once else: is typed, the editor doesn't de-intent the line and I have to do it manually.

Repro Steps

  1. Create a new file and start typing.

Expected behavior

The else: is supposed to be de-indented one level.

Actual behavior

else: stays below the previous line (which belongs to the if part)

de-indent-bug-vscode

Logs

Experiment 'pythonaacf' is active
Experiment 'pythonSurveyNotificationcf' is active
LSP Notebooks experiment is enabled
LSP Notebooks interactive window support is enabled
Python interpreter path: ~/.poetry_envs/foo-cv-BPVTkgqj-py3.11/bin/python
> poetry env list --full-path
cwd: .
> ~/.pyenv/versions/3.10.4/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> ~/.pyenv/versions/3.11.1/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> conda info --json
> ~/.pyenv/versions/3.7.13/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> ~/.pyenv/versions/3.9.13/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> . ~/.poetry_envs/foo-cv-BPVTkgqj-py3.11/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/printEnvVariables.py
> ~/.poetry_envs/username-cv-lqqRfM4F-py3.11/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> ~/.poetry_envs/fast-api-sql-example-hJw5i7sx-py3.11/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> ~/.poetry_envs/foo-cv-BPVTkgqj-py3.11/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> ~/.poetry_envs/kumsitz-lib-iVmOjJvt-py3.10/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> ~/.poetry_envs/pyspark-playground-Dg6oNfQx-py3.10/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> ~/.poetry_envs/stocks-playground-bIdRsd_o-py3.10/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> /usr/bin/python3 ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> /opt/homebrew/bin/python3 ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> /opt/homebrew/bin/python3.11 ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
> ~/.local/share/virtualenvs/test-project-GV0hGqPR/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
[ERROR 2022-11-23 8:14:35.526]: [Error: Command failed: /Users/username/.local/share/virtualenvs/test-project-GV0hGqPR/bin/python /Users/username/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py /Users/username/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
/bin/sh: /Users/username/.local/share/virtualenvs/test-project-GV0hGqPR/bin/python: No such file or directory

	at ChildProcess.exithandler (node:child_process:408:12)
	at ChildProcess.emit (node:events:526:28)
	at maybeClose (node:internal/child_process:1092:16)
	at Socket.<anonymous> (node:internal/child_process:451:11)
	at Socket.emit (node:events:526:28)
	at Pipe.<anonymous> (node:net:687:12)] {
  killed: false,
  code: 127,
  signal: null,
  cmd: '/Users/username/.local/share/virtualenvs/test-project-GV0hGqPR/bin/python /Users/username/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py /Users/username/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py'
}
Starting Pylance language server.
Extension search path: /Users/username/.vscode/extensions
> ~/.poetry_envs/foo-cv-BPVTkgqj-py3.11/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/linter.py -p flake8 /opt/homebrew/bin/flake8 --max-line-length=120 ./foo_cv/__init__.py
cwd: .
##########Linting Output - flake8##########


> ~/.local/share/virtualenvs/test-project-GV0hGqPR/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
[ERROR 2022-11-23 8:14:37.540]: [Error: Command failed: /Users/username/.local/share/virtualenvs/test-project-GV0hGqPR/bin/python /Users/username/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py /Users/username/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py
/bin/sh: /Users/username/.local/share/virtualenvs/test-project-GV0hGqPR/bin/python: No such file or directory

	at ChildProcess.exithandler (node:child_process:408:12)
	at ChildProcess.emit (node:events:526:28)
	at maybeClose (node:internal/child_process:1092:16)
	at ChildProcess._handle.onexit (node:internal/child_process:302:5)] {
  killed: false,
  code: 127,
  signal: null,
  cmd: '/Users/username/.local/share/virtualenvs/test-project-GV0hGqPR/bin/python /Users/username/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/get_output_via_markers.py /Users/username/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/interpreterInfo.py'
}
> /opt/homebrew/bin/black --diff --quiet ./foo_cv/__init__.py.fcaa6f612d7121d453663cbc59080266.tmp
cwd: .
Extension search path: /Users/username/.vscode/extensions
> ~/.poetry_envs/foo-cv-BPVTkgqj-py3.11/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/linter.py -p flake8 /opt/homebrew/bin/flake8 --max-line-length=120 ./foo_cv/__init__.py
cwd: .
##########Linting Output - flake8##########


> ~/.poetry_envs/foo-cv-BPVTkgqj-py3.11/bin/python ~/.vscode/extensions/ms-python.python-2022.20.1/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear tests
cwd: .

I don't really know what is test-project-GV0hGqPR nor where is it coming from. Maybe that's a hint for the problem.

Activity

  1. rchiodo commented on Jan 4, 2023

    @rchiodo
    Author

    This actually seems to be working for me. When typing the ':' it works.
    elsededent

  2. rchiodo commented on Jan 4, 2023

    @rchiodo
    Author

    Dror Atariah (@drorata) what are your user settings?

    They should have these:

      "[python]": {
        "editor.formatOnType": true
      },
      "python.languageServer": "Pylance",
  3. drorata commented on Jan 5, 2023

    @drorata

    I have just added in a workspace the settings suggested by Rich Chiodo (@rchiodo) and restarted VScode. Then I recorded the following:

    Screen.Recording.2023-01-05.at.08.42.55.mov

    So, it doesn't seem to do the trick...

  4. rchiodo commented on Jan 5, 2023

    @rchiodo
    Author

    I wonder if something else is taking over formatting. What other extensions do you have installed?

  5. drorata commented on Jan 5, 2023

    @drorata

    Is there a way to exctarct a full list of installed extensions?

  6. rchiodo commented on Jan 5, 2023

    @rchiodo
    Author

    From the command line:

    code --list-extensions
    

    It'd probably be better to just disable everything, then reenable python and pylance and see if it works. Because I'm not going to be able to tell that something else has a format on type provider just by looking at the list.

  7. drorata commented on Jan 6, 2023

    @drorata

    I tried to disable all extensions and then enable "Python" + "Pylence". Note that a few other extensions were enabled as well by doing so (e.g. Jupyter related). In any case, this didn't solve the problem.

    The installed extensions are:

    bierner.markdown-mermaid
    bpruitt-goddard.mermaid-markdown-syntax-highlighting
    bungcip.better-toml
    christian-kohler.path-intellisense
    eamodio.gitlens
    gera2ld.markmap-vscode
    hediet.vscode-drawio
    IronGeek.vscode-env
    James-Yu.latex-workshop
    kenhowardpdx.vscode-gist
    mechatroner.rainbow-csv
    ms-azuretools.vscode-docker
    ms-python.isort
    ms-python.python
    ms-python.vscode-pylance
    ms-toolsai.jupyter
    ms-toolsai.jupyter-keymap
    ms-toolsai.jupyter-renderers
    ms-toolsai.vscode-jupyter-cell-tags
    ms-toolsai.vscode-jupyter-slideshow
    ms-vscode-remote.remote-containers
    ms-vscode-remote.remote-ssh
    ms-vscode-remote.remote-ssh-edit
    ms-vscode-remote.remote-wsl
    ms-vscode-remote.vscode-remote-extensionpack
    ms-vscode.remote-explorer
    njpwerner.autodocstring
    PKief.material-icon-theme
    rebornix.ruby
    redhat.vscode-yaml
    stkb.rewrap
    streetsidesoftware.code-spell-checker
    vstirbu.vscode-mermaid-preview
    wholroyd.jinja
    wingrunr21.vscode-ruby
    yzhang.markdown-all-in-one
    
  8. karrtikr commented on Jan 6, 2023

    @karrtikr

    @rchido curious, is this behind an experiment or available to all users?

  9. rchiodo commented on Jan 6, 2023

    @rchiodo
    Author

    @rchido curious, is this behind an experiment or available to all users?

    We discussed this yesterday in standup and it might be affected by an experiment. It isn't listed here:

    image

    But Luciana Abud (@luabud) thought it might be enabled with just a flag somewhere else. I don't see it in any of our package.jsons though.

    Dror Atariah (@drorata) can you try setting this flag and see if it has any effect?

      "python.experiments.optInto": [
        "All"
      ]

    That should turn on whatever experiment may be making this not work.

    At least if I do the opposite, it stops working for me:

      "python.experiments.optOutFrom": [
        "All"
      ]
  10. rchiodo commented on Jan 6, 2023

    @rchiodo
    Author

    Thanks Luciana Abud (@luabud), it looks like this code here is the culprit:

    return inExperiment && formatOnTypeEffectiveValue;

    This only works if the user is in the experiment too. Switching this to the python repository, That code should be checking if it gets the default value or if the user supplied it and only using the experiment value if the user didn't supply it.

  11. rchiodo commented on Jan 6, 2023

    @rchiodo
    Author

    Dror Atariah (@drorata) That means my workaround should fix your issue:

      "python.experiments.optInto": [
        "All"
      ]
  12. changed the title [-]Auto dedent `else` in an `if`..`else` block[/-] [+]Auto dedent `else` in an `if`..`else` block not working if user is not in experiment[/+] on Jan 6, 2023
  13. drorata commented on Jan 8, 2023

    @drorata

    I added

    "python.experiments.optInto": [
        "All"
      ]

    to my users settings.json and now it works as expected.

    FWIW, I still can see the strange virtual environment as discussed in microsoft/vscode-jupyter#12501.

  14. added
    bugIssue identified by VS Code Team member as probable bug
    area-intellisenseLSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc.
    needs PRReady to be worked on
    and removed
    triage-neededNeeds assignment to the proper sub-team
    on Jan 9, 2023
  15. luabud commented on Jan 9, 2023

    @luabud
    Member

    This should be fixed on our pre-release version, so it should work without the "python.experiments.optInto" setting as well!

  16. locked as resolved and limited conversation to collaborators on Feb 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-intellisenseLSP-related functionality: auto-complete, docstrings, navigation, refactoring, etc.bugIssue identified by VS Code Team member as probable bugneeds PRReady to be worked on

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions