我目前正在研究 Sublime Text 3 使用 Jedi - Python 自动完成,它适用于大多数基本的东西。 但是我像在这种情况下使用 BeautifulSoup4 一样使用它
主要 问题是在文件上执行多个点 (.
) 方法时无法正确显示补全,补全者必须像 一样首先看到它>.find_all
方法然后它会提示它(但这似乎是 Sublime Text 3 本身的自动完成)。
接下来会发生什么
import requests
from bs4 import BeautifulSoup as Soup # works ok, shows all suggestions
request = requests.get('http://example.com')
soup = Soup(request.text, 'lxml')
main = soup.find('body') # shows find method
# However, No available completions in the next case
second_lookup = main.find('div') # doesn't show any autocompletions/hints when starting w/ .fi..
在寻找任何其他“更深入”的自动完成方法时也是如此。到目前为止,我已经尝试调整 Jedi..settings 文件中的所有设置。这没有帮助,我尝试使用 Anaconda,因为它有一些额外的工具,包括 Jedi。
这似乎特定于某些库,例如 numpy 和 bs4。
注意事项:
这不是特定于 Sublime Text 3。 Atom 和类似的 IDE 也是如此。
最佳答案
Python 是一种动态语言。函数或方法的参数完全依赖于 docstrings 的类型规范.返回类型也是如此。
例如,这是docstring
get
的(或文档) requests
的功能模块:
def get(url, params=None, **kwargs):
r"""Sends a GET request.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
kwargs.setdefault('allow_redirects', True)
return request('get', url, params=params, **kwargs)
返回类型在函数定义中指定。您还可以指定函数的参数类型。
然而,find
Soup
的方法|类是这样写的:
def find(self, name=None, attrs={}, recursive=True, text=None,
**kwargs):
"""Return only the first child of this Tag matching the given
criteria."""
r = None
l = self.find_all(name, attrs, recursive, text, 1, **kwargs)
if l:
r = l[0]
return r
编者可查find
Soup
的方法|类(class)。但是,他们不知道此方法返回什么类型的对象。
解决方法是 specify type分配给变量时:
import requests
from bs4 import Tag
from bs4 import BeautifulSoup as Soup
request = requests.get('http://example.com')
soup = Soup(request.text, 'lxml')
main: Tag = soup.find('body')
# Auto completion works.
second_lookup = main.find('div')
或者您可以添加 :rtype: Tag
至 find
文档字符串。我知道它返回 Tag
自 type(main)
以来的对象或 type(second_lookup)
都返回 <class 'bs4.element.Tag'>
.
我提供的链接足以让您了解 python 中的静态类型并完美地记录您的代码。希望这可以帮助。
关于python - Jedi - Python 自动完成(提示)不显示方法或建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58594395/