ANN: make_stub_files 0.1

Edward K. Ream edreamleo at gmail.com
Thu Mar 31 07:31:54 EDT 2016


m
​ake_stub_files.py ​
0.1 is now available here <https://github.com/edreamleo/make-stub-files>.

This script eliminates much of the drudgery of creating python stub (.pyi)
<https://www.python.org/dev/peps/pep-0484/#stub-files> from python source
files. This script should encourage more people to use mypy. Stub files can
be used by people who use Python 2.x code bases.

This script makes a stub (.pyi) file in the output directory for each
source file listed on the command line (wildcard file names are supported).
This script never creates directories automatically, nor does it overwrite
stub files unless the --overwrite command-line option is in effect.

GvR says,

We actually do have a stub generator
<https://github.com/JukkaL/mypy/blob/master/mypy/stubgen.py> as part of
mypy now (it has a few options) but yours has the advantage of providing a
way to tune the generated signatures...This allows for a nice iterative way
of developing stubs.

The script does no type inference. Instead, the user supplies *patterns* in
a configuration file. The script matches these patterns to:

   1.

   The names of arguments in functions and methods and
   2.

   The text of *return expressions*. Return expressions are the actual text
   of whatever follows the "return" keyword. The script removes all comments
   in return expressions and converts all strings to "str". This preprocessing
   greatly simplifies pattern matching.

For example, given the method:

def foo(self, i, s):
    if i:
        return "abc" # a comment
    else:
        return s

and the patterns:

i: int
s: str

the script produces the stub:

def foo(i: int, s: str) --> str: ...

Edward K. Ream
March 31, 2016
------------------------------------------------------------------------------------------
Edward K. Ream: edreamleo at gmail.com Leo: http://leoeditor.com/
------------------------------------------------------------------------------------------


More information about the Python-announce-list mailing list