简体   繁体   English

Python 的 VS Code 函数签名 IntelliSense 来自哪里?

[英]Where does VS Code function signature IntelliSense come from for Python?

If I import the requests Python library (for example) and hover over requests.get I see this:如果我导入requests Python 库(例如)并将鼠标悬停在requests.get上,我会看到:

在此处输入图像描述

See how the function signature parameters and types are described in detail?看看函数签名参数和类型是如何详细描述的? Now, if I Ctrl + Click on the get method, I am taken to my local <python-dir>/site-packages/requests/api.py file where the function is exported.现在,如果我Ctrl +单击get方法,我将转到导出函数的本地<python-dir>/site-packages/requests/api.py文件。 If I hover over the get function there I see the following much less descriptive function signature:如果我将鼠标悬停在get函数上,我会看到以下描述性较差的函数签名:

在此处输入图像描述

I also don't see any type annotations there either, so the question is, where is all that other type information for the function signature coming from when I hover over it in my own script, and how do I add similar information for my own code?我在那里也看不到任何类型注释,所以问题是,当我在自己的脚本中将鼠标悬停在函数签名上时,函数签名的所有其他类型信息来自哪里,以及如何为自己添加类似信息代码?

This is clearly explained in type stub files .这在类型存根文件中有清楚的解释。

Why does Pyright not attempt (by default) to determine types from imported python sources?为什么 Pyright 不尝试(默认情况下)从导入的 python 源中确定类型? There are several reasons.有几个原因。

  1. Imported libraries can be quite large, so analyzing them can require significant time and computation.导入的库可能非常大,因此分析它们可能需要大量时间和计算。
  2. Some libraries are thin shims on top of native (C++) libraries.一些库是原生 (C++) 库之上的薄垫片。 Little or no type information would be inferable in these cases.在这些情况下,很少或没有类型信息是可推断的。
  3. Some libraries override Python's default loader logic.一些库会覆盖 Python 的默认加载器逻辑。 Static analysis is not possible in these cases.在这些情况下,无法进行静态分析。
  4. Type information inferred from source files is often of low value because many types cannot be inferred correctly.从源文件推断的类型信息通常价值很低,因为许多类型无法正确推断。 Even if concrete types can be inferred, generic type definitions cannot.即使可以推断出具体类型,泛型类型定义也不能。
  5. Type analysis would expose all symbols from an imported module, even those that are not meant to be exposed by the author.类型分析将公开导入模块中的所有符号,即使是那些不打算由作者公开的符号。 Unlike many other languages, Python offers no way of differentiating between a symbol that is meant to be exported and one that isn't.与许多其他语言不同,Python 无法区分要导出的符号和不应该导出的符号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM