Pycharm:代码完成没有给出建议 [英] Pycharm: Code completion not giving recommendations

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

问题描述

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

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

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

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

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.

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

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.

为什么会这样?

推荐答案

由于 Python 是一种动态类型语言,因此您需要确保它能够确定事物的类型,并正确检查系统上的库.尽量确保在你的代码中对象的类型很明显.

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.

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

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.

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

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".

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

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.

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

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 3 开始,Python 还获得了对函数注释的支持.根据 PEP 484.请参阅 typing 模块了解更多信息.这是更正式的,所以它也可以用于像 mypy 这样的工具一个类型检查器,可以以编程方式检查这些类型的一致性,为 Python 提供 TypeScript 样式的可选静态类型.

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天全站免登陆