Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "i" represent in Python .pyi extension?

Tags:

python

People also ask

What is .PYI extension?

A PYI file is a code stub used to specify type hints for a Python module. Python type checkers can read PYI files and use them to warn developers of potential type mismatches. PYI files have the same syntax as regular Python modules; they are typically stored alongside the module for which they contain type hints.

What is stub file in Python?

Introduction. A type stub is a file with a .pyi extension that describe a module's types while omitting implementation details. For example, if a module foo has the following source code: class Foo: CONSTANT = 42 def do_foo(x): return x.

What is a stubbed file?

A stub file is a computer file that appears to the user to be on disk and immediately available for use, but is actually held either in part or entirely on a different storage medium.


I think the i in .pyi stands for "Interface"

Definition for Interface in Java:

An interface in the Java programming language is an abstract type that is used to specify a behaviour that classes must implement

  • From Python typeshed github repository:

Each Python module is represented by a .pyi "stub". This is a normal Python file (i.e., it can be interpreted by Python 3), except all the methods are empty.

  • In 'Mypy' repository, they explicitly mention "stub" files as public interfaces:

A stubs file only contains a description of the public interface of the module without any implementations.

Because "Interfaces" do not exist in Python (see this SO question between Abstract class and Interface) I think the designers intended to dedicate a special extension for it.

pyi implements "stub" file (definition from Martin Fowler)

Stubs: provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test.

But people are more familiar with Interfaces than "stub" files, therefore it was easier to choose .pyi rather than .pys to avoid unnecessary confusion.


Apparently PyCharm creates .pyi file for its own purposes:

The *.pyi files are used by PyCharm and other development tools to provide more information, such as PEP 484 type hints, than it is able to glean from introspection of extension types and methods. They are not intended to be imported, executed or used for any other purpose other than providing info to the tools. If you don't use use a tool that makes use of .pyi files then you can safely ignore this file.

See: https://www.python.org/dev/peps/pep-0484/ https://www.jetbrains.com/help/pycharm/2016.1/type-hinting-in-pycharm.html

This comment was found in: python27/Lib/site-packages/wx/core.pyi


The i in .pyi stands for ‘interface’.

The .pyi extension was first mentioned in this GitHub issue thread where JukkaL says:

I'd probably prefer an extension with just a single dot. It also needs to be something that is not in use (it should not be used by cython, etc.). .pys seems to be used in Windows (or was). Maybe .pyi, where i stands for an interface definition?