Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Type Hinting in Emacs

I am comming 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?

like image 382
m3nthal Avatar asked Oct 17 '22 16:10

m3nthal


2 Answers

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

  1. Install pyflakes and mypy using pip.

  2. Install use-package in your Emacs config (I'll leave the instructions for this to you).

  3. 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.

like image 75
jidicula Avatar answered Oct 31 '22 19:10

jidicula


Jedi 0.10.0 has added partial PEP 484 support after https://github.com/davidhalter/jedi/issues/858

like image 40
m3nthal Avatar answered Oct 31 '22 20:10

m3nthal