elpy icon indicating copy to clipboard operation
elpy copied to clipboard

Support typing/mypy based type checking

Open RyanKung opened this issue 9 years ago • 7 comments

The (https://github.com/python/mypy)[mypy] (buildin typing module in Python 3.5) can be use to do some Type checking.

There is a very very simple implementation with flymake: https://github.com/RyanKung/flymake-mypy

I think to support mypy/typing checking is a good idea for elpy.

RyanKung avatar Jul 06 '16 08:07 RyanKung

Nice idea, thank you!

jorgenschaefer avatar Jul 08 '16 09:07 jorgenschaefer

I also belive is a great idea. MyPy requires a venv with Python3 (if you work primarily with Python 2, which is my case). Any pointers on how make this work within Emacs? I would be glad to try to help here.

mfcabrera avatar May 11 '17 07:05 mfcabrera

@mfcabrera "For Python 2.7, you can add annotations as comments (this is also specified in PEP 484)." From https://github.com/python/mypy

And a really simple implementation which calls mypy via flymake is just like this(From https://github.com/RyanKung/flymake-mypy)

(defun flymake-mypy-init ()
  "Init mypy."
  (let* ((temp-file (flymake-init-create-temp-buffer-copy
                     'flymake-create-temp-inplace))
         (local-file (file-relative-name
                      temp-file
                      (file-name-directory buffer-file-name))))
    (list "mypy" (list local-file "-s"))))

RyanKung avatar May 11 '17 08:05 RyanKung

@RyanKung hi Ryan! Thanks for the tip. Indeed I know, and I am using it right now with Python 2.7. Thats the main Python of my system. So I created a venv with Python3.6 and installed mypy (mypy request Py3). I was wondering if there was a way to call the mypy binary within that venv from Emacs. The other alternative is to make Py3 the main Python of my system (i.e. Anaconda) and have my projects use P2.7 from a venv I guess.

mfcabrera avatar May 11 '17 08:05 mfcabrera

@mfcabrera Have you tried to call mypy exec fire directly? I think mypy will find the right Python itself via shebang.

(mypy) ➜  ~ cat /Users/ryan/Envs/AI/bin/mypy
#!/Users/ryan/Envs/AI/bin/python3.6
"""Mypy type checker command line tool."""

from mypy.main import main

main(__file__)

RyanKung avatar May 11 '17 09:05 RyanKung

I tried using a variation on https://github.com/msherry/flycheck-pycheckers/blob/master/bin/pycheckers.py to run flake8 and mypy and maybe others in parallel, but I discovered that elpy/python.el/flymake.el are expecting to be able to pass the file as the checker stdin.

I didn't found a way to give the file to check as checker first parameter as expected by mypy or pylint for example.

Typically pylint will whine about stdin not being a valid module name:

WARNING C0103:pylint: Module name "pipe:[17210525]" doesn't conform to snake_case naming style  ("invalid-name") at /dev/stdin line 1,1.

JulienPalard avatar Apr 05 '19 09:04 JulienPalard

Elpy may not have native Mypy support, but it does integrate well with Flycheck, which does.

If you use Flycheck instead of Flymake with Elpy, the Flycheck docs discuss how to make Flycheck run multiple checkers.

The docs say the Flake8 checker should run after the Mypy by defeault, but in the actual code, it seems to be the other way around; if you set flycheck-checker to python-flake8 (either as a file-/dir-local or using flycheck-select-checker), Flycheck will run Mypy after Flake8 (at least if there were no Flake8 errors).

jirassimok avatar Dec 26 '23 22:12 jirassimok