A tool for autoadding simple type annotations.
Project description
When I refactor code I often find myself tediously adding type annotations that are obvious from context: functions that don't return anything, boolean flags, etcetera. That's where autotyping comes in: it automatically adds those types and inserts the right annotations.
It is built as a LibCST codemod; see the LibCST documentation for more information on how to use codemods.
Here's how to use it:
pip install autotyping- Make sure you have a
.libcst.codemod.yamlwith'autotyper'in themoduleslist. For an example, see the.libcst.codemod.yamlin this repo. - Run
python -m libcst.tool codemod autotyping.AutotypeCommand /path/to/my/code
By default it does nothing; you have to add flags to make it do more transformations. The following are supported:
- Annotating return types:
--none-return: add a-> Nonereturn type to functions without any return, yield, or raise in their body--scalar-return: add a return annotation to functions that only return literal bool, str, bytes, int, or float objects.
- Annotationg parameter types:
--bool-param: add a: boolannotation to any function parameter with a default ofTrueorFalse--int-param,--float-param,--str-param,--bytes-param: add an annotation to any parameter for which the default is a literal int, float, str, or bytes object--annotate-optional foo:bar.Baz: for any parameter of the formfoo=None, addBaz, imported frombar, as the type. For example, use--annotate-optional uid:my_types.Uidto annotate anyuidin your codebase with aNonedefault asOptional[my_types.Uid].--annotate-named-param foo:bar.Baz: annotate any parameter with no default that is namedfoowithbar.Baz. For example, use--annotate-named-param uid:my_types.Uidto annotate anyuidparameter in your codebase with no default asmy_types.Uid.
- Annotating magical methods:
--annotate-magics: add type annotation to certain magic methods. Currently this does the following:__str__returnsstr__repr__returnsstr__len__returnsint__init__returnsNone__del__returnsNone__bool__returnsbool__bytes__returnsbytes__format__returnsstr__contains__returnsbool__complex__returnscomplex__int__returnsint__float__returnsfloat__index__returnsint__exit__: the three parameters areOptional[Type[BaseException]],Optional[BaseException], andOptional[TracebackType]__aexit__: same as__exit__
--annotate-imprecise-magics: add imprecise type annotations for some additional magic methods. Currently this addstyping.Iteratorreturn annotations to__iter__,__await__, and__reversed__. These annotations should have a generic parameter to indicate what you're iterating over, but that's too hard for autotyping to figure out.
Things to add:
- Infer
-> boolas the return type if all return statements are boolean expressions like==.
Changelog
21.12.0 (December 21, 2021)
- Initial PyPI release
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file autotyping-21.12.0.tar.gz.
File metadata
- Download URL: autotyping-21.12.0.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.8.3 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b862a0597fbda0f7a17978d17eae4751d1581a7ec04bce18224172a7ee86018
|
|
| MD5 |
70b431fb96db02f7da52c1d5bac3e11b
|
|
| BLAKE2b-256 |
f60d617e3561c376a23fa095163544b3b3687873076af77921a3f3e2da22ba7a
|
File details
Details for the file autotyping-21.12.0-py3-none-any.whl.
File metadata
- Download URL: autotyping-21.12.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.8.3 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.6.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
789ae2b74897aabfb138b705169b4bb19ab0dfc8c66a5b5fc22dcecca714dfae
|
|
| MD5 |
dc0a8b071d3d7b0f7ab7e67caec10879
|
|
| BLAKE2b-256 |
2b94497fda81d4b546dcee060bd151a6089facd7079cfe77fd0a27a75e24eef2
|