infer types
1.0.0
一个自动将类型注释添加到Python代码中的CLI工具。
使用该工具的主要情况是帮助您注释大型和旧代码库。它不会100%解决您的任务,但肯定会为您提供巨大的帮助,因为现实世界中的许多功能都具有很容易自动推断的非常简单的返回类型。
特征:
假设您有以下方法:
class Database :
def users_count ( self ):
return len ( self . users )由于len始终返回int , infer-types能够推断该方法的返回类型。因此,在运行工具后,代码将看起来像这样:
class Database :
def users_count ( self ) -> int :
return len ( self . users )python3 -m pip install infer-typespython3 -m infer_types ./example/该工具将添加可以复制并不在文件顶部的新导入语句。要修复它,请运行isort:
python3 -m isort ./example/推论类型工具使用新的花式语法用于Python 3.10中引入的类型注释。因此,代替Optional[str]会发出str | None 。如果您的代码应该在较旧版本的Python上运行,请在每个文件的开头添加from __future__ import annotations 。它将解决该问题,并使您的应用程序更快地启动您的应用程序。您也可以使用Isort做到这一点:
python3 -m isort --add-import ' from __future__ import annotations ' ./example/请参阅Awesome-Python类型,以获取更多工具,以帮助您注释代码。
None 。yield语句,则返回类型是typing.Iterator 。is_open函数返回bool ,因为它始于is_ 。您只能使用--only标志来运行特定的启发式。