Code formatting guidelines set a standard for the layout of your code, for stylistic elements such as use of line breaks and whitespace. Format doesn't have any impact on how the code works, but using a consistent style makes your code more readable, and makes it more likely to be reused.
Linting tools check your code for errors such as a missing bracket or line indent. This can save time and frustration because you can catch errors in advance of running the code.
As a project grows and goes through various stages of development it becomes important to maintain code quality. Using a consistent format and linting your code ensures that it is consistent, readable, and easy to debug and maintain.
There are a variety of Python tools available to use with your Kedro projects. This guide shows you how to use ruff
.
ruff
is a fast linter and formatter that replaces flake8
, pylint
, pyupgrade
, isort
, black
and more.
- It helps to make your code compliant to PEP 8.
- It reformats code and sorts imports alphabetically and automatically separating them into sections by type.
Install ruff
by adding the following lines to your project's requirements.txt
file:
ruff # Used for linting, formatting and sorting module imports
To install all the project-specific dependencies, including the linting tools, navigate to the root directory of the project and run:
pip install -r requirements.txt
Alternatively, you can individually install the linting tools using the following shell commands:
pip install ruff
ruff
read configurations from pyproject.toml
within your project root. You can enable different rule sets within the [tool.ruff]
section. For example, the rule set F
is equivalent to Pyflakes
.
To start with ruff
, we recommend adding this section to enable a few basic rules sets.
[tool.ruff]
select = [
"F", # Pyflakes
"E", # Pycodestyle
"W", # Pycodestyle
"UP", # pyupgrade
"I", # isort
"PL", # Pylint
]
It is a good practice to [split your line when it is too long](https://beta.ruff.rs/docs/rules/line-too-long/), so it can be read easily even in a small screen. `ruff` treats this slightly different from `black`, when using together we recommend to disable this rule, i.e. `E501` to avoid conflicts.
Use the following commands to run lint checks:
ruff format --check <project_root>
ruff check <project_root>
You can also have ruff format
automatically format your code by omitting the --check
flag.
You can automate the process of formatting and linting with pre-commit
hooks.
These hooks are run before committing your code to your repositories to automatically point out formatting issues,
making code reviews easier and less time-consuming.
You can install pre-commit
along with other dependencies by including it in the requirements.txt
file of your
Kedro project by adding the following line:
pre-commit
You can also install pre-commit
using the following command:
pip install pre-commit
Create a file named .pre-commit-config.yaml
in your Kedro project root directory. You can add entries for the hooks
you want to run before each commit
.
Below is a sample YAML
file with entries for ruff
:
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: '' # Replace with latest version, for example 'v0.1.8'
hooks:
- id: ruff
args: [--fix]
- id: ruff-format
See GitHub for the latest configuration for ruff's pre-commit.
Run the following command to complete installation:
pre-commit install
This enables pre-commit
hooks to run automatically every time you execute git commit
.