A tool for autoadding simple type annotations.
Slowing down. Last release over 2 years ago, little activity since. Fine for existing code; think twice for a new project.
pip install autotypingWhen 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.
Usage
Autotyping can be called directly from the CLI, be used as a pre-commit hook or run via the libcst interface as a codemod.
Here's how to use it from the CLI:
pip install autotypingpython -m autotyping /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.
- Annotating 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.--guess-common-names: infer certain parameter types from their names based on common patterns in open-source Python code. For example, infer that averboseparameter is of typebool.