Skip to content

Instantly share code, notes, and snippets.

@dsedivec
Forked from abo-abo/flycheck-ruff.el
Last active January 11, 2024 18:33
Show Gist options
  • Save dsedivec/65eff752ec3aa3b652977c6e681bd609 to your computer and use it in GitHub Desktop.
Save dsedivec/65eff752ec3aa3b652977c6e681bd609 to your computer and use it in GitHub Desktop.
Emacs ruff flycheck config
(require 'flycheck)
;; From https://github.com/flycheck/flycheck/issues/1974#issuecomment-1343495202
(flycheck-define-checker python-ruff
"A Python syntax and style checker using the ruff utility.
To override the path to the ruff executable, set
`flycheck-python-ruff-executable'.
See URL `http://pypi.python.org/pypi/ruff'."
:command ("ruff"
"check"
"--output-format=text"
(eval (when buffer-file-name
(concat "--stdin-filename=" buffer-file-name)))
"-")
:standard-input t
:error-filter (lambda (errors)
(let ((errors (flycheck-sanitize-errors errors)))
(seq-map #'flycheck-flake8-fix-error-level errors)))
:error-patterns
((warning line-start
(file-name) ":" line ":" (optional column ":") " "
(id (one-or-more (any alpha)) (one-or-more digit)) " "
(message (one-or-more not-newline))
line-end))
:modes (python-mode python-ts-mode))
;; Use something adapted to your config to add `python-ruff' to `flycheck-checkers'
;; This is an MVP example:
(setq python-mode-hook
(list (defun my-python-hook ()
(unless (bound-and-true-p org-src-mode)
(when (buffer-file-name)
(setq-local flycheck-checkers '(python-ruff))
(flycheck-mode))))))
@dsedivec
Copy link
Author

dsedivec commented Jan 11, 2024

@dsedivec How do you know if it's actually using ruff or not? Sorry if this is a dumb question, I didn't have much luck finding it out myself.

@chookity-pokk

  1. C-c ! v will show the checkers in use. You should see python-ruff listed at the top under active checkers.
  2. C-c ! c should run an immediate check.
  3. When in doubt, temporarily modify the above code to add something invalid to the command line, like change "check" to "barkbark". If Flycheck runs ruff, presumably barkbark will cause an error, and Flycheck should report that error back to you.

@chookity-pokk
Copy link

chookity-pokk commented Jan 11, 2024

Sorry, you responded much faster than I expected. I updated my question with the output of C-c ! v and it says ruff isn't registered. So I registered it and disabled all other linters but then flycheck throws the below error. Have you by chance gotten the same thing?

Suspicious state from syntax checker python-ruff: Flycheck checker python-ruff returned 2, but its output contained no errors: error: unexpected argument '--output-format' found

  tip: a similar argument exists: '--output-file'

Usage: ruff check <FILES|--fix|--no-fix|--show-source|--no-show-source|--show-fixes|--no-show-fixes|--diff|--watch|--fix-only|--no-fix-only|--ignore-noqa|--format <FORMAT>|--output-file <OUTPUT_FILE>|--target-version <TARGET_VERSION>|--config <CONFIG>|--select <RULE_CODE>|--ignore <RULE_CODE>|--extend-select <RULE_CODE>|--extend-ignore <RULE_CODE>|--per-file-ignores <PER_FILE_IGNORES>|--extend-per-file-ignores <EXTEND_PER_FILE_IGNORES>|--exclude <FILE_PATTERN>|--extend-exclude <FILE_PATTERN>|--fixable <RULE_CODE>|--unfixable <RULE_CODE>|--extend-fixable <RULE_CODE>|--extend-unfixable <RULE_CODE>|--respect-gitignore|--no-respect-gitignore|--force-exclude|--no-force-exclude|--line-length <LINE_LENGTH>|--dummy-variable-rgx <DUMMY_VARIABLE_RGX>|--no-cache|--isolated|--cache-dir <CACHE_DIR>|--stdin-filename <STDIN_FILENAME>|--exit-zero|--exit-non-zero-on-fix|--statistics|--add-noqa|--show-files|--show-settings|--ecosystem-ci>

For more information, try '--help'.

Try installing a more recent version of python-ruff, and please open a bug report if the issue persists in the latest release.  Thanks!

For what it's worth, I am using ruff 0.1.11

@chookity-pokk
Copy link

Nevermind, the problem was I apparently had ruff installed with both pip and homebrew so it was using the pip version which was very out of date. So adding the homebrew one to the exec path fixed the issue. Thank you for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment