如何禁用 pylsp 中的 linter?

发布于 2025-01-16 16:56:48 字数 1757 浏览 1 评论 0原文

我的 neovim(0.6.1) 使用 nvim-lint 管理 pylint,使用 pylsp 完成。

编辑python文件时,使用numpy、scipy等,代码竞争、悬停、签名很慢,CPU使用率100%。该代码有两次相同的 lint 概念。我想在 pylsp 中禁用 linter,但它不起作用。我该怎么办?

这是我的配置:pylsp.lua

opts = {
    cmd = { "pylsp" },
    filetypes = { "python" },
    root_dir = function()
        return vim.fn.getcwd()
    end,
    single_file_support = true,
    configurationSources = {""},  -- default is pycodestyle
    rope = {extensionModules = "", ropeFolder = {} },
    plugins = {
        jedi_completion = {
            enabled = true,
            eager = true,
            cache_for = {"numpy", "scipy"}
        },
        jedi_definition = {
            enabled = true,
            follow_imports = true,
            follow_builtin_imports = true,
        },
        jedi_hover = { enabled = true },
        jedi_references = { enabled = true },
        jedi_signature_help = { enabled = true },
        jedi_symbols = { enabled = true, all_scopes = true, include_import_symbols = true },
        preload = { enabled = true, modules = {"numpy", "scipy"} },
        mccabe = { enabled = false },
        mypy = { enabled = false },
        isort = { enabled = false },
        spyder = { enabled = false },
        memestra = { enabled = false },
        pycodestyle = { enabled = false },  -- not work
        flake8 = { enabled = false },
        pyflakes = { enabled = false },
        yapf = { enabled = false },
        pylint = {
            enabled = false,
            args = {
                "-f",
                "json",
                "--rcfile=" .. "~/.pylintrc"
            }
        },
        rope = { enabled = false },
        rope_completion = { enabled = false, eager = false },
    },
}
pylsp.setup(opts)

My neovim(0.6.1) use nvim-lint manage pylint, use pylsp for completion.

When edit a python file, use numpy, scipy etc, the code competion, hover, signature is slow, and cpu use 100%. The code have same lint notion twice. I want disable linter in pylsp, but it not work. How can i do?

This is my config: pylsp.lua

opts = {
    cmd = { "pylsp" },
    filetypes = { "python" },
    root_dir = function()
        return vim.fn.getcwd()
    end,
    single_file_support = true,
    configurationSources = {""},  -- default is pycodestyle
    rope = {extensionModules = "", ropeFolder = {} },
    plugins = {
        jedi_completion = {
            enabled = true,
            eager = true,
            cache_for = {"numpy", "scipy"}
        },
        jedi_definition = {
            enabled = true,
            follow_imports = true,
            follow_builtin_imports = true,
        },
        jedi_hover = { enabled = true },
        jedi_references = { enabled = true },
        jedi_signature_help = { enabled = true },
        jedi_symbols = { enabled = true, all_scopes = true, include_import_symbols = true },
        preload = { enabled = true, modules = {"numpy", "scipy"} },
        mccabe = { enabled = false },
        mypy = { enabled = false },
        isort = { enabled = false },
        spyder = { enabled = false },
        memestra = { enabled = false },
        pycodestyle = { enabled = false },  -- not work
        flake8 = { enabled = false },
        pyflakes = { enabled = false },
        yapf = { enabled = false },
        pylint = {
            enabled = false,
            args = {
                "-f",
                "json",
                "--rcfile=" .. "~/.pylintrc"
            }
        },
        rope = { enabled = false },
        rope_completion = { enabled = false, eager = false },
    },
}
pylsp.setup(opts)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狂之美人 2025-01-23 16:56:48

我设法在 lspconfig 设置中禁用 pylst linters:

例如,如果您使用 nvim-lspconfig 的建议配置,您可以更改lspconfig.pylsp.setup 并配置您喜欢的任何插件:

-- Mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
local opts = { noremap=true, silent=true }
vim.api.nvim_set_keymap('n', '<space>e', '<cmd>lua vim.diagnostic.open_float()<CR>', opts)
vim.api.nvim_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
vim.api.nvim_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts)
vim.api.nvim_set_keymap('n', '<space>q', '<cmd>lua vim.diagnostic.setloclist()<CR>', opts)

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
  -- Enable completion triggered by <c-x><c-o>
  vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')

  -- Mappings.
  -- See `:help vim.lsp.*` for documentation on any of the below functions
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
end

require('lspconfig').pylsp.setup {
  on_attach = on_attach,
  flags = {
    -- This will be the default in neovim 0.7+
    debounce_text_changes = 150,
  }
  settings = {
    -- configure plugins in pylsp
    pylsp = {
      plugins = {
        pyflakes = {enabled = false},
        pylint = {enabled = false},
      },
    },
  },
}

希望它有效!

I managed to disable pylst linters in lspconfig setup:

For example, if you use the suggested configuration of nvim-lspconfig, you can change the lspconfig.pylsp.setup and configure any plugin you like:

-- Mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
local opts = { noremap=true, silent=true }
vim.api.nvim_set_keymap('n', '<space>e', '<cmd>lua vim.diagnostic.open_float()<CR>', opts)
vim.api.nvim_set_keymap('n', '[d', '<cmd>lua vim.diagnostic.goto_prev()<CR>', opts)
vim.api.nvim_set_keymap('n', ']d', '<cmd>lua vim.diagnostic.goto_next()<CR>', opts)
vim.api.nvim_set_keymap('n', '<space>q', '<cmd>lua vim.diagnostic.setloclist()<CR>', opts)

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
  -- Enable completion triggered by <c-x><c-o>
  vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')

  -- Mappings.
  -- See `:help vim.lsp.*` for documentation on any of the below functions
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gD', '<cmd>lua vim.lsp.buf.declaration()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gd', '<cmd>lua vim.lsp.buf.definition()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'K', '<cmd>lua vim.lsp.buf.hover()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gi', '<cmd>lua vim.lsp.buf.implementation()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<C-k>', '<cmd>lua vim.lsp.buf.signature_help()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>rn', '<cmd>lua vim.lsp.buf.rename()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>ca', '<cmd>lua vim.lsp.buf.code_action()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', 'gr', '<cmd>lua vim.lsp.buf.references()<CR>', opts)
  vim.api.nvim_buf_set_keymap(bufnr, 'n', '<space>f', '<cmd>lua vim.lsp.buf.formatting()<CR>', opts)
end

require('lspconfig').pylsp.setup {
  on_attach = on_attach,
  flags = {
    -- This will be the default in neovim 0.7+
    debounce_text_changes = 150,
  }
  settings = {
    -- configure plugins in pylsp
    pylsp = {
      plugins = {
        pyflakes = {enabled = false},
        pylint = {enabled = false},
      },
    },
  },
}

Hope it works!

影子是时光的心 2025-01-23 16:56:48

如果有人仍然遇到这个问题,可以通过 pycodestyle 进行 linting。它出现在 pylsp github 页面 (https://github.com/python- lsp/python-lsp-server#installation)默认情况下禁用 pylint,但 pydocstyle 不是,如果您像我一样对编码相当“松散”,它会提供大量错误和警告。要在我的配置中禁用此功能,我执行了以下操作:

local lspconfig = require("lspconfig")

-- configure python server
lspconfig["pylsp"].setup({
  capabilities = capabilities,
  settings = {
    pylsp = {
      plugins = {
        pylint = { enabled = "false" },
        pyflakes = { enabled = "false" },
        pycodestyle = { enabled = "false" },
      }
    }
  },
  on_attach = on_attach,
})

因此,如果您使用 Mason 与 pylint 进行 linting,并且 pylsp 也正常工作,那么您可能会收到很多错误,其中一些错误可能会多次出现。在 lsp 和 linting 上观看对我有帮助的一个好视频是 https://www.youtube.com/watch?v=ybUE4D80XSk&t=1382s&ab_channel=JoseanMartinez 以及 https://youtu.be/NL8D8EkphUw。我的代码非常适合他在后面的视频中展示的示例,他使用pyright,我使用pylsp instad。他在后面的视频中执行此操作的具体部分是 26:38。
您还可以设置选项来保持 pylsp 和/或 pylint 的 linting,但禁用某些错误消息,例如“line to long”(谁没有超过 80 个屏幕字符可供使用?),其操作与上面的代码类似,但是在 linter 配置中,我使用 nvim-lint (老兄只在视频中简要提到了这一点):

local pylint = require("lint").linters.pylint
pylint.args = {
  "-f",
  "--disable=C,R",
}

这来自帖子:如何禁用 Pylint 警告?
此处的附加信息: https://docs.pylint.org/faq.html#can-i-give-pylint-a-file-as-an-argument-instead-of-a-module

If anyone is still having this issue there is linting being through pycodestyle. it appears from the pylsp github page (https://github.com/python-lsp/python-lsp-server#installation) that pylint is disabled by default but the pydocstyle is no which provides a load of errors and warnings if your quite "loose" with coding as i am. to disable this within my config i did the following:

local lspconfig = require("lspconfig")

-- configure python server
lspconfig["pylsp"].setup({
  capabilities = capabilities,
  settings = {
    pylsp = {
      plugins = {
        pylint = { enabled = "false" },
        pyflakes = { enabled = "false" },
        pycodestyle = { enabled = "false" },
      }
    }
  },
  on_attach = on_attach,
})

so if you are using Mason to do linting with pylint and you have the pylsp working also then you may get a lot of errors and some of those might appear multiple times. a good video/videos to watch on lsp and linting that helped me is https://www.youtube.com/watch?v=ybUE4D80XSk&t=1382s&ab_channel=JoseanMartinez and also https://youtu.be/NL8D8EkphUw. My code fits his example he showed in that later video nicely where he uses pyright, i use pylsp instad. The specific part of that later video where he does this is 26:38.
you can also setup options for keeping linting with pylsp and/or pylint but disabling certain error messages like "line to long" (who hasn't got more than 80 chars of screen to use?) that is done similar to this above code but in the linter config, i use nvim-lint (dude only briefly mentions this in the videos):

local pylint = require("lint").linters.pylint
pylint.args = {
  "-f",
  "--disable=C,R",
}

this comes from the post : How do I disable a Pylint warning?
additional info here: https://docs.pylint.org/faq.html#can-i-give-pylint-a-file-as-an-argument-instead-of-a-module

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文