Pyrefly —— Python 的快速类型检查器和 IDE
Pyrefly 是一款快速的 Python 类型检查器,计划在 2025 年底取代 Meta 现有的 Pyre 类型检查器。
Pyrefly 旨在通过 IDE 功能和检查 Python 代码来提高开发速度。
主要特点:
- 类型推断:除了函数参数之外,Pyrefly 可以在大多数位置推断类型。它可以推断变量的类型和返回类型。
- Flow Types:Pyrefly 可以理解程序的控制流以细化静态类型。
- 增量性:Pyrefly 旨在实现模块级的大规模增量性,并优化检查和并行性。
示例:
# Example: Basic Type Checking
def greet(name: str) -> str:
return "Hello, " + name
# This works fine since both "World" is a string and greet expects a string
message: str = greet("World")
# Pyrefly catches this error before runtime due to a type misatch between 42 and "str"
# Error: Argument of type 'int' is not assignable to parameter of type 'str'
error_message: str = greet(42)