python - Jedi - Python 自动完成(提示)不显示方法或建议

标签 python python-jedi

我目前正在研究 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。

这似乎特定于某些库,例如 numpybs4

注意事项:

不是特定于 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: Tagfind文档字符串。我知道它返回 Tagtype(main) 以来的对象或 type(second_lookup)都返回 <class 'bs4.element.Tag'> .

我提供的链接足以让您了解 python 中的静态类型并完美地记录您的代码。希望这可以帮助。

关于python - Jedi - Python 自动完成(提示)不显示方法或建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58594395/

相关文章:

python - Pandas - 使用定界符拆分文本

python - VSCode 中的失控绝地语言任务

vim - 无法使jedi-vim插件正常工作

python - jedi 应该安装在每个 python 项目环境中吗?

python - 如何从以空格结尾的字符串中排除子字符串?

python - 如何从父实体获取后代

python - 无法使用 dataframe.loc 提取通过 dataframe.series 添加列创建的数据框

python - 使用 python cgi 发送 .png 文件