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:
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)
python3 -m pip install infer-types
python3 -m infer_types ./example/
The tool will add new import statements that can be duplicated and are located not at the top of the file. To fix it, run isort:
python3 -m isort ./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. You can also do that with isort:
python3 -m isort --add-import 'from __future__ import annotations' ./example/
See awesome-python-typing for more tools to help you with annotating your code.
None
.yield
statement, the return type is typing.Iterator
.is_open
function is assumed to return bool
because it starts with is_
.You can run only a specific heuristic using the --only
flag.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4