Skip to content

Instantly share code, notes, and snippets.

@nullchilly
Last active June 18, 2024 10:47
Show Gist options
  • Save nullchilly/91fb9d5bf410350d23dd8f4369e5183a to your computer and use it in GitHub Desktop.
Save nullchilly/91fb9d5bf410350d23dd8f4369e5183a to your computer and use it in GitHub Desktop.
Add pylance to coc.nvim

Install the pylance extension in vscode or manually download it from the marketplace

The extension path should be similar to this: ~/.vscode/extensions/ms-python.vscode-pylance-2023.11.10

In init.vim, this will automatically detect the latest pylance version because after an upgrade the old plugin might linger for a while:

call coc#config('languageserver', { "pylance": { "module": expand("~/.vscode/extensions/ms-python.vscode-pylance-*/dist/server.bundle.js", 0, 1)[0] } })

In coc-settings.json, we add these env to trick pylance into thinking we are running vscode:

{
  "languageserver": {
    "pylance": {
      "enable": true,
      "filetypes": [
        "python"
      ],
      "env": {
        "ELECTRON_RUN_AS_NODE": "0",
        "VSCODE_NLS_CONFIG": "{\"locale\": \"en\"}"
      },
      "module": "${userHome}/.vscode/extensions/ms-python.vscode-pylance-2023.11.10/dist/server.bundle.js",
      "initializationOptions": {},
      "settings": {
        "python.analysis.typeCheckingMode": "basic",
        "python.analysis.diagnosticMode": "openFilesOnly",
        "python.analysis.stubPath": "./typings",
        "python.analysis.autoSearchPaths": true,
        "python.analysis.extraPaths": [],
        "python.analysis.diagnosticSeverityOverrides": {},
        "python.analysis.useLibraryCodeForTypes": true
      }
    }
  }
}
@levouh
Copy link

levouh commented Jul 12, 2023

Doesn't seem to work anymore, at least with the latest version of Pylance.

@nullchilly
Copy link
Author

nullchilly commented Jul 13, 2023

Still works on 2023.7.20, remember to add the mentioned code in init.vim

image

@levouh
Copy link

levouh commented Jul 17, 2023

Are you running directly on Windows? It appears not, but just curious.

If I run node ~/.vscode/extensions/ms-python.vscode-pylance-2023.7.20/dist/server.bundle.js --stdio I get a message noting that it can only be used from MS products. Even when doing export ELECTRON_RUN_AS_NODE=1 and export VSCODE_NLS_CONFIG=1 beforehand, the same thing occurs.

Does it work for you directly from an interactive shell @nullchilly? I wonder of coc is doing something special that allows it to work.

@nullchilly
Copy link
Author

nullchilly commented Jul 21, 2023

The code in server.bundle.js is essentially

if not ELECTRON_RUN_AS_NODE or not VSCODE_NLS_CONFIG or --stdio:
  print "no vscode no pylance"

This is why ELECTRON_RUN_AS_NODE=1 VSCODE_NLS_CONFIG=1 works for coc.nvim because it doesn't need --stdio

Unfortunately for neovim native lsp, you will have to use a different method which I can't share publicly because Microsoft will patch it instantly.

@smackesey
Copy link

Hi @nullchilly I've played around with this and would like to compare notes. I sent you a discord connection request.

@goingtosleep
Copy link

Hi @nullchilly, @smackesey, I tried the gist for coc.nvim, but it doesn't seem to recognize the settings. I saw @smackesey's issue on coc.nvim. It's closed, but I don't see a working solution. Have you guys been successful getting coc.nvim to recognize the settings?

I also tried pylance with native-lsp, all good except for the inlayHint feature. Would like to know if anyone has it?

@nullchilly
Copy link
Author

nullchilly commented Aug 16, 2023

Hmm, inlayhint works for me on native-lsp

image

My nvim-lspconfig:

{
  "neovim/nvim-lspconfig",
  opts = {
    inlay_hints = { enabled = true },
    servers = {
      pylance = {
        settings = {
          python = {
            analysis = {
              inlayHints = {
                variableTypes = true,
                functionReturnTypes = true,
                callArgumentNames = true,
              },
            },
          },
        },
      },
    },
  },
},

@goingtosleep
Copy link

goingtosleep commented Aug 16, 2023

Thanks for the hint @nullchilly. Turns out my config for root_dir has some issue with inlay hints. Now it shows the function return type and function params, but variable types are still missing. Does it show the variable types on your end? Looks like it's the behavior of pylance, I got the same result in vscode. Thanks again bro. You're crazy good at this 😁

image

@disrupted
Copy link

doesn't seem to work anymore with new versions (>2023.10.30)

@nullchilly
Copy link
Author

Updated to work with the newest version (2023.11.10)

@fusying-hwang
Copy link

@nullchilly hi, I'd like to know how to setup a native pylance, I sent you a discord connection request. my user name is KKChang. Thanks

@Fau818
Copy link

Fau818 commented Jan 9, 2024

It seems it doesn't support inlay hints.

@smackesey
Copy link

It seems it doesn't support inlay hints.

Yup, I investigated this my looking at the LSP logs. No matter what I do I can't get inlay hints to work on latest nvim nightly. I can see in the telemetry LSP notification emitted after pylance starts inlay hints are always disabled no matter my settings. My guess is it's running some kind of off-books capabilities check that nvim doesn't pass and turning it off.

I also can't configure typeCheckingMode via lang server settings, though I can with a pyright config file.

@Fau818
Copy link

Fau818 commented Jan 9, 2024

@smackesey I found the problem, and it is necessary to set the value of settings.python.pythonPath.

I used nvim-lspconfig to test it.
You can set any non-empty value to the settings.python.pythonPath field, even if it's an invalid path.

Enjoy the Inlay Hints!!!

@nullchilly
Copy link
Author

nullchilly commented Jan 9, 2024

@Fau818 Thank you so much! Here is a working config

For anyone who doesn't use virtualenv, the tldr config is just:

pylance = {
  settings = {
    python = {
      pythonPath = "/usr/bin/python3",
      analysis = {
        inlayHints = {
          variableTypes = true,
          functionReturnTypes = true,
          callArgumentNames = true,
          pytestParameters = true,
        }
      }
    }
  }
}

@Fau818
Copy link

Fau818 commented Jan 9, 2024

@nullchilly
I just looked at your configuration and found that pythonPath affects the library index.
So we can't set the value of pythonPath arbitrarily.

Thank you for providing the configuration file, which helped me find a bug!

@mochaaP
Copy link

mochaaP commented Jan 11, 2024

HTTP/1.1 410 Gone
Content-Type: text/plain
Date: Thu, 01 Jan 1970 00:00:00 GMT

@xzbdmw
Copy link

xzbdmw commented Feb 29, 2024

@nullchilly Hello I send a discord friend request to you I can't send you mesaages if I'm not your friend😥
@mochaaP what does these mean I can't open it😥

@pearagit
Copy link

has anyone tried this and basedpyright and could comment on how they compare? thank you

@igorlfs
Copy link

igorlfs commented May 10, 2024

Hey @pearagit, IMO, basedpyright's major drawback is that it's really, really slow. Everything from completion to hover just takes forever when compared to Pylance.

But hey fellas, has anyone noticed some issues with semantic highlighting when using Pylance with neovim's built-in LSP?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment