Reputation: 433
I am coming from PyCharm wanting to learn about Python setup with Emacs (Spacemacs).
PyCharm has this feature called Type Hinting which basically allows specifying types and then get hints based on the specified types. Python 3.5+ has PEP 484 -- Type Hints which allows specifying types without comments. Before PEP 484, specifying types was done using comments.
Is there such Type Hinting available with Emacs?
Upvotes: 8
Views: 4349
Reputation: 136505
elpy
was great in the past, I used a setup very similar to that of @jidicula. However, elpy
no longer works with current versions of jedi
and this problem has not been fixed for over a year now. Turns out elpy
is no longer maintained, see https://github.com/jorgenschaefer/elpy
I tried eglot
and it does the job. However, eglot
author insists on using obsolete flymake
instead of its modern replacement flycheck
. flycheck
can use both mypy3
and pyflakes
, whereas flymake
cannot, so one has to use both flycheck
and flymake
in the same buffer, which creates unnecessary friction and new difficulties which I'd rather not waste my time on.
lsp-mode
appears to be the best choice as of now. It is more popular, has 5 times more contributors and is more flexible and less opinionated because it works with both flycheck
and flymake
.
I install lsp-mode
Python dependencies with:
conda install python-lsp-server pylsp-mypy
These should also be available with pip
.
Upvotes: 3
Reputation: 3989
I came across this question in 2020 and found that there is some better tooling for this. Jedi alone probably won't get you what you want. Instead, you will likely want to use a combo of elpy
, flycheck
flycheck-pycheckers
, pyflakes
, and mypy
.
Elpy is the Emacs Python IDE package. flycheck is a general syntax-checking package for Emacs. flycheck-pycheckers works with flycheck to allow you to use more than one Python syntax checker since pyflakes doesn't handle type hints and mypy doesn't handle general syntax – you'll need to use both.
So your steps would be to
Install pyflakes and mypy using pip.
Install use-package
in your Emacs config (I'll leave the instructions for this to you).
Add the following lines to your init.el or .emacs:
;; flycheck
(use-package flycheck
:ensure t
:config
(global-flycheck-mode t)
;; note that these bindings are optional
(global-set-key (kbd "C-c n") 'flycheck-next-error)
;; this might override a default binding for running a python process,
;; see comments below this answer
(global-set-key (kbd "C-c p") 'flycheck-prev-error)
)
;; flycheck-pycheckers
;; Allows multiple syntax checkers to run in parallel on Python code
;; Ideal use-case: pyflakes for syntax combined with mypy for typing
(use-package flycheck-pycheckers
:after flycheck
:ensure t
:init
(with-eval-after-load 'flycheck
(add-hook 'flycheck-mode-hook #'flycheck-pycheckers-setup)
)
(setq flycheck-pycheckers-checkers
'(
mypy3
pyflakes
)
)
)
;; elpy
(use-package elpy
:after poetry
:ensure t
:config
(elpy-enable)
(add-hook 'elpy-mode-hook 'poetry-tracking-mode) ;; optional if you're using Poetry
(setq elpy-rpc-virtualenv-path 'current)
(setq elpy-syntax-check-command "~/.pyenv/shims/pyflakes") ;; or replace with the path to your pyflakes binary
;; allows Elpy to see virtualenv
(add-hook 'elpy-mode-hook
;; pyvenv-mode
'(lambda ()
(pyvenv-mode +1)
)
)
;; use flycheck instead of flymake
(when (load "flycheck" t t)
(setq elpy-modules (delq 'elpy-module-flymake elpy-modules))
(add-hook 'elpy-mode-hook 'flycheck-mode))
)
;; poetry
(use-package poetry
:ensure t)
This solution will get you set up with both Python syntax and type hints in Emacs.
Upvotes: 3
Reputation: 433
Jedi 0.10.0 has added partial PEP 484 support after https://github.com/davidhalter/jedi/issues/858
Upvotes: 3