Skip to content

Instantly share code, notes, and snippets.

@rewhile
Last active October 6, 2024 08:45
Show Gist options
  • Save rewhile/91fb9d5bf410350d23dd8f4369e5183a to your computer and use it in GitHub Desktop.
Save rewhile/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
      }
    }
  }
}
@PyDataBlog
Copy link

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

can you share your config?

@goingtosleep
Copy link

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

can you share your config?

Unfortunately, I’m currently using basedpyright and didn’t save the configuration for pylance, but if I remember correctly, having root_dir = function() return vim.fn.getcwd() end in the server config caused the issue I mentioned.

@RexsyBima
Copy link

RexsyBima commented Oct 6, 2024

Im so new into neovim but configuring pylance for neovim is very confsuing, anyone willing to help me????

EDIT :

Got it working now, but the thing is im only using old version of pylance, but is it possible to use the latest version? if so can anyone have a contact with me privately?

Sorry english is not my main...

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