python-lsp / pylsp-mypy

Mypy plugin for the Python LSP Server.
MIT License
118 stars 34 forks source link

Add mypy's error code to the error diagnostic message #76

Open lieryan opened 7 months ago

lieryan commented 7 months ago

Use case

Whenever I want to add an # type ignore[xxx] for an error, I often need to recall what error code that this particular error message has.

image

It would be nice if the diagnostic message produced by pylsp-mypy would also display the error code, similar to mypy's default error message on the CLI:

...
foo.py:8: error: Incompatible return value type (got "int", expected "str")  [return-value]

Additionally, it'd be even nicer if there's a code action that allows you to add an ignore line and/or automatically suggest some common quick fixes for some of the more common error codes.

Richardk2n commented 7 months ago

The error code is not part of the message. However, pylsp-mypy passes the error code with the message to pylsp which in turn gives it to your editor (as far as I know). The editor is then responsible for displaying it is so desired by the user.

The only quick fix I can think of would be the ignore. Is there another you would like to see implemented?

lieryan commented 7 months ago

Adding type: ignore is pretty much the only use case I needed it for now. I can run mypy <filename> on the terminal for the rarer cases or just copy the error code from the code action.

However, pylsp-mypy passes the error code with the message to pylsp which in turn gives it to your editor (as far as I know)

Thanks for the pointer, I'll double check if this is something that my LSP client can be configured to do.

CameronNemo commented 6 months ago

I think I've noticed the same problem with ruff, too. I get the description of the error, but not the actual code. I am using neovim's builtin LSP client.

Richardk2n commented 6 months ago

The code action is implemented now. From my testing client support was not as straight forward as I expected. Feel free to try the current master and tell me if it works.

@ccordoba12 Quick question: Does Spyder support LSP code actions, because I did not find them.

lieryan commented 6 months ago

The quickfix action works well so far on master, I hadn't noticed any issues. Tested on vim-lsp.

Thanks @Richardk2n!