zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
48.18k stars 2.86k forks source link

pyright does weird stuff #15644

Closed timptner closed 1 month ago

timptner commented 2 months ago

Check for existing issues

Related issues:

13541 #13206 #12883 #11508 #10587 #7296 #8541 #7646 #5524

Describe the bug / provide steps to reproduce it

Pyright complaints about unresolvable imports and undefined values.

Fresh install of Zed and starting a python project with virtualenv pyright highlights all non-standard library imports as unresolvable. This is fine because pyright doesn't know anything about my virtualenv. But it also marks any python standard functions, e. g. print() or int().

(1) Using ./pyproject.toml it recognizes all custom imports but no longer standard library. Still does not know anything about standard python functions.

[tool.pyright]
venvPath = "."
venv = ".venv"

(2) Using ./pyrightconfig.json same results as with ./pyproject.toml.

{
  "venvPath": ".",
  "venv": ".venv"
}

(3) Using global ~/.config/zed/settings.json it does find all third-party libraries installed inside .venv and also finds standard libraries. But it still does not know about standard functions (print, int, dict, sorted, ...).

{
  "lsp": {
    "pyright": {
      "settings": {
        "python": {
          "pythonPath": ".venv/bin/python"
        }
      }
    }
  }
}

I'm quite sure that some weeks ago I could solve all pyright errors by using pyproject.toml or pyrightconfig.json. But in the meantime nothing seems to work anymore. Using combinations of those 3 options above pyrightconfig.json will overwrite pyconfig.toml and settings.json in their behaviour.

Environment

Zed: v0.146.4 (Zed) OS: macOS 13.6.7 Memory: 8 GiB Architecture: x86_64

Python is installed via brew (python@3.12). PyRight is not installed via brew or as global pip package. (Brew stops one with a warning when trying python3 -m pip install <package> outside of an virtualenv.)

If applicable, add mockups / screenshots to help explain present your vision of the feature

No response

If applicable, attach your Zed.log file to this issue.

Zed.log

JosephTLyons commented 2 months ago

Hey @timptner, do you have any projects you could share that show the issue? I'm not able to reproduce this on my end - here's a project where print is being recognized by pyright:

SCR-20240802-kfob

We've had some issues where restarting Zed, after creating a pyrightconfig.json file, would correct some weird pyright issues - do your issues still persist after restarting Zed?

timptner commented 2 months ago

Freshly project:

Bildschirmfoto 2024-08-02 um 22 29 08

Restartet Zed multiple times in between those 3 options above. Even restarted Zed before creating this project and also tried zed . while CWD is this project, which some other issue mentioned as solution.

timptner commented 2 months ago
Bildschirmfoto 2024-08-02 um 22 31 29
timptner commented 2 months ago

I noticed that each time I restart Zed it always checks for pyright updates and everytime downloads pyright. Don't know if this is expected behaviour.

timptner commented 2 months ago

Just checked with my second workstation (debian) and there I don't get the pyright errors

imbev commented 2 months ago

I've encountered the same problem:

  1. Open empty directory
  2. uv init
  3. uv venv
  4. Set pyproject.toml dependencies to dependencies = ["requests"]
  5. uv pip compile pyproject.toml -o requirements.txt
  6. uv pip sync requirements.txt
  7. Add import requests to src/project/__init__.py

There will be a warning with Pyright: Import "requests" could not be resolved from source.

As @timptner said, this can be resolved by adding the following to pyproject.toml:

[tool.pyright]
venvPath = "."
venv = ".venv"

However this is a tweak that isn't needed when working in other editors.

ikheifets commented 2 months ago

@JosephTLyons the same behaviour, venv's libraries pyright recognizing correctly, but pythons builtins not:

Pyright: "str" is not defined (reportUndefinedVariable)
Pyright: "print" is not defined (reportUndefinedVariable)
Pyright: "range" is not defined (reportUndefinedVariable)

Previously it worked fluently, it seems that something became wrong after auto-update

I using the same setting for venv and I tried recreate venv and restart zed. Disable of venv settings also not working.

timptner commented 1 month ago

Just installed pyright with pip and checked the files.

(.venv) aiven@MacBook-Pro test % pyright --version
pyright 1.1.375
(.venv) aiven@MacBook-Pro test % pyright main.py
0 errors, 0 warnings, 0 informations

Seems like it's a local problem of the pyright version inside Zed.

ikheifets commented 1 month ago

@timptner for me working this workaround:

rm -rf ~/Library/Application\ Support/Zed/languages/pyright/

After that need to restart Zed and it will automatically download latest pyright version

timptner commented 1 month ago

@timptner for me working this workaround:

rm -rf ~/Library/Application\ Support/Zed/languages/pyright/

After that need to restart Zed and it will automatically download latest pyright version

This has solved it!

imbev commented 1 month ago

This is still a problem on the Linux version of Zed.

After running this command, the newly downloaded pyright has the same issues.

rm -rdf ~/.local/share/zed/languages/pyright/

ikheifets commented 1 month ago

@imbev you have another problem. In our case pyright can't recognize python stdlib (it seems that we've got broken version of pyright), in your case third-patry libs from venv, big chance that you wrongly configured virtualenv, just read docs.