Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create .pyi files automatically?

How can I automatically create the boilerplate code of pyi files?

I want to create a pyi file for type hinting as described in pep484 which contains all method names.

I don't want magic. I want to add the type information after auto creating the file.

I want to avoid the copy+paste work.

Goal: Type hinting in PyCharm for Python2.

like image 538
guettli Avatar asked Feb 24 '16 12:02

guettli


People also ask

How do I create a stub file?

Create a stub file for your own implementationSelect File | New from the main menu, then select Python File (alternatively, use the Alt+Insert shortcut). In the New Python file dialog, select Python stub and specify the filename. The filename should be the same as the name of the implementation file.

What is a .PYI file?

pyi Python Interface file is another way to describe the contents of a module that Wing has trouble analyzing. This file is simply a Python skeleton with the appropriate structure, call signature, and return values to match the functions, attributes, classes, and methods defined in a module.

What is type stub 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.

What is a Python stub file?

What is a stub file? It is a file with the . pyi extension, which describes the Python type information, both input and return for functions and omits the logical part.


1 Answers

As far as I am concerned, there is no such direct tool in PyCharm. There are, however, 3rd party tools for this.


.pyi generators

MyPy

Yes, I guess anyone who wants to use compile-time type checking in Python, probably ends up using MyPy. MyPy contains stubgen.py tool which generates .pyi files.

Usage

mkdir out stubgen urllib.parse 

generates out/urllib/parse.pyi.

You can use it wit Python2 too:

stubgen --py2 textwrap 

And for C modules:

scripts/stubgen --docpath <DIR>/Python-3.4.2/Doc/library curses 

If you want to specify the path to your custom package, you can use --search-path option:

stubgen my-pkg --search-path=folder/path/to/the/package 

make-stub-files

This project is dedicated to exactly this goal.

Usage

A very basic one (but it has a handful of options, just consult README.md or make_stub_files -h

make_stub_files foo.py 

pre-written .pyi files

So you don't have to write your own.

Typeshed

Yes, if you're using .pyi files in your own project, you probably want to use this also when using external code. Typeshed contains .pyi files for Python2 and Python3 stdlib and a bunch of Python2 libraries (like redis, crypto, ...) and some Python3 libraries (like werkzeug or requests), all nicely versioned.


PyCharm alternatives to .pyi files

In case you're lazy, or simply just working on a project which doesn't require .pyi files and you don't want to be bothered by using 3rd party tools, PyCharm allows you to use:

  • Type docstrings (legacy)
  • Python 3 function annotations
like image 135
sjaustirni Avatar answered Sep 17 '22 13:09

sjaustirni