Pycharm:代码完成不提供建议 [英] Pycharm: Code completion not giving recommendations

查看:35
本文介绍了Pycharm:代码完成不提供建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我正在使用请求"python 库.

req = requests.get("http://google.com")

现在,如果我输入req.,我应该得到我可以访问的所有方法的列表.但由于某种原因我不这样做,即使我手动按 ctrl-space.

如果我在 ipython 中尝试这个,我会得到自动完成建议.即使我通过 pycharm 中的内置 python 控制台尝试它,我也会得到建议.

为什么会这样?

解决方案

由于 Python 是一种动态类型语言,您需要确保它可以计算出事物的类型,并正确检查系统上的库.尽量确保代码中对象的类型很明显.

从 PyCharm 2.7 开始(当版本还是数字时)的一个好方法是启用运行时类型检测 - PyCharm 在程序运行时(在调试时)挂钩到您的程序中,并在使用变量时检查它们的类型.

您可以通过转到设置,转到构建、执行、部署"部分,然后转到Python 调试器"小节并启用收集运行时类型信息以获取代码洞察"来启用此功能.

显然值得注意的是,这并不完美 - 如果您进行更改,则在代码执行之前不会更新,并且它只能告诉您它已经看到的值 - 您还没有的其他代码路径't 试过可以设置其他类型.

您还可以使用包含有关参数和返回值类型的信息的 Epydoc 或 Sphinx 样式的文档字符串来告诉"PyCharm.PyCharm 将使用这些来改进它的检查.

从 Python 3 开始,Python 还获得了对函数注释的支持.这些可用于类型提示,如 PEP 484.有关更多信息,请参阅 typing 模块.这是更正式的,所以它也可以用于像 mypy 这样的工具一个类型检查器,可以以编程方式检查这些类型的一致性,为 Python 提供 TypeScript 风格的可选静态类型.

Say I'm working with the 'requests' python library.

req = requests.get("http://google.com")

Now after this, if I type req., I'm supposed to get a list of all methods I can access. But for some reason I don't, even if I manually press ctrl-space.

If I try this in ipython, I get autocomplete recommendations. Even if I try it via the built in python console in pycharm, I get recommendations.

Why's this happening?

解决方案

As Python is a dynamically typed language, you need to ensure it can work out what type things are, and inspect on the libraries on your system correctly. Try to make sure it's obvious what type the object is in your code.

One good way as of PyCharm 2.7 (back when versions were numbers) is to enable runtime type detection - PyCharm hooks into your program while it runs (while debugging), and checks the types of variables as they are used.

You can enable this by going to settings, going to the "Build, Execution, Deployment" section and then the "Python Debugger" subsection and enabling "Collect run-time types information for code insight".

Obviously it is worth noting that this isn't perfect - if you make changes, this won't be updated til the code is executed, and it can only tell you about values it has seen - other code paths you haven't tried could set other types.

You can also 'tell' PyCharm by using Epydoc or Sphinx style docstrings that contain information about parameter and return value types. PyCharm will use these to improve it's inspections.

Python also gained support for function annotations as of Python 3. These can be used for type hints as per PEP 484. See the typing module for more. This is more formal, so it can also be used for tools like mypy which a type checker that can programmatically check these types for consistency, giving Python a TypeScript-style optional static typing.

这篇关于Pycharm:代码完成不提供建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆