Pyrefly
Pyrefly is a Python type checker and language server with built-in support for a number of Pydantic-specific features. This support works out-of-the-box with Pydantic and Pyrefly installed, with no additional configuration needed.
Pyrefly is available as both an IDE extension and a command-line type checker.
IDE extension¶
Pyrefly gives you IDE features such as go-to-definition and autocomplete on your Pydantic models.
Here’s an example of signature help, powered by Pyrefly’s understanding of the Pydantic-specific validation_alias
keyword:
As you type Model()
, Pyrefly hints that you need to use the name x_
to populate the x
field.
Type checker¶
Pyrefly can also catch errors in your code before you run it. Consider the following example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Since Model1
declares that it is frozen using a standard type system feature, any type checker of your choice will catch the validation error from mutating model1.x
. However, a type checker without special support for the Pydantic ConfigDict
class will miss the validation error from mutating model2.x
.
Pyrefly catches both errors:
ERROR Cannot set field `x` [read-only]
--> foo.py:11:1
|
11 | model1.x = 1 # validation error: mutating a frozen field
| ^^^^^^^^
|
This field belongs to a frozen Pydantic model
ERROR Cannot set field `x` [read-only]
--> foo.py:14:1
|
14 | model2.x = 1 # validation error: mutating a frozen field
| ^^^^^^^^
|
This field belongs to a frozen Pydantic model
See the Pyrefly documentation for more information.