All Projects → chadrik → doc484

chadrik / doc484

Licence: MIT license
Generate PEP 484 type annotations from docstrings

Programming Languages

python
139335 projects - #7 most used programming language
shell
77523 projects

Projects that are alternatives of or similar to doc484

flake8-mypy
A plugin for flake8 integrating Mypy.
Stars: ✭ 103 (+329.17%)
Mutual labels:  mypy
starlite
Light, Flexible and Extensible ASGI API framework
Stars: ✭ 1,525 (+6254.17%)
Mutual labels:  mypy
phantom-types
Phantom types for Python.
Stars: ✭ 120 (+400%)
Mutual labels:  mypy
pybind11-stubgen
Generates stubs for python modules (targeted to C++ extensions compiled with pybind11)
Stars: ✭ 103 (+329.17%)
Mutual labels:  mypy
mypy-check
github action for python's mypy type checker tool
Stars: ✭ 23 (-4.17%)
Mutual labels:  mypy
mypy-playground
The mypy playground. Try mypy with your web browser.
Stars: ✭ 58 (+141.67%)
Mutual labels:  mypy
Returns
Make your functions return something meaningful, typed, and safe!
Stars: ✭ 2,015 (+8295.83%)
Mutual labels:  mypy
python-lint
GitHub Action for Lint your code
Stars: ✭ 57 (+137.5%)
Mutual labels:  mypy
classes
Smart, pythonic, ad-hoc, typed polymorphism for Python
Stars: ✭ 461 (+1820.83%)
Mutual labels:  mypy
pandas-stubs
Pandas type stubs. Helps you type-check your code.
Stars: ✭ 84 (+250%)
Mutual labels:  mypy
BFSG
BFSG - BruteForce String Generator 😾
Stars: ✭ 16 (-33.33%)
Mutual labels:  mypy
rubric
Linter Config Initializer for Python
Stars: ✭ 21 (-12.5%)
Mutual labels:  mypy
aiorwlock
Read/Write Lock - synchronization primitive for asyncio
Stars: ✭ 90 (+275%)
Mutual labels:  mypy
mmpm
MagicMirror Package Manager
Stars: ✭ 104 (+333.33%)
Mutual labels:  mypy
vim-mypy
Vim plugin for executing Python's optional static type checker MyPy (http://mypy-lang.org/)
Stars: ✭ 89 (+270.83%)
Mutual labels:  mypy
pypackage
Cookiecutter python package using Poetry, mypy, black, isort, autoflake, pytest, mkdocs, and GitHub Actions
Stars: ✭ 12 (-50%)
Mutual labels:  mypy
docopt-ng
Humane command line arguments parser. Now with maintenance, typehints, and complete test coverage.
Stars: ✭ 94 (+291.67%)
Mutual labels:  mypy
linter-mypy
Atom Linter plugin to lint Python optional static type as defined in PEP 484
Stars: ✭ 29 (+20.83%)
Mutual labels:  mypy
Typer
Typer, build great CLIs. Easy to code. Based on Python type hints.
Stars: ✭ 6,793 (+28204.17%)
Mutual labels:  typehints

Doc 484

CI Status

Generate PEP 484 type annotations from docstrings

doc484 provides a script to find type declarations within your docstrings and convert them to PEP 484 type comments. It supports the three major docstring conventions numpy, google, and restructuredText (sphinx)

Regardless of docstring convention you choose, the types declared within your docstrings should following the guidelines in PEP 484, especially use of the typing module, where necessary.

Why Use This?

If you answer affirmative to at least 2 of these, this project is probably for you:

  • You're stuck supporting python 2.7, so you have to use type comments, instead of the type annotations supported in 3.5+
  • Your projects have existing docstrings with types that are already mostly correct
  • You find it easier to maintain and comprehend types specified alongside the description of an argument

Examples

Basic

Before

from typing import Optional

def maxlines(input, numlines=None):
    """
    Trim a string to a maximum number of lines.

    Parameters
    ----------
    input : str
    numlines : Optional[int]
        maximum number of lines

    Returns
    -------
    str
    """
    if numlines is None:
        return input
    return '\n'.join(input.split('\n')[:numlines])

After doc484

from typing import Optional

def maxlines(input, numlines=None):
    # type: (str, Optional[int]) -> str
    """
    Trim a string to a maximum number of lines.

    Parameters
    ----------
    input : str
    numlines : Optional[int]
        maximum number of lines

    Returns
    -------
    str
    """
    if numlines is None:
        return input
    return '\n'.join(input.split('\n')[:numlines])

The file is now properly inspectable by mypy or PyCharm.

Advanced

A more complex example demonstrates some of the added readability that comes from specifying types within your docstrings. Below we use numpy format to document a generator of tuples:

Before

from typing import *

def itercount(input, char):
    """
    Iterate over input strings and yield a tuple of the string with `char`
    removed, and the number of occurrences of `char`.

    Parameters
    ----------
    input : Iterable[str]
    char : str
        character to remove and count

    Yields
    ------
    stripped : str
        input string with all occurrences of `char` removed
    count : int
        number of occurrences of `char`
    """
    for x in input:
        yield x.strip(char), x.count(char)

After doc484

from typing import *

def itercount(input, char):
    # type: (Iterable[str], str) -> Iterator[Tuple[str, int]]
    """
    Iterate over input strings and yield a tuple of the string with `char`
    removed, and the number of occurrences of `char`.

    Parameters
    ----------
    input : Iterable[str]
    char : str
        character to remove and count

    Yields
    ------
    stripped : str
        input string with all occurrences of `char` removed
    count : int
        number of occurrences of `char`
    """
    for x in input:
        yield x.strip(char), x.count(char)

Installing

pip install doc484

Usage

doc484 -h

By default doc484 will not modify files, instead it will print out a diff of what would be modified. When you're ready to make changes, add the --write flag.

Check the scripts directory for an example of how to automatically run doc484 on modified files in your git or mercurial repository.

Configuration

You can override any of the command line options using an ini-style configuration file. By default, doc484 looks for a setup.cfg file in the current working directory, but you can also provide a config explicitly using the --config option.

For example, to override the number of processes to use when converting, and specify the docstring format for the project, add this to your setup.cfg and run doc484 from the directory where this config file resides:

[doc484]
processes = 12
format = numpy

TODO

  • automatically insert typing imports
  • add option to convert docstrings to function annotations (for python 3.5+)
  • finish support for fixing non-PEP484-compliant docstrings (e.g. list of str)
  • convert doctypes utility script to python?
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].