1

Can somebody who has made type hints work give the init.el example that shows how? And provide description of how it works for you inside Emacs?

I gather that some people have successfully moved from Elpy to LSP as a Python development tool.

https://stackoverflow.com/a/73059420/1086346

I've been experimenting with this for a week and failing. I am able to get LSP to work with jedi for displaying info on built-in python definitions.

I miss some other features in Elpy, however, and I'm trying to decide if the LSP effort is worthwhile.

This page on Medium claims they make type hints work via linkage between LSP and mypy,

https://medium.com/@enzuru/helpful-emacs-python-mode-hooks-especially-for-type-hinting-c4b70b9b221

but I don't see any hints when I try that.

So what I'm asking for is the smallest path to get type hints, especially if I don't have to install Evil or Anaconda-mode, neither of which I want :)

1 Answer 1

1

Yes, it is possible to configure lsp to work with mypy. Below is an example showing a function configured with type annotation and the errors found by mypy.

enter image description here The server chosen: pylsp, with all dependencies and the plugins specified, with the main interest for pylsp-mypy.

The lsp configuration code:

(use-package lsp-mode
  :commands lsp
  :custom
  (lsp-eldoc-render-all nil)  ;; This show hover info in minibuffer
  (lsp-idle-delay 0.2)

  :init
  (setq lsp-keymap-prefix "C-c l")
  (setq lsp-enable-text-document-color nil)
  (setq read-process-output-max (* 1024 1024))
  ;; Adding pylint as linter for python, with pylsp.
  (setq lsp-pylsp-plugins-pylint-enabled t)
  (setq lsp-pylsp-plugins-pylint-args ["--rcfile=/Users/yname/.pylintrc"])
  (setq lsp-pylsp-configuration-sources ["pylsp-mypy"])

  :hook
  (lsp-mode . lsp-enable-which-key-integration)
  :config
  (add-hook 'lsp-mode-hook 'lsp-ui-mode)
  (add-hook 'python-mode-hook #'lsp-deferred)
  )

In project root there is a small mypy.ini file with the following content:

[mypy]
disallow_untyped_defs = True
disallow_any_unimported = True
no_implicit_optional = True
check_untyped_defs = True
warn_return_any = True
warn_unused_ignores = True
show_error_codes = True

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.