What is .pyi file
Kexin Tang

Sometimes in VSCode, we want to “jump to declaration”, then IDE redirect us to a file with suffix .pyi, and all it contains are some strange code like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Object:
def __init__(self, name: str, age: _T) -> None:
...

def get_name(self) -> str:
...

def get_age(self) -> _T:
...

def __lt__(self, other: Object) -> bool:
...

def __add__(self, v: _T) -> _T:
...

It looks like .h file in C++, only declares the function signature, typedef, etc without any implementation.

Actually, a .pyi file is not required but recommended, the only purpose for .pyi file is: IDE can provide auto complete and programmer can read the APIs fast and don’t need to care about the implementation details.