Table of contents
Ruff is a Python linter built with Rust, and is extremely fast in comparison with other linters(10-100x faster) in the Python ecosystem, according to its documentation.
One of the things that make Ruff different from the alternative tools, it is faster and has more functionality behind a single interface.
What is a linter?
According to this page in Wikipedia, a linter is a tool that analyzes the source code to flag programming errors, bugs, stylistic errors and suspicious constructs.
So, when we are writing a program, we can use a linter to check if there is an error or a bug. Also, when we are contributing to a code base, we use a linter to check if we are writing the code with stylistic errors.
This tool allows us to write idiomatic code, more consistent with the code base and with fewer errors and bugs.
Requirements
- Pip installed
Installation
pip install ruff
Usage
The examples are very simple, we are going to write a function in a Python file, import a module without using it, and run Ruff to see its output in the console.
import os
def say_hello():
print('Hello')
say_hello()
Now, in the console, we run the command ruff check <python file>
If we use the command ruff --fix <python file>
, ruff fixes the problem for us.
Next, we declare a variable, without using it. And use an undeclared variable.
def say_hello():
message = "hello"
print(messag)
If there are no problems with the code, then, ruff will not show any message.
Conclusion
I had the opportunity to try Ruff in an open-source project that I'm contributing to, and it was my first time using a linter. The project uses it before the code is pushed to a branch. But now, every time I make a modification or add a piece of code I run Ruff, is so fast and allows me to catch any error or bug before I run the program.
Thank you for taking the time to read this article.
If you have any recommendations about other packages, architectures, how to improve my code, my English, or anything; please leave a comment or contact me through Twitter, or LinkedIn.