If I import the requests
Python library (for example) and hover over requests.get
I see this:
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. If I hover over the get
function there I see the following much less descriptive function signature:
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?
CodePudding user response:
This is clearly explained in type stub files.
Why does Pyright not attempt (by default) to determine types from imported python sources? There are several reasons.
- Imported libraries can be quite large, so analyzing them can require significant time and computation.
- Some libraries are thin shims on top of native (C ) libraries. Little or no type information would be inferable in these cases.
- Some libraries override Python’s default loader logic. Static analysis is not possible in these cases.
- 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.
- 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.