CLI tool to automatically annotate Python code.
Project description
infer-types
A CLI tool to automatically add type annotations into Python code.
The main scenario for using the tool is to help you with annotating a big and old codebase. It won't solve the task for you 100% but will definitely help you tremendously, because many of the functions in the real world have quite simple return types that are easy to infer automatically.
Features:
- 100% automated, get a bunch of type annotations with no effort.
- Unix-way, does only one job and does it well.
- A lot of heuristics and smart inference.
- Actively uses typeshed to find annotations for unannotated dependencies.
Example
Let's say, you have the following method:
class Database:
def users_count(self):
return len(self.users)
Since len
always returns int
, infer-types
is able to infer the return type of the method. So, after running the tool, the code will look like this:
class Database:
def users_count(self) -> int:
return len(self.users)
Installation
python3 -m pip install infer-types retype
This will also install retype which we're going to use to apply generated type annotations back to the code (see Usage below).
Usage
First of all, run the tool:
python3 -m infer_types ./example/
It will infer types for all code in the example
directory and save stub files inside of types
directory.
The next thing you need to do is to apply the stub files back to the code. For that, we're going to use retype:
retype -it ./example/ ./example/
The infer-types tool uses the new fancy syntax for type annotations introduced in Python 3.10. So, instead of Optional[str]
it will emit str | None
. If your code is supposed to run on an older version of Python, add from __future__ import annotations
at the beginning of each file. It will solve the issue and also make startup of your app faster.
See awesome-python-typing for more tools to help you with annotating your code.
How it works
- Most of heuristics live in astypes package. Check it out learn more about the main inference logic.
- If the same method is defined in a base class, copy the type annotations from there.
- If there are no return statements returning a value, the return type is
None
. - If there is a
yield
statement, the return type istyping.Iterator
.
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
File details
Details for the file infer_types-0.2.0.tar.gz
.
File metadata
- Download URL: infer_types-0.2.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.25.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 676c48732a0bc7cacde7d8e328d7f7e1abc2ae6e7e89f7dccf2e993bf79ba2bb |
|
MD5 | 3ad0db0825f5ab1b09fe33c81e401b4f |
|
BLAKE2b-256 | 7905bc870d45a560e50a9bbf921f318fdce6bc395f3adc384f63329cb85cb47d |
File details
Details for the file infer_types-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: infer_types-0.2.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.25.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6242e69ee30c95bde3d5ed49732232355c021b09eacec65078cdcaa45c57ba4b |
|
MD5 | 82edb5b7c56689d7f605f2d9a5421e4f |
|
BLAKE2b-256 | d038429232bd0af596090d73e826d1e3c07c150a7c0248ee93d0ed136bb8a75c |