Neovim LSP: pyright server does not dynamically regognize changes in sub folders
Asked Answered
Q

1

6

I setup Neovim LSP using the nvim-lspconfig and the lsp-installer where I also installed the pyright server.

Without any further configuration it worked out of the box. However when I have a class in a subfolder and add a new method, pyright does not recognize this method when I want to access it in a different file. When I restart neovim, or open and close the file, pyright suddenly recognizes the newly added method.

I also tried :LspRestart with no effect.

I tried to add some settings to the pyright server:

return {
    settings = {
  python = {
    analysis = {
      autoSearchPaths = true,
      diagnosticMode = "workspace",
      useLibraryCodeForTypes = true,
    }
  }

    },
}

But this also had no effect.

:LspLog also does not show anything which could point to the issue:

[START][2022-07-15 11:11:05] LSP logging initiated
[WARN][2022-07-15 11:11:09] ...lsp/handlers.lua:109 "The language server pyright triggers a registerCapability handler despite dynamicRegistration set to false. Report upstream, this warning is harmless"
[WARN][2022-07-15 11:11:09] ...lsp/handlers.lua:456 "stubPath typings is not a valid directory."
[WARN][2022-07-15 11:11:20] ...lsp/handlers.lua:109 "The language server pyright triggers a registerCapability handler despite dynamicRegistration set to false. Report upstream, this warning is harmless"

I also could not find any setting regarding to this issue here which could solve this.

Since I am new to python, the way I import and structure classes might not be common and might be an issue which could cause this problem.

  • As a main entry point I have main.py in the root folder
  • All other source files are in a program/ folder which does not have a __init__.py
  • Inside program/ there are folders which each have a __init__.py file f.e. core/

core/__init__.py:

from .myClass import myClass

and in main.py I import it like this:

from subfolder.core import myClass
myClass.newMethod() # this is only recognized by lsp/pyright after the file is closed and reopen

Is the issue a bug in pyright (not likely I guess), a missing setting or my strange folder/import structure?

Quinquefid answered 15/7, 2022 at 10:9 Comment(1)
Did you find a solution to this? I am experiencing the same problem, and it's annoying to have to close neovim and reopen it after creating a new file. I previously used coc-pyright, and this wasn't an issue (I appreciate that they are not the same). Perhaps some sort of file watcher needs to be enabled...Nanaam
D
0

Can you try this: create (or modify) pyproject.toml, put it in the project root directory. Inside pyproject.toml, add the following lines:

[tool.pyright]
extraPaths = ["program/core" ,"program/directory_2", "program/directory_3"]

The idea is that you have to add the sub directories manually, which is really tedious but at least it works in my case.

Decade answered 30/7, 2022 at 15:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.