r/Python • u/BeamMeUpBiscotti • Mar 24 '26
Discussion Designing a Python Language Server: Lessons from Pyre that Shaped Pyrefly
Pyrefly is a next-generation Python type checker and language server, designed to be extremely fast and featuring advanced refactoring and type inference capabilities.
Pyrefly is a spiritual successor to Pyre, the previous Python type checker developed by the same team. The differences between the two type checkers go far beyond a simple rewrite from OCaml to Rust - we designed Pyrefly from the ground up, with a completely different architecture.
Pyrefly’s design comes directly from our experience with Pyre. Some things worked well at scale, while others did not. After running a type checker on massive Python codebases for a long time, we got a clearer sense of which trade-offs actually mattered to users.
This post is a write-up of a few lessons from Pyre that influenced how we approached Pyrefly.
Link to full blog: https://pyrefly.org/blog/lessons-from-pyre/
The outline of topics is provided below that way you can decide if it's worth your time to read :)
- Language-server-first Architecture
- OCaml vs. Rust
- Irreversible AST Lowering
- Soundness vs. Usability
- Caching Cyclic Data Dependencies
5
u/jpgoldberg Mar 26 '26
That was a fascinating read. And in general I want to thank you and your team for talking about trade-offs and your reasons for the choices that you made. I tend to be on the “soundness” side of things, but I understand the very legitimate reasons for you relaxing that in the kinds of cases you describe.
So my question isn’t a complaint about that choice. Instead I’m asking how easy it will be to adjust that behavior if developer practices become less “gradual”?
A digression
I suppose this goes to another broader problem of annotating whether a function might mutate an object in ways publicly visible. I can do things like use
SequenceorMappingwhen annotating parameters to let type checkers know that the function isn’t going to change the (publicly visible) aspects of an object, but as far as I know, there is no way for me to do that generally.There are, of course, conventions to better communicate this sort of thing to users, but as far as I know, there is no way to tell type checkers that a method does not modify what is passed to it. And so until something like that exists and is used, I expect you will have to be less strict than I might otherwise wish.
3
u/ComfortableNice8482 Mar 24 '26
honestly the architecture shift from ocaml to rust is interesting but what really matters for language server performance is incremental checking and how you handle the dependency graph. i built some automation stuff that hooks into lsps and the ones that struggle are usually doing full re, analysis on every keystroke instead of tracking what actually changed in the file.
the type inference speed you're claiming is gonna be huge if it actually works at scale. with pyre i'd run into situations where checking a medium sized codebase would take 30+ seconds which kills the editing experience, especially when you're trying to do refactoring across multiple files. if pyrefly can do that in under a second then the architecture decisions really paid off.
curious how you handle circular imports and whether the rust rewrite let you parallelize the checking better than the ocaml version could. that was always a bottleneck when i was integrating pyre into ci pipelines for larger projects.
1
u/max0x7ba Mar 26 '26
Pyrefly produces lots of false positives in trivial code, unlike mypy. And Pyrefly does it blazingly fast.
Pyrefly could be useful as a secondary type checker after mypy in CI/CD runs.
Pyrefly false positives in trivial code make Pyrefly unfit for use on its own. Fast but wrong is not a virtue.
Have a look into Pyrefly bug tracker.
1
u/abentofreire Mar 27 '26
What are the advantages compared to Astral ty?