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
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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))))))
@chookity-pokk
Copy link

chookity-pokk commented Jan 11, 2024

@dsedivec How do you know if it's actually using ruff or not? When I run C-c ! v I get that my first checker to run is still flake8.

Syntax checkers for buffer compilation.py in python-ts-mode:

First checker to run:

  python-flake8
    - may enable:         yes
    - configuration file: Not found
    - next checkers:      python-pylint, python-mypy

Checkers that may run as part of the first checker's chain:

  python-pylint
    - may enable:         yes
    - next checkers:      python-mypy

Checkers that could run if selected:

  python-pycompile  select
    - may enable:    yes
    - next checkers: python-mypy

  python-pyright  select
    - may enable: yes
    - executable: Found at /opt/homebrew/bin/pyright

Checkers that are compatible with this mode, but will not run until properly configured:

  python-mypy (automatically disabled) reset
    - may enable:         no
    - may run:            t
    - executable:         Not found

The following syntax checkers are not registered:
  - python-ruff
Try adding these syntax checkers to `flycheck-checkers'.

Flycheck Mode is enabled.  Use C-u C-c ! x to enable disabled checkers.

--------------------

Flycheck version: 33snapshot (package: 20230813.620)
Emacs version:    29.1
System:           aarch64-apple-darwin21.6.0
Window system:    ns

@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