Package: python-lsp-ruff
python-lsp-ruffpython-lsp-ruffis a plugin forpython-lsp-serverthat adds linting, code action and formatting capabilities that are provided byruff,
an extremely fast Python linter and formatter written in Rust.InstallIn the samevirtualenvaspython-lsp-server:pipinstallpython-lsp-ruffThere also exists anAUR package.When using ruff before version 0.1.0Ruff version0.1.0introduced API changes that are fixed in Python LSP Ruffv1.6.0. To continue withruff<0.1.0please usev1.5.3, e.g. usingpip:pipinstall"ruff<0.1.0""python-lsp-ruff==1.5.3"UsageThis plugin will disablepycodestyle,pyflakes,mccabe,autopep8, andyapfby default, unless they are explicitly enabled in the client configuration.
Whenpython-lsp-ruffis enabled, all linting diagnostics and formatting capabilities will be provided byruff.Any codes given in theformatoption will only be marked asfixablefor ruff during the formatting operation, the user has to make sure that these codes are also in the list of codes that ruff checks!ConfigurationConfiguration options can be passed to the python-language-server. If apyproject.tomlfile is present in the project,python-lsp-ruffwill ignore specific options (see below).The plugin followspython-lsp-server's configuration.
This example configuration using forneovimshows the possible options:pylsp={plugins={ruff={enabled=true,-- Enable the pluginexecutable="<path-to-ruff-bin>",-- Custom path to ruffconfig="<path_to_custom_ruff_toml>",-- Custom config for ruff to useextendSelect={"I"},-- Rules that are additionally used by ruffextendIgnore={"C90"},-- Rules that are additionally ignored by ruffformat={"I"},-- Rules that are marked as fixable by ruff that should be fixed when running textDocument/formattingseverities={["D212"]="I"},-- Optional table of rules where a custom severity is desiredunsafeFixes=false,-- Whether or not to offer unsafe fixes as code actions. Ignored with the "Fix All" action-- Rules that are ignored when a pyproject.toml or ruff.toml is present:lineLength=88,-- Line length to pass to ruff checking and formattingexclude={"__about__.py"},-- Files to be excluded by ruff checkingselect={"F"},-- Rules to be enabled by ruffignore={"D210"},-- Rules to be ignored by ruffperFileIgnores={["__init__.py"]="CPY001"},-- Rules that should be ignored for specific filespreview=false,-- Whether to enable the preview style linting and formatting.targetVersion="py310",-- The minimum python version to target (applies for both linting and formatting).},}}For more information on the configuration visitRuff's homepage.Custom severitiesBy default, all diagnostics are marked as warning, except for"E999"and all error codes starting with"F", which are displayed as errors.
This default can be changed through thepylsp.plugins.ruff.severitiesoption, which takes the error code as a key and any of"E","W","I"and"H"to be displayed as errors, warnings, information and hints, respectively.
For more information on the diagnostic severities please refer tothe official LSP reference.Withv2.0.0it is also possible to use patterns to match codes. Rules match if the error code starts with the given pattern. If multiple patterns match the error code,python-lsp-ruffchooses the one with the most amount of matching characters.Code formattingWithpython-lsp-ruff>1.6.0formatting is done usingruffs own formatter.
In addition, rules that should be fixed during thetextDocument/formattingrequest can be added with theformatoption.Coming from previous versions the only change is thatisortrules arenotapplied by default.
To enable sorting of imports using ruff's isort functionality, add"I"to the list offormatrules.Code actionspython-lsp-ruffsupports code actions as given by possible fixes byruff.python-lsp-ruffalso supportsunsafe fixes.
Fixes considered unsafe byruffare marked(unsafe)in the code action.
TheFix allcode actiononlyconsideres safe fixes.