Skip to main content

Python stub file generator.

Project description

pygenstub is a utility for generating stub files from Python source files. It takes a source file as input and creates a stub file with the same base name and the .pyi extension.

When installed, a script named pygenstub gets generated which can be used as follows:

$ pygenstub foo.py

This command will generate the file foo.pyi.

This utility can be used with the Unix watch command or PyCharm file watchers to update stub files automatically from source files.

Features

At the moment, the utility only handles functions (not methods). If the docstring for the function includes a signature field, it uses this signature to generate a prototype for the function.

For example:

def foo(a, b):
    """Whatever.

    :signature: (int, str) -> None
    :param a: ...
    """

The generated prototype will be:

def foo(a: int, b: str) -> None: ...

Imported names in the source will be used in the stub if needed:

from x import A, B, C

def foo(a, b):
    """Whatever.

    :signature: (A, B) -> A
    :param a: ...
    """

will generate:

from x import A, B

def foo(a: A, b: B) -> A: ...

Dotted type names will generate imports in the stub file:

def foo(a, b):
    """Whatever.

    :signature: (x.A, x.B) -> x.A
    :param a: ...
    """

will generate:

import x

def foo(a: x.A, b: x.B) -> x.A: ...

It will also look up unknown names from the typing module:

def foo(a, b):
    """Whatever.

    :signature: (Dict, Tuple) -> Optional[str]
    :param a: ...
    """

will generate:

from typing import Dict, Optional, Tuple

def foo(a: Dict, b: Tuple) -> Optional[str]: ...

TODO

  • Support for methods.

  • Sphinx extension for adjusting documentation.

Disclaimer

Some of these (or maybe even all of them) are probably in the “not a good idea” category. The whole thing could be pointless. I’m experimenting at the moment. Anyway, if you’re not using .pyi files, it should be harmless.

History

1.0a3 (2017-01-06)

  • Proper support for names from the typing module in input parameters.

  • Added parameter default values to stubs.

1.0a2 (2017-01-03)

  • Support for Python 2.7.

1.0a1 (2017-01-03)

  • First release on PyPI.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygenstub-1.0a3.tar.gz (5.4 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page