Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ruff #1974

Closed
1 task done
failable opened this issue Sep 6, 2022 · 11 comments · Fixed by #2033
Closed
1 task done

Add support for ruff #1974

failable opened this issue Sep 6, 2022 · 11 comments · Fixed by #2033

Comments

@failable
Copy link

failable commented Sep 6, 2022

Thank you for taking the time to improve Flycheck.

Checklist

  • I have checked existing issues for potential duplicates before creating this one.

Feature description

Add support for ruff (An extremely fast Python linter, written in Rust.).

@mhvk
Copy link

mhvk commented Dec 8, 2022

I don't know enough about flycheck to be sure this is enough, but the following in a separate flycheck-ruff.el (and then (require 'flycheck-ruff) does seem to work:

(require 'flycheck)

(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"
            "--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)

(add-to-list 'flycheck-checkers 'python-ruff)

(provide 'flycheck-ruff)

(based on https://github.com/Wilfred/flycheck-pyflakes and the flake8 bits of flycheck)

@Yevgnen
Copy link

Yevgnen commented Dec 14, 2022

@mhvk Seems work for me!

@Paulswith
Copy link

I don't know enough about flycheck to be sure this is enough, but the following in a separate flycheck-ruff.el (and then (require 'flycheck-ruff) does seem to work:

(require 'flycheck)

(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"
            "--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)

(add-to-list 'flycheck-checkers 'python-ruff)

(provide 'flycheck-ruff)

(based on https://github.com/Wilfred/flycheck-pyflakes and the flake8 bits of flycheck)

Cool!! I use Spacemacs and it works for me.

@rpeck
Copy link

rpeck commented Jul 14, 2023

Woo! It's working great!

It'd be so great if we had ruff's "quick fix" and "fix the whole buffer" features. Even after 36 years of emacs, I'm not elisp-enabled enough to knock that out quickly. :-(

@rpeck
Copy link

rpeck commented Jul 14, 2023

Something that might be an easy, incremental improvement is to add ruff to the UI menu (Python -> Checks -> Ruff). I'm not normally a menu guy, but this would be a nice add.

@mhvk
Copy link

mhvk commented Jul 14, 2023

Auto-fix would indeed be nice, but I'm afraid I'm not enough of an emacs expert to do that...

@vikigenius
Copy link

@rpeck You can get ruff's auto-fix if you use Ruff as an lsp-server instead of as a flycheck-checker

@mhvk
Copy link

mhvk commented Jul 14, 2023

@vikigenius - that sounds very interesting! Do you have a snippet for enabling that?

@vikigenius
Copy link

Well, I use doom emacs so it already has lsp mode pre enabled and auto configured. As long as the lsp-mode is able to find a package called ruff-lsp in your Path (I typically have it installed in my venv) it automatically starts ruff alongside any other python language servers you may have.

wyuenho added a commit to wyuenho/flycheck that referenced this issue Sep 7, 2023
wyuenho added a commit to wyuenho/flycheck that referenced this issue Sep 7, 2023
wyuenho added a commit to wyuenho/flycheck that referenced this issue Sep 7, 2023
@waveFrontSet
Copy link
Contributor

waveFrontSet commented Oct 25, 2023

Already noted that in the associated pull request: The --format option has been replaced by --output-format now. For convenience, here's how it currently works for me in Doom Emacs (config.el):

(flycheck-def-config-file-var flycheck-python-ruff-config python-ruff
                              '("pyproject.toml" "ruff.toml" ".ruff.toml"))

(flycheck-define-checker python-ruff
  "A Python syntax and style checker using the ruff.
To override the path to the ruff executable, set
`flycheck-python-ruff-executable'.

See URL `https://beta.ruff.rs/docs/'."
  :command ("ruff"
            "check"
            (config-file "--config" flycheck-python-ruff-config)
            "--output-format=text"
            "--stdin-filename" source-original
            "-")
  :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)
  :next-checkers ((warning . python-mypy)))

;; Python config: Use ruff + mypy.
(defun python-flycheck-setup ()
  (progn
    (flycheck-select-checker 'python-ruff)
    (flycheck-add-next-checker 'python-ruff 'python-mypy)
    ))
(after! flycheck
  (add-to-list 'flycheck-checkers 'python-ruff)
  (add-hook 'python-mode-local-vars-hook #'python-flycheck-setup 'append)
  )

@mhvk
Copy link

mhvk commented Feb 4, 2024

Thanks!

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

Successfully merging a pull request may close this issue.

7 participants