Skip to content

Commit

Permalink
Add mypy as a flycheck checker
Browse files Browse the repository at this point in the history
Rather than allowing the user to specify each argument, allow the user
to set a path to a mypy configuration file.

Output parsing is based on
https://github.com/lbolla/emacs-flycheck-mypy and adds support for
warnings too.
  • Loading branch information
Wilfred committed Jun 5, 2018
1 parent 40ae191 commit d169cab
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Expand Up @@ -21,6 +21,7 @@
- MarkdownLint CLI with ``markdownlint`` [GH-1366]
- Rust with ``rust-clippy`` [GH-1385]
- VHDL with ``ghdl`` [GH-1160]
- mypy with ``python-mypy`` [GH-1354]

- New features:

Expand Down
17 changes: 17 additions & 0 deletions doc/languages.rst
Expand Up @@ -900,6 +900,23 @@ to view the docstring of the syntax checker. Likewise, you may use

.. syntax-checker-config-file:: flycheck-flake8rc

.. syntax-checker:: python-mypy

Type check python with `mypy <http://www.mypy-lang.org/>`_.

.. note::

This syntax checker requires mypy 0.580 or newer.

.. syntax-checker-config-file:: flycheck-python-mypy-ini

.. defcustom:: flycheck-python-mypy-cache-dir

Directory used to write ``.mypy_cache`` directories.

Set to ``null-device`` to disable writing cache directories
entirely.

.. syntax-checker:: python-pylint

Check syntax and lint with `Pylint <https://pylint.org/>`_.
Expand Down
30 changes: 30 additions & 0 deletions flycheck.el
Expand Up @@ -225,6 +225,7 @@ attention to case differences."
python-flake8
python-pylint
python-pycompile
python-mypy
r-lintr
racket
rpm-rpmlint
Expand Down Expand Up @@ -8950,6 +8951,35 @@ See URL `https://docs.python.org/3.4/library/py_compile.html'."
line ", " column ", " (one-or-more not-newline) line-end))
:modes python-mode)

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

(flycheck-def-option-var flycheck-python-mypy-cache-dir nil python-mypy
"Directory used to write .mypy_cache directories."
:safe #'stringp
:type '(choice
(const :tag "Write to the working directory" nil)
(const :tag "Never write .mypy_cache directories" null-device)
(string :tag "Path"))
:package-version '(flycheck . "32"))

(flycheck-define-checker python-mypy
"Mypy syntax and type checker. Requires mypy>=0.580.

See URL `http://mypy-lang.org/'."
:command ("mypy"
(config-file "--config-file" flycheck-python-mypy-ini)
(option "--cache-dir" flycheck-python-mypy-cache-dir)
source-original)
:error-patterns
((error line-start (file-name) ":" line ": error:" (message) line-end)
(warning line-start (file-name) ":" line ": warning:" (message) line-end))
:modes python-mode
;; Ensure the file is saved, to work around https://github.com/python/mypy/issues/4746.
:predicate flycheck-buffer-saved-p
:next-checkers '(t . python-flake8))

(flycheck-def-option-var flycheck-lintr-caching t r-lintr
"Whether to enable caching in lintr.

Expand Down
9 changes: 9 additions & 0 deletions test/flycheck-test.el
Expand Up @@ -3764,6 +3764,15 @@ Why not:
'(22 1 error "undefined name 'antigravity'" :id "F821"
:checker python-flake8)))

(flycheck-ert-def-checker-test python-mypy python nil
(let ((flycheck-disabled-checkers '(python-flake8))
(flycheck-checkers '(python-mypy))
(flycheck-python-mypy-cache-dir null-device))
(flycheck-ert-should-syntax-check
"language/python/invalid_type.py" 'python-mode
'(2 nil error "Incompatible return value type (got \"str\", expected \"int\")"
:checker python-mypy))))

(flycheck-ert-def-checker-test python-pylint python syntax-error
(let ((flycheck-disabled-checkers '(python-flake8))
(python-indent-guess-indent-offset nil)) ; Silence Python Mode
Expand Down
2 changes: 2 additions & 0 deletions test/resources/language/python/invalid_type.py
@@ -0,0 +1,2 @@
def foo(x: str) -> int:
return x

0 comments on commit d169cab

Please sign in to comment.