2

Jumping to the solution, I know, apologies.

Context: I work on a large Django code base where mypy is too slow to be usable, so I use pyright, which is fast enough but can't handle some of the Django magic (django-stubs is the normal fix for this but the relevant part is implemented as _mypy_ plugin so won't work for pyright)

e.g.:

Cannot access member "applications" for type "Account" Pyright (reportAttributeAccessIssue) [58, 24]

(because .applications is a custom queryset attribute and pyright can't pick up on those)

So I'd like to silence a subset of pyright warnings. I don't want to silence the general reportAttributeAccessIssue because that would catch too many valid errors. And I don't want to fill my codebase with type: ignores, because CI (and other devs) use mypy, pyright is a personal choice and i want a personal solution that doesn't affect others.

Instead I'm happy to maintain a list of regexes to catch eg the specific querysets and model names i know of.

So, is there a way to silence lsp / vim diagnostic errors using a regex?

Also interested in other bright ideas if you have them!

I'm using lsp-zero and mason-lspconfig, if that's relevant.

7
  • 1
    You would disable pyright checks the same way you'd always do that. See e.g. stackoverflow.com/questions/57335636/… Your question has nothing to do with Vim. I'm voting to close as off-topic.
    – Friedrich
    Commented May 13 at 9:42
  • I don't think that the LSP bridge as a way to filter out some warnings or errors. The best would be to filter them our from the source (i.e. from pyright) like @Friedrich suggested. Commented May 13 at 9:59
  • thanks Friedrich, Vivian, i don't want to fill my code with type: ignores tho. will clarify in the q.
    – hwjp
    Commented May 13 at 13:19
  • I'd argue it's the reasonable approach to comment where code does not live up to a linting rule. Anyhow, it seems you can also put exceptions in a pyrightconfig.json file.
    – Friedrich
    Commented May 13 at 13:25
  • 1
    I understand but as said I doubt that the functionality you would like to have in the LSP bridge already exist. If nobody come with a suggestion I suppose you have to ask for a new functionality in the LSP bridge that you are using (it is probably the Neovim LSP builtin bridge). Commented May 13 at 13:36

1 Answer 1

1

What I do is that I fill location list with the diagnostics and filter it using cfilter (default package) You need to run packadd cfilter first.

nnoremap <leader>XD <CMD>lua vim.diagnostic.setloclist()<CR>

"check file
nnoremap <leader>xf <CMD>lua vim.diagnostic.setloclist()<CR><CMD>Lfilter /syntax\\|is not defined/<CR>

If you want to be more methodical, you might filter the diagnostics in lua at a certain event (using autocmd). If you print the table of diagnostics, you will see how to do it. But I do agree with @Vivan that ideally it should be done in the lsp bridge.

UPDATE: Someone wrote such plugin

https://github.com/m-gail/diagnostic_manipulation.nvim/tree/main

The code below works(Lazy.nvim) :

        {
            "m-gail/diagnostic_manipulation.nvim",
            event = "VeryLazy",
            init = function ()
                require("diagnostic_manipulation").setup {
                    blacklist = {
                        function(diagnostic)
                            return string.find(diagnostic.message, "Undefined global `vim`")
                        end
                        --require("diagnostic_manipulation.builtin.tsserver").tsserver_codes({ 6133, 6196 })
                    },
                    whitelist = {
                        -- Your whitelist here
                    }
                }
            end
        },


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.