Tired of Python code analyzers slower than a snail on Xanax? Well, say hello to pylyzer, the new star of static analysis! 🚀
This little thing is written in Rust, which allows it to be on average 100 times faster than its competitors like pytype or pyright. Besides speed, pylyzer stands out for its ultra-detailed analysis. We’re not just talking basic type checking here; pylyzer can detect out-of-bounds list accesses or accesses to non-existent keys in dictionaries.
In fact, contrary to what you might think, pylyzer isn’t just a simple type checker. It’s a true Swiss Army knife of static analysis! It also acts as a linter to help you keep your code clean and consistent.
However, let’s not kid ourselves, pylyzer has its limits. Firstly, it assumes your code is potentially statically typed, so forget about using exec, setattr, and the like. Also, the typing of the standard Python API isn’t complete, so you might see errors like “this API doesn’t exist,” be warned! Lastly, since pylyzer’s type checking is conservative in its approach, you might see quite a few false positives. If that annoys you, disable the diagnostics; we won’t hold it against you (though I still don’t recommend it)! 😅
Speaking of access, you won’t have any trouble accessing pylyzer’s error reports because unlike some, they are crystal clear! No more unreadable jargon, we’ll show you exactly where the error is and explain the problem without any hassle. And as for features, pylyzer doesn’t do things halfway. The LSP support is super rich, from completion to renaming, along with many other cool features. All this thanks to the adaptation of Erg’s language server. And if you want to enjoy it directly in VSCode, no problem! The extension is available on the Marketplace and voilà, it’s done! 😎
Now you might say, “OK, that’s all well and good, but what is pylyzer for compared to Ruff, for example?” Well, yes, Ruff is also a static analysis tool for Python written in Rust, but it focuses solely on linting while pylyzer does type checking and serves as a language server. In short, we’re not mixing apples and oranges!
If you want to know how it works in detail, know that pylyzer uses Erg’s type checker internally, in addition to its own type checking capabilities. It converts the Python AST into Erg AST before passing it to the type checker, then adapts the results for Python. Clever! 😏 However, Erg is not a language transpiled into Python as one might think; it can run directly on the target machine.
Despite these small flaws, pylyzer still handles a bunch of stuff: type checking on variables, operators, functions, methods, and classes, type inference, module and package resolution, collection types (list, dict, tuple), a good part of typing, generic types and variance, type guards, type narrowing (which can be done with is and isinstance, among others), type assertions (via typing.cast for example)… In short, it covers a lot! 🦿
How to use Pylyzer
- Installation:
There are multiple ways to install Pylyzer:
- Using pip:
pip install pylyzer
- Using cargo (Rust package manager):
cargo install pylyzer --locked
- Building from source:
git clone https://github.com/mtshiba/pylyzer.git
cargo install --path . --locked
- Basic Usage:
After installation, you can use Pylyzer from the command line to analyze Python files:
pylyzer path/to/your/python_file.py
- VSCode Extension:
For a more integrated experience, you can install the Pylyzer VSCode extension:
- From the VSCode Marketplace
- Or via command line:
code --install-extension pylyzer.pylyzer
- Features:
Pylyzer offers several key features[1][2]:
- Fast performance (claimed to be 100 times faster than pytype and pyright)
- Detailed static analysis, including type checking
- Detection of out-of-bounds list accesses and nonexistent dict key accesses
- Clear and readable error reports
- Rich Language Server Protocol (LSP) support for features like code completion and renaming
- Limitations:
Be aware of some limitations[1]:
- Pylyzer assumes potentially statically typed code and cannot check code using reflections (e.g.,
exec
,setattr
) - It may not have complete type declarations for all Python standard APIs
- The type checking can be conservative, potentially resulting in false positive errors
- Configuration:
Pylyzer doesn’t require extensive configuration, but you may want to adjust settings in your IDE or editor for optimal use.
- Integration with Development Workflow:
Consider integrating Pylyzer into your development workflow:
- Use it as part of your CI/CD pipeline
- Run it before committing code changes
- Configure your editor to use Pylyzer for real-time feedback
Remember that Pylyzer is still a relatively new tool and may have some bugs or limitations. It’s recommended to use it alongside other Python development tools for comprehensive code analysis and quality assurance[6][7].
So what are you waiting for to test this little gem?
See you soon on Tech To Geek.