[英]Python Type Hinting in Emacs
I am comming from PyCharm wanting to learn about Python setup with Emacs (Spacemacs).我来自 PyCharm,想了解如何使用 Emacs (Spacemacs) 设置 Python。
PyCharm has this feature called Type Hinting which basically allows specifying types and then get hints based on the specified types. PyCharm 有一个称为类型提示的功能,它基本上允许指定类型,然后根据指定的类型获取提示。 Python 3.5+ has PEP 484 -- Type Hints which allows specifying types without comments.
Python 3.5+ 具有PEP 484 -- 类型提示,允许指定类型而不使用注释。 Before PEP 484, specifying types was done using comments.
在 PEP 484 之前,指定类型是使用注释完成的。
Is there such Type Hinting available with Emacs? Emacs 有这样的类型提示吗?
Jedi 0.10.0在https://github.com/davidhalter/jedi/issues/858之后添加了部分 PEP 484 支持
I came across this question in 2020 and found that there is some better tooling for this.我在 2020 年遇到了这个问题,发现有一些更好的工具可以解决这个问题。 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
、 flycheck
flycheck-pycheckers
、 pyflakes
和mypy
。
Elpy is the Emacs Python IDE package. Elpy 是 Emacs Python IDE 包。 flycheck is a general syntax-checking package for Emacs.
flycheck 是 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.
flycheck-pycheckers 与 flycheck 配合使用,允许您使用多个 Python 语法检查器,因为 pyflakes 不处理类型提示,而 mypy 不处理一般语法——您需要同时使用两者。
So your steps would be to所以你的步骤是
Install pyflakes and mypy using pip.使用 pip 安装 pyflakes 和 mypy。
Install use-package
in your Emacs config (I'll leave the instructions for this to you).在您的 Emacs 配置中安装
use-package
(我会将相关说明留给您)。
Add the following lines to your init.el or .emacs:将以下行添加到您的 init.el 或 .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.此解决方案将让您在 Emacs 中使用 Python 语法和类型提示进行设置。
elpy
was great in the past, I used a setup very similar to that of @jidicula. elpy
过去很棒,我使用的设置与@jidicula 非常相似。 However, elpy
no longer works with current versions of jedi
and this problem has not been fixed for over a year now.但是,
elpy
不再适用于当前版本的jedi
,并且这个问题已经一年多没有得到解决。 Turns out elpy
is no longer maintained, see https://github.com/jorgenschaefer/elpy原来
elpy
不再维护,请参阅https://github.com/jorgenschaefer/elpy
I tried eglot
and it does the job.我尝试
eglot
,它完成了这项工作。 However, eglot
author insists on using obsolete flymake
instead of its modern replacement flycheck
.然而,
eglot
作者坚持使用过时的flymake
而不是现代的替代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. flycheck
可以同时使用mypy3
和pyflakes
,而flymake
不能,所以必须在同一个缓冲区中同时使用flycheck
和flymake
,这会产生不必要的摩擦和新的困难,我不想浪费我的时间。
lsp-mode
appears to be the best choice as of now. lsp-mode
似乎是目前最好的选择。 It is more popular, has 5 times more contributors and is more flexible and less opinionated because it works with both flycheck
and flymake
.它更受欢迎,贡献者多 5 倍,并且更灵活且不那么固执己见,因为它可以与
flycheck
和flymake
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.