wyuenho / emacs-pet

Tracks down the correct Python tooling executables from your virtualenvs so you can glue the binaries to Emacs and delete code in init.el
GNU General Public License v3.0
121 stars 15 forks source link

pylsp-mypy is not recognizing the virtual environment #29

Closed publicimageltd closed 1 year ago

publicimageltd commented 1 year ago

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:

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 commented 1 year ago

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 commented 1 year ago

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:

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 commented 1 year ago

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.)