Flymake plugin to run a linter for python buffers using ruff
Make sure you have at least
ruff0.1.0version because there is a breaking change in the output format flag If you are using a version ofruff<0.5.0, set flymake-ruff-program-args to'("--output-format" "text" "--exit-zero" "--quiet" "-")
Clone this repo somewhere, and add this to your config:
(add-to-list 'load-path "path where the repo was cloned")
(require 'flymake-ruff)
(add-hook 'python-mode-hook #'flymake-ruff-load)(use-package flymake-ruff
:ensure t
:hook (python-mode . flymake-ruff-load))(use-package flymake-ruff
:straight (flymake-ruff
:type git
:host github
:repo "erickgnavar/flymake-ruff"))To use flymake-ruff together with eglot, you should add flymake-ruff-load to
eglot-managed-mode-hook instead. For example:
(add-hook 'eglot-managed-mode-hook 'flymake-ruff-load)Or, if you use use-package:
(use-package flymake-ruff
:ensure t
:hook (eglot-managed-mode . flymake-ruff-load))Pyright has some diagnostic notes that overlap with diagnostics provided by
ruff. These diagnostic notes can't be disabled via Pyright's config, but you can
exclude them by adding a filter to eglot--report-to-flymake. For example, to
remove Pyright's "variable not accessed" notes, add the following:
(defun my-filter-eglot-diagnostics (diags)
"Drop Pyright 'variable not accessed' notes from DIAGS."
(list (seq-remove (lambda (d)
(and (eq (flymake-diagnostic-type d) 'eglot-note)
(s-starts-with? "Pyright:" (flymake-diagnostic-text d))
(s-ends-with? "is not accessed" (flymake-diagnostic-text d))))
(car diags))))
(advice-add 'eglot--report-to-flymake :filter-args #'my-filter-eglot-diagnostics)The flymake-ruff-goto-doc function scans the Flymake diagnostics buffer at
point for a "RULE123"-style code and opens its reference page at
https://docs.astral.sh/ruff/rules.
To bind it to a key in Flymake diagnostics buffers:
(with-eval-after-load 'flymake
(define-key flymake-diagnostics-buffer-mode-map
(kbd "M-RET") #'flymake-ruff-goto-doc)
(define-key flymake-project-diagnostics-mode-map
(kbd "M-RET") #'flymake-ruff-goto-doc))Now, when you’re in any Flymake diagnostics buffer, pressing M-RET on a
line containing a Ruff rule will open the corresponding rule page in your
browser.