emacs-lsp / lsp-pyright

lsp-mode :heart: pyright
https://emacs-lsp.github.io/lsp-pyright
GNU General Public License v3.0
291 stars 25 forks source link

How setup it to use the `pyright` installed in the environment? #95

Open dhilst opened 4 months ago

dhilst commented 4 months ago

I have this example code:

from typing import Protocol, final

class Proto(Protocol):
    def foo(self, x: int) -> str: ...

@final
class Impl(Proto):
    def foo(self, x: int) -> int: ...

I have no errors in the editor, but if I install pyright manually and run the command I have the expected error

(mypyproject-py3.12) ➜  mypyproject poetry add pyright                                         
Using version ^1.1.369 for pyright

Updating dependencies
Resolving dependencies... (0.1s)

Package operations: 2 installs, 0 updates, 0 removals

  - Installing nodeenv (1.9.1)
  - Installing pyright (1.1.369)

Writing lock file
(mypyproject-py3.12) ➜  mypyproject pyright 
/nvme/geckos/home/code/mypyproject/mypyproject/foo.py
  /nvme/geckos/home/code/mypyproject/mypyproject/foo.py:9:9 - error: Method "foo" overrides class "Proto" in an incompatible manner
    Return type mismatch: base method returns type "str", override returns type "int"
      "int" is incompatible with "str" (reportIncompatibleMethodOverride)
1 error, 0 warnings, 0 information 

How can I tell to lsp-pyright to use the installed pyright?

I have this configuration in my .emacs

;; Required by lsp-pyright
(use-package lsp-pyright
  :ensure t
  :hook (python-mode . (lambda ()
                          (require 'lsp-pyright)
                          (lsp))))  ; or lsp-deferred

Also I'm using poetry as the package manager for my projects, and poetry.el package. I activate the environment by M-x poetry RET v and restart the lsp with lsp-workspace-restart

I guess lsp-pyright install its own version of pyright, but couldn't found by looking into the elisp code

I'm also using poetry config virtualenvs.in-project true, which make poetry create the virtual environment at .venv. When I ran M-: (lsp-pyright-locate-venv) I receive "~/code/mypyproject/.venv" in the minibuffer, but still no error reporting for the protocol implementation.

Note: This may be an Y/X problem. I want to have the proper error message (X) and I believe the problem that the plugin is using whatever other version of pyright that is not the same of the command line(Y), but maybe Y is not the case

zipy124 commented 3 months ago

I guess lsp-pyright install its own version of pyright, but couldn't found by looking into the elisp code

It does not. The user must supply it.

Your problem is that lsp-pyright looks for pyright in the general $PATH, not with your environment active. That is it looks for your system pythons pyright.

I'm unsure as to the best solution to your problem, but it is likely either adding your poetries environment to the $PATH, or installing pyright into the system, itself, not in a venv. (though it would be possible to modify lsp-pyright to use your poetry version of pyright, I don't think it is the intended usecase)

sergeyklay commented 2 hours ago

I thought the same — that lsp-pyright would install all its dependencies automatically. At least, that would’ve made sense, because that’s what other packages like anaconda-mode or dap-mode typically do — it’s common for packages to handle dependencies themselves. A lot of them work that way. I have to admit, I spent a lot of time trying to figure out why things weren’t working before stumbling across this issue at GitHub.

The most frustrating part, in my opinion, is that lsp-pyright just quietly “sat there” without saying it was missing something essential to function properly. No error messages, no warnings — just silent inaction. It wasn’t until I ran sudo npm install -g pyright that things finally worked. But I have to say, I didn’t find any mention anywhere that I MUST to do this as a required step. I feel like this should be stated clearly, right at the very beginning of the documentation, in bold letters.