Python’s type checking ecosystem has long been dominated by mypy – the original type checker that pioneered gradual typing for Python. But mypy’s Python-based implementation has always struggled with performance on large codebases. Ty is Astral’s answer to this problem: a Python type checker and language server written entirely in Rust, designed to be 10 to 60 times faster than existing alternatives.
Astral, the company behind the incredibly popular Ruff linter, has applied the same Rust-powered performance philosophy to type checking. Ty builds on Astral’s deep experience with Python tooling in Rust, leveraging the same multi-threaded architecture and incremental computation patterns that made Ruff the industry standard for Python linting.
The numbers speak for themselves. On large monorepos with hundreds of thousands of lines of Python, Ty completes initial type checking in seconds rather than minutes. Incremental checks after file changes are effectively instant, making it feasible to run type checking on every file save in even the largest projects. For developers who have tolerated mypy’s multi-minute check times, Ty represents a transformative improvement in developer experience.
How Does Ty Achieve Such Dramatic Speed Improvements?
Ty’s performance advantage comes from a combination of architectural decisions and implementation choices, not from any single optimization.
graph LR
A[Python Source Files] --> B[Parallel File Discovery]
B --> C[AST Parsing in Rust]
C --> D[Salsa Incremental Engine]
D --> E{Cached Result?}
E -->|Yes| F[Reuse Cached Type Info]
E -->|No| G[Type Inference & Checking]
G --> H[Store in Incremental DB]
F --> I[Collect Diagnostics]
H --> I
I --> J[Emit Results]
The Salsa-based incremental computation engine is key to Ty’s performance. It tracks dependencies between files and type definitions at a granular level. When a file changes, only the computations that depend on the changed parts are re-executed, rather than re-checking the entire project. This is the same approach used by the Rust compiler’s incremental compilation system.
How Does Ty Compare to Other Python Type Checkers?
The Python type checking landscape has several established players, each with different performance characteristics and feature sets.
| Feature | Ty | mypy | pyright | pyre | pytype |
|---|---|---|---|---|---|
| Language | Rust | Python | TypeScript | OCaml | Python |
| Speed | 10-60x faster | Baseline | 5-10x faster than mypy | Moderate | Slow |
| LSP server | Built-in | Separate | Built-in | Separate | Separate |
| PEP 695 support | Yes | Partial | Partial | No | No |
| Plugin system | Planned | Extensive | Limited | Minimal | Minimal |
| Monorepo support | Native | Via config | Via config | Native | Basic |
| Configuration | pyproject.toml | mypy.ini, setup.cfg | pyproject.toml | Custom config | setup.cfg |
Ty’s clean-slate design in Rust allows it to adopt the latest Python type system features without the legacy compatibility constraints that slow down evolution in older tools. Its built-in LSP server eliminates the need for separate language server setup, providing a unified toolchain experience.
What Type System Features Does Ty Support?
Ty aims for comprehensive coverage of Python’s evolving type system, including both established and emerging features.
| Feature | Status | PEP Reference |
|---|---|---|
| Basic type annotations | Complete | PEP 484 |
| Generic types | Complete | PEP 484, PEP 695 |
| Union types (X | Y) | Complete | PEP 604 |
| Protocols (structural subtyping) | Complete | PEP 544 |
| TypedDict | Complete | PEP 589 |
| Literal types | Complete | PEP 586 |
| TypeVarTuple / ParamSpec | Complete | PEP 646, PEP 612 |
| Type narrowing | Complete | – |
| Overload decorator | Complete | PEP 484 |
| Self type | Complete | PEP 673 |
| TypeIs (type narrowing) | Complete | PEP 742 |
| TypedDict alternatives | Complete | PEP 728 |
| Python 3.14 type features | Planned | Latest PEPs |
Ty tracks the latest PEP developments closely, aiming to support new type features shortly after they are accepted. This rapid adoption cycle ensures that projects using the latest Python versions can benefit from modern type system features with full type checker support.
How Does Ty Integrate with Editor Workflows?
Ty’s built-in LSP server provides a seamless development experience across all major editors and IDEs.
| Editor/IDE | Integration Method | Key LSP Features |
|---|---|---|
| VS Code | LSP extension | Real-time diagnostics, completions |
| Neovim | built-in LSP client | Inline errors, hover, go-to-definition |
| JetBrains | LSP plugin | Full IDE integration |
| Helix | Built-in LSP | Automatic setup |
| Sublime Text | LSP plugin | Diagnostics, completions |
| Emacs | eglot/lsp-mode | Full LSP support |
The LSP server provides real-time diagnostics as you type, auto-completions that respect type constraints, go-to-definition and find references with type-aware navigation, hover information with inferred types, and inline error messages with suggested fixes.
FAQ
What is Ty? Ty is a Python type checker and language server written in Rust by Astral, designed to be 10-60x faster than existing tools like mypy and pyright. It provides real-time type checking with IDE integration.
How does Ty achieve its speed compared to mypy? Ty is written in Rust and uses a multi-threaded architecture with incremental compilation, AST caching, and parallel file analysis. It processes files independently using a Salsa-based incremental computation model.
Does Ty support Python type annotations fully? Ty aims for near-complete support of Python’s type system including generics, overloads, Protocols, TypedDicts, Literal types, and the latest PEP specifications. It tracks PEP 695 (type parameter syntax) and later standards.
Can Ty replace mypy in existing projects? Yes, Ty is designed as a drop-in replacement. It supports mypy-compatible configuration and can be integrated into existing CI/CD pipelines and editor workflows. Many projects can switch by simply changing their type checking command.
Does Ty include a language server protocol implementation? Yes, Ty includes a built-in LSP server that provides real-time type checking, auto-completions, go-to-definition, find references, hover information, and inline error diagnostics in any LSP-compatible editor.
Further Reading
- Ty GitHub Repository – Source code, issues, and documentation
- Ruff (Astral’s Python Linter) – Ty’s sibling project for blazing-fast linting
- Mypy Official Documentation – The original Python type checker for comparison
- Python Type Checking Guide – Official Python typing documentation
- Salsa Incremental Computation Framework – The Rust framework powering Ty’s incremental engine
無程式碼也能輕鬆打造專業LINE官方帳號!一鍵導入模板,讓AI助你行銷加分!