Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pylsp-mypy is not recognizing the virtual environment #29

Closed
publicimageltd opened this issue Nov 15, 2023 · 3 comments
Closed

pylsp-mypy is not recognizing the virtual environment #29

publicimageltd opened this issue Nov 15, 2023 · 3 comments

Comments

@publicimageltd
Copy link

This is a more precise statement of a problem which I still believe might be solvable with PET. I use pylsp with several plugins (flake8, mypy, ruff), but mypy is called with the global environment. Local checks on the shell return no errors, but opening the file in Emacs does.

I have the following configuration: pylsp, pylsp-mypy, pylsp-ruff; on Emacs eglot and PET. Eglot configuration is done on user-basis with the following setting:

    (setq-default eglot-workspace-configuration
                '(:pylsp (:plugins
                          (:mypy (:enabled t)
                                 :flake8 (:enabled t
                                                   :ignore ["E501"])
                           :pycodestyle (:enabled :json-false)
                           :ruff (:enabled t
                                           :extendSelect ["D" "E" "E201"]
                                           :extendIgnore ["E741" ])))))

The output of eglot-show.workspace-configuration yields:

{
  "pylsp": {
    "plugins": {
      "mypy": {
        "enabled": true
      },
      "flake8": {
        "enabled": true,
        "ignore": [
          "E501"
        ],
        "executable": "/usr/bin/flake8"
      },
      "pycodestyle": {
        "enabled": false
      },
      "ruff": {
        "enabled": true,
        "extendSelect": [
          "D",
          "E",
          "E201"
        ],
        "extendIgnore": [
          "E741"
        ]
      },
      "jedi": {
        "environment": "/home/jv/spiced/DeScribble/.venv/"
      },
      "pylint": {
        "executable": null
      }
    }
  }
}

So there are some observations:

  • PET works fine, adding the virtual environment directory e.g. to jedi
  • The initial configuration variable did not set jedi nor pylint. These entries are added by PET (in the function pet-lookup-eglot-server-initialization-options. I do not know if this a problem, but at least we have an intransparent modification of a configuration variable here.
  • mypy, however, is not called correctly; but it works with the activated environment on the command line.

In Emacs, mypy complains about missing imports which are de facto imported, and about which mypy does not complain when called from the command line.

I hope this is something PET related (maybe we could use an "environment" variable?); if not, I would be glad to profit from your expertise and get some pointers how to further investigate this issue. Configuring this lsp stuff is not a very amusing thing, it feels more like magic because of all the different layers involved.

@wyuenho
Copy link
Owner

wyuenho commented Nov 15, 2023

Does it work as intended if you manually set your eglot-workspace-configuration to this?

    (setq-default eglot-workspace-configuration
                '(:pylsp (:plugins
                          (:mypy (:enabled t :overrides ["True", "--python-executable", "/home/jv/spiced/DeScribble/.venv/bin/python"])
                           :flake8 (:enabled t :ignore ["E501"])
                           :pycodestyle (:enabled :json-false)
                           :ruff (:enabled t :extendSelect ["D" "E" "E201"] :extendIgnore ["E741" ])))))

@publicimageltd
Copy link
Author

This helped! It did not work directly, here's the result:

    (setq-default eglot-workspace-configuration
                  '(:pylsp (:plugins
                            (:pylsp_mypy (;;:enabled t
                                          :overrides ["--python-executable"
                                                    ".venv/bin/python"
                                                     t])
                                   :flake8 (:enabled t :ignore ["E501"])
                                   :pycodestyle (:enabled :json-false)
                                   :ruff (:enabled t :extendSelect ["D" "E" "E201"] :extendIgnore ["E741" ])))))

So there are several changes to your suggestion:

  • use t instead of True , and put it at the end (see this discussion
  • no commas in the vector
  • use pylsp_mypy (which means that my setup was effectively not modifying mypy's config at all)
  • in my setting, mypy is enabled automatically
  • the local path to the executable seems to suffice

I must admit I do not understand the last point. My source file is located in a subfolder, how does mypy know that the path refers to the venv's root? It seems like if it can recognize this, it should also know which python to use.

Maybe you could add that to your eglot customization in PET.

@publicimageltd
Copy link
Author

I just notice that in python files which do not have a venv, mypy now throws an error 'invalid file or executable'. So it would be really nice if PET could somehow handle these two cases automagically, as it does with all the other venv stuff (e.g. by using pet-executable-find.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants