Re-apply type annotations from .pyi stubs to your codebase.
MIT License
!!! Note this project is no longer supported/maintained as of 2022 August 14th !!!
Re-apply type annotations from .pyi stubs to your codebase.
Usage: retype [OPTIONS] [SRC]...
Re-apply type annotations from .pyi stubs to your codebase.
Options:
-p, --pyi-dir DIRECTORY Where to find .pyi stubs. [default: types]
-t, --target-dir DIRECTORY Where to write annotated sources. [default:
typed-src]
-i, --incremental Allow for missing type annotations in both stubs
and the source.
-q, --quiet Don't emit warnings, just errors.
-a, --replace-any Allow replacing Any annotations.
--hg Post-process files to preserve implicit byte
literals.
--traceback Show a Python traceback on error.
--version Show the version and exit.
--help Show this message and exit.
When you run retype
, it goes through all files you passed as SRC, finds the
corresponding .pyi files in the types/
directory, and re-applies typing annotations
from .pyi to the sources, using the Python 3 function and variable annotation syntax.
The resulting combined sources are saved in typed-src/
.
You can also pass directories as sources, in which case retype
will look for .py files
in them recursively.
It's smart enough to do the following:
print
__init__()
methods are moved verbatim to the__init__()
method in the implementation. They are never translated into__init__()
retype
will notretype
will create an value-lessassert isinstance()
checks.mypy
will complain that a name@asyncio.coroutine
.Just run:
tox
Relax, you can run retype as a tool perfectly fine under Python 3.6+ even if you want to analyze Python 2 code. This way you'll be able to parse all of the new syntax supported on Python 3 but also effectively all the Python 2 syntax at the same time.
By making the code exclusively Python 3.6+, I'm able to focus on the quality of the checks and re-use all the nice features of the new releases (check out pathlib or f-strings) instead of wasting cycles on Unicode compatibility, etc.
Note: to retype modules using f-strings you need to run on Python 3.6.2+ due to bpo-23894.
MIT
python -m retype
.gitignore
on merge of foldersast3.num
setup.cfg
based packaging configurationpyproject.toml
support --replace-any to allow replacing pre-existing Any
annotations without
raising errors
bugfix: don't re-apply # type: ignore
as an annotation if followed by another
comment. Original patch by Shannon Zhu.
bugfix: don't try to re-apply # type: ignore
as a function annotation
bugfix: support arbitrary source file encodings, patch by Michael Overmeyer.
bugfix: support missing newlines at the end of the file, patch by Michael Overmeyer.
bugfix: in --incremental, format default values according to PEP 8 (no spaces around the = sign if the type is missing)
support async functions
support --traceback for getting more information about internal errors
first published version
date-versioned
Glued together by Łukasz Langa. Multiple improvements by Michael Overmeyer and Bernat Gabor.