Search code examples
pythonpycharmpython-typing

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.


Solution

  • 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: