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

Use python -c to invoke pylint and flake8 #1113

Merged
merged 2 commits into from Feb 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.rst
Expand Up @@ -38,6 +38,8 @@
located. If ``Gemfile`` does not exist, old behaviour of running command
from directory where ``.rubocop.yml`` is found will be used [GH-1368]
- ``rust-cargo`` now uses ``cargo check`` and ``cargo test`` [GH-1289]
- ``python-pylint`` and ``python-flake8`` are now invoked with ``python -c``,
to make it easier to change between Python 2 and Python 3 [GH-1113]

31 (Oct 07, 2017)
=================
Expand Down
62 changes: 58 additions & 4 deletions flycheck.el
Expand Up @@ -8687,6 +8687,46 @@ See URL `http://puppet-lint.com/'."
;; the buffer is actually linked to a file, and if it is not modified.
:predicate flycheck-buffer-saved-p)

(defun flycheck-python-find-module (checker module)
"Check if a Python MODULE is available.
CHECKER's executable is assumed to be a Python REPL."
(-when-let* ((py (flycheck-find-checker-executable checker))
(script (concat "import sys; sys.path.pop(0);"
(format "import %s; print(%s.__file__)"
module module))))
(with-temp-buffer
(and (eq (ignore-errors (call-process py nil t nil "-c" script)) 0)
(string-trim (buffer-string))))))

(defun flycheck-python-needs-module-p (checker)
"Determines whether CHECKER needs to be invoked through Python.
Previous versions of Flycheck called pylint and flake8 directly;
this check ensures that we don't break existing code."
(not (string-match-p (rx (or "pylint" "flake8")
(or "-script.pyw" ".exe" ".bat" "")
eos)
(flycheck-checker-executable checker))))

(defun flycheck-python-verify-module (checker module)
"Verify that a Python MODULE is available.
Return nil if CHECKER's executable is not a Python REPL. This
function's is suitable for a checker's :verify."
(when (flycheck-python-needs-module-p checker)
(let ((mod-path (flycheck-python-find-module checker module)))
(list (flycheck-verification-result-new
:label (format "`%s' module" module)
:message (if mod-path (format "Found at %S" mod-path) "Missing")
:face (if mod-path 'success '(bold error)))))))

(defun flycheck-python-module-args (checker module-name)
"Compute arguments to pass to CHECKER's executable to run MODULE-NAME.
Return nil if CHECKER's executable is not a Python REPL.
Otherwise, return a list starting with -c (-m is not enough
because it adds the current directory to Python's path)."
(when (flycheck-python-needs-module-p checker)
`("-c" ,(concat "import sys,runpy;sys.path.pop(0);"
(format "runpy.run_module(%S)" module-name)))))

(flycheck-def-config-file-var flycheck-flake8rc python-flake8 ".flake8rc"
:safe #'stringp)

Expand Down Expand Up @@ -8764,7 +8804,10 @@ Update the error level of ERR according to

Requires Flake8 3.0 or newer. See URL
`https://flake8.readthedocs.io/'."
:command ("flake8"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment saying why we don't call flake8 directly and linking to this PR (or #1055).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

;; Not calling flake8 directly makes it easier to switch between different
;; Python versions; see https://github.com/flycheck/flycheck/issues/1055.
:command ("python"
(eval (flycheck-python-module-args 'python-flake8 "flake8"))
"--format=default"
(config-file "--config" flycheck-flake8rc)
(option "--max-complexity" flycheck-flake8-maximum-complexity nil
Expand All @@ -8782,10 +8825,13 @@ Requires Flake8 3.0 or newer. See URL
(id (one-or-more (any alpha)) (one-or-more digit)) " "
(message (one-or-more not-newline))
line-end))
:enabled (lambda ()
(or (not (flycheck-python-needs-module-p 'python-flake8))
(flycheck-python-find-module 'python-flake8 "flake8")))
:verify (lambda (_) (flycheck-python-verify-module 'python-flake8 "flake8"))
:modes python-mode)

(flycheck-def-config-file-var flycheck-pylintrc python-pylint
".pylintrc"
(flycheck-def-config-file-var flycheck-pylintrc python-pylint ".pylintrc"
:safe #'stringp)

(flycheck-def-option-var flycheck-pylint-use-symbolic-id t python-pylint
Expand All @@ -8805,7 +8851,11 @@ This syntax checker requires Pylint 1.0 or newer.

See URL `https://www.pylint.org/'."
;; -r n disables the scoring report
:command ("pylint" "-r" "n"
;; Not calling pylint directly makes it easier to switch between different
;; Python versions; see https://github.com/flycheck/flycheck/issues/1055.
:command ("python"
(eval (flycheck-python-module-args 'python-pylint "pylint"))
"-r" "n"
"--output-format" "text"
"--msg-template"
(eval (if flycheck-pylint-use-symbolic-id
Expand All @@ -8830,6 +8880,10 @@ See URL `https://www.pylint.org/'."
(info line-start (file-name) ":" line ":" column ":"
"C:" (id (one-or-more (not (any ":")))) ":"
(message) line-end))
:enabled (lambda ()
(or (not (flycheck-python-needs-module-p 'python-pylint))
(flycheck-python-find-module 'python-pylint "pylint")))
:verify (lambda (_) (flycheck-python-verify-module 'python-pylint "pylint"))
:modes python-mode)

(flycheck-define-checker python-pycompile
Expand Down
3 changes: 2 additions & 1 deletion maint/flycheck-checkdoc.el
Expand Up @@ -74,7 +74,8 @@ location and error message."
;; Switch to Emacs Lisp mode to give checkdoc the proper syntax table, etc.
(delay-mode-hooks (emacs-lisp-mode))
(setq delay-mode-hooks nil)
(checkdoc-current-buffer 'take-notes)
(let ((checkdoc-arguments-in-order-flag nil))
(checkdoc-current-buffer 'take-notes))
(flycheck/checkdoc-get-current-errors)))

(defun flycheck/batch-checkdoc ()
Expand Down