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

Pyright configuration for disabling hint diagnostics #726

Closed
horta opened this issue Feb 10, 2021 · 17 comments
Closed

Pyright configuration for disabling hint diagnostics #726

horta opened this issue Feb 10, 2021 · 17 comments
Labels
bug Something isn't working

Comments

@horta
Copy link

horta commented Feb 10, 2021

According to: microsoft/pyright#982 (comment)

Pyright never emits an error or warning for unused "self". It does emit a "hint" diagnostic that clients can either ignore or use to display the unreferenced variable in gray if they choose. VS Code handles this fine, but some clients (editors) may not properly handle this hint and display it as a warning instead. Which client are you using?

AFAIK, there is not way to currently disable it using nvim-lspconfig:

require'lspconfig'.pyright.setup{}

  Commands:
  
  Default Values:
    cmd = { "pyright-langserver", "--stdio" }
    filetypes = { "python" }
    root_dir = <function 1>
    settings = {
      python = {
        analysis = {
          autoSearchPaths = true,
          useLibraryCodeForTypes = true
        }
      }
    }

Here is the diagnostic I would like to suppress:

Screenshot 2021-02-10 at 12 08 42

@horta horta added the bug Something isn't working label Feb 10, 2021
@mjlbach
Copy link
Contributor

mjlbach commented Feb 10, 2021

You can pass any settings to pyright that you would with any other client, just override the settings key that you pass into pyright. The issue is pyright's language server (I believe) doesn't allow you to selectively disable returning hints per diagnostic type like you want.

If you want to disable showing hints (generally), you'll need to override the diagnostic handler in neovim to pass a severity limit for virtual text. If you want to only disable one type of hint, I guess you could write a filtering function to filter out diagnostics of the type you are seeing (both of which, are not implemented/associated with nvim-lspconfig, which only handles starting your language server and passing initialization options).

@mjlbach mjlbach closed this as completed Feb 10, 2021
@soupglasses
Copy link

@horta Did you ever find out a way to do this? I am so close to giving up on the LSP-ecosystem in neovim because it keeps telling me im using self "wrong".

@mjlbach
Copy link
Contributor

mjlbach commented May 10, 2021

In that example why would you not make it a static method? Something like https://stackoverflow.com/questions/1697501/staticmethod-with-property. The diagnostic is correct, self is unused. FWIW you can pass an arbitrary filter on diagnostics by overriding the diagnostic handlers if it really bothers you.

@AxelSilverdew
Copy link

@mjlbach out of curiosity, how would one go about doing that? I didn't know we could filter diagnostics like that :o

@mjlbach
Copy link
Contributor

mjlbach commented May 10, 2021

You have access to the diagnostic list in on_publish_diagnostics, see here for an example of how it's sorted (you could apply a filter as well) https://github.com/neovim/neovim/blob/d2be261e8d1bcf165346255ae701564a5f62bf7d/runtime/lua/vim/lsp/diagnostic.lua#L1008-L1013

You'd want to make a custom version of that function, and then override the handler to call that. https://github.com/neovim/neovim/blob/d2be261e8d1bcf165346255ae701564a5f62bf7d/runtime/lua/vim/lsp/diagnostic.lua#L967-L983

@mjlbach
Copy link
Contributor

mjlbach commented May 10, 2021

Anyways, this isn't neovim's fault, pyright may or may not have some configuration option you can pass to it, we literally just pipe the diagnostics directly from pyright.

@disrupted
Copy link
Contributor

@imsofi update pyright to the latest version. This behaviour has been fixed for a while now.

@disrupted
Copy link
Contributor

Here's the related (closed) issue for more information: microsoft/pylance-release#194

@horta
Copy link
Author

horta commented May 10, 2021

@horta Did you ever find out a way to do this? I am so close to giving up on the LSP-ecosystem in neovim because it keeps telling me im using self "wrong".

I don't remember but

@property
def sequence(_):
    ...

might do the trick...

@MarcelRobitaille
Copy link

For anyone coming back to this, I am using the following based on @mjlbach's suggestions:

function filter(arr, func)
	-- Filter in place
	-- https://stackoverflow.com/questions/49709998/how-to-filter-a-lua-array-inplace
	local new_index = 1
	local size_orig = #arr
	for old_index, v in ipairs(arr) do
		if func(v, old_index) then
			arr[new_index] = v
			new_index = new_index + 1
		end
	end
	for i = new_index, size_orig do arr[i] = nil end
end


function filter_diagnostics(diagnostic)
	-- Only filter out Pyright stuff for now
	if diagnostic.source ~= "Pyright" then
		return true
	end

	-- Allow kwargs to be unused, sometimes you want many functions to take the
	-- same arguments but you don't use all the arguments in all the functions,
	-- so kwargs is used to suck up all the extras
	if diagnostic.message == '"kwargs" is not accessed' then
		return false
	end

	-- Allow variables starting with an underscore
	if string.match(diagnostic.message, '"_.+" is not accessed') then
		return false
	end

	return true
end

function custom_on_publish_diagnostics(a, params, client_id, c, config)
	filter(params.diagnostics, filter_diagnostics)
	vim.lsp.diagnostic.on_publish_diagnostics(a, params, client_id, c, config)
end

vim.lsp.handlers["textDocument/publishDiagnostics"] = vim.lsp.with(
	custom_on_publish_diagnostics, {})

@Swoogan
Copy link

Swoogan commented Feb 21, 2023

There is a much easier way to disable these hints. Pyright sends these hints to neovim because neovim is specifically asking for them (and probably shouldn't be -- see this discussion and this comment).
You can configure neovim to not ask for them:

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.publishDiagnostics.tagSupport.valueSet = { 2 } 
require('lspconfig')['pyright'].setup{
    capabilities = capabilities,
    on_attach = on_attach
}

For reference, here are the tags.

@MarcelRobitaille
Copy link

@Swoogan I prefer my method. It's more customizable. I can choose exactly what diagnostics I want to ignore. For example, *args, **kwargs, anything starting with _, etc.

@Swoogan
Copy link

Swoogan commented Feb 22, 2023

@MarcelRobitaille It certainly is more customizable. In my case I wanted all hints to be removed because I have them coming from other tools and was getting duplicate diagnostics. I wonder though, could it be implemented in the client's handler instead of globally? Then you wouldn't need to check the source. For example:

require('lspconfig')['pyright'].setup {
  on_attach = function(client, buffer)
    client.handlers["textDocument/publishDiagnostics"] = function(...) end
    on_attach(client, buffer)
  end,

@MarcelRobitaille
Copy link

@Swoogan I will try that, thanks!

@disrupted
Copy link
Contributor

@Swoogan yes, that is possible.

@zhou13
Copy link

zhou13 commented Mar 2, 2023

For anyone that is using LunarVim or nlsp-settings.nvim so that is is inconvenient to call require('lspconfig')['pyright'].setup, a solution is to use

local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.publishDiagnostics.tagSupport.valueSet = { 2 }
local util = require("lspconfig.util")
util.default_config = vim.tbl_extend("force", util.default_config, {
  capabilities = capabilities,
})

That said, I believe this is a bug since it is stated very clear here that Clients are allowed to render diagnostics with this tag faded out when the tag is 1.

rouge8 added a commit to rouge8/dotfiles that referenced this issue Mar 14, 2023
These largely duplicate messages from Ruff.

neovim/nvim-lspconfig#726 (comment)
@tan-wei-xin-alez
Copy link

those using lazyvim can add the following to a file in .config/nvim/lua/plugins as mentioned here

return {
  -- change nvim-lspconfig options
  "neovim/nvim-lspconfig",
  opts = {
    servers = {
      -- https://github.com/microsoft/pyright/discussions/5852#discussioncomment-6874502
      pyright = {
        capabilities = {
          textDocument = {
            publishDiagnostics = {
              tagSupport = {
                valueSet = { 2 },
              },
            },
          },
        },
      },
      ruff_lsp = {},
    },
  },
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants