Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CHG: switch to ruff format instead of black entirely #3435

Merged
merged 18 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ default_stages: [commit, manual]

repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.3
rev: v0.1.8
hooks:
- id: ruff
name: "ruff on kedro/, tests/ and docs/"
Expand Down Expand Up @@ -53,14 +53,6 @@ repos:
- types-toml
- attrs

- repo: https://github.com/asottile/blacken-docs
rev: v1.12.1
hooks:
- id: blacken-docs
additional_dependencies:
- black~=23.0
entry: blacken-docs --skip-errors

- repo: local
hooks:
- id: imports
Expand Down Expand Up @@ -98,11 +90,5 @@ repos:
exclude: ^features/steps/test_starter
pass_filenames: false
entry: make secret-scan
- id: bandit
name: "Bandit security check"
language: system
types: [file, python]
exclude: ^kedro/templates/|^tests/|^features/steps/test_starter
entry: bandit -ll

# Manual only
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@

## What are the main features of Kedro?

| Feature | What is this? |
| -------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Project Template | A standard, modifiable and easy-to-use project template based on [Cookiecutter Data Science](https://github.com/drivendata/cookiecutter-data-science/). |
| Data Catalog | A series of lightweight data connectors used to save and load data across many different file formats and file systems, including local and network file systems, cloud object stores, and HDFS. The Data Catalog also includes data and model versioning for file-based systems. |
| Pipeline Abstraction | Automatic resolution of dependencies between pure Python functions and data pipeline visualisation using [Kedro-Viz](https://github.com/kedro-org/kedro-viz). |
| Coding Standards | Test-driven development using [`pytest`](https://github.com/pytest-dev/pytest), produce well-documented code using [Sphinx](http://www.sphinx-doc.org/en/master/), create linted code with support for [`flake8`](https://github.com/PyCQA/flake8), [`isort`](https://github.com/PyCQA/isort) and [`black`](https://github.com/psf/black) and make use of the standard Python logging library. |
| Flexible Deployment | Deployment strategies that include single or distributed-machine deployment as well as additional support for deploying on Argo, Prefect, Kubeflow, AWS Batch and Databricks. |
| Feature | What is this? |
| -------------------- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Project Template | A standard, modifiable and easy-to-use project template based on [Cookiecutter Data Science](https://github.com/drivendata/cookiecutter-data-science/). |

Check warning on line 44 in README.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Kedro.words] Use '' instead of 'easy'. Raw Output: {"message": "[Kedro.words] Use '' instead of 'easy'.", "location": {"path": "README.md", "range": {"start": {"line": 44, "column": 53}}}, "severity": "WARNING"}
| Data Catalog | A series of lightweight data connectors used to save and load data across many different file formats and file systems, including local and network file systems, cloud object stores, and HDFS. The Data Catalog also includes data and model versioning for file-based systems. |

Check warning on line 45 in README.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Kedro.weaselwords] 'many' is a weasel word! Raw Output: {"message": "[Kedro.weaselwords] 'many' is a weasel word!", "location": {"path": "README.md", "range": {"start": {"line": 45, "column": 100}}}, "severity": "WARNING"}
| Pipeline Abstraction | Automatic resolution of dependencies between pure Python functions and data pipeline visualisation using [Kedro-Viz](https://github.com/kedro-org/kedro-viz). |
| Coding Standards | Test-driven development using [`pytest`](https://github.com/pytest-dev/pytest), produce well-documented code using [Sphinx](http://www.sphinx-doc.org/en/master/), create linted code with support for [`ruff`](https://github.com/astral-sh/ruff) and make use of the standard Python logging library. |
| Flexible Deployment | Deployment strategies that include single or distributed-machine deployment as well as additional support for deploying on Argo, Prefect, Kubeflow, AWS Batch and Databricks. |

## How do I use Kedro?

Expand Down
1 change: 1 addition & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Removed example pipeline requirements when examples are not selected in `tools`.
* Allowed modern versions of JupyterLab and Jupyter Notebooks.
* Removed setuptools dependency
* Removed black dependency
sbrugman marked this conversation as resolved.
Show resolved Hide resolved

## Breaking changes to the API
* Added logging about not using async mode in `SequentiallRunner` and `ParallelRunner`.
Expand Down
2 changes: 1 addition & 1 deletion docs/source/deployment/prefect.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def kedro_init(
context = session.load_context()
catalog = context.catalog
logger.info("Registering datasets...")
unregistered_ds = pipeline.datasets() - set(catalog.list()) # NOQA
unregistered_ds = pipeline.datasets() - set(catalog.list())
for ds_name in unregistered_ds:
catalog.add(ds_name, MemoryDataset())
return {"catalog": catalog, "sess_id": session.session_id}
Expand Down
56 changes: 33 additions & 23 deletions docs/source/development/linting.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,36 @@
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.

## Set up Python tools
There are a variety of Python tools available to use with your Kedro projects. This guide shows you how to use [`black`](https://github.com/psf/black) and [`ruff`](https://beta.ruff.rs).
- **`black`** is a [PEP 8](https://peps.python.org/pep-0008/) compliant opinionated Python code formatter. `black` can
check for styling inconsistencies and reformat your files in place.
[You can read more in the `black` documentation](https://black.readthedocs.io/en/stable/).
- **`ruff`** is a fast linter that replaces `flake8`, `pylint`, `pyupgrade`, `isort` and [more](https://beta.ruff.rs/docs/rules/).
- It helps to make your code compliant to [`pep8`](https://pypi.org/project/pep8/).
- It reformats code by sorting imports alphabetically and automatically separating them into sections by
type. [You can read more in the `isort` documentation](https://pycqa.github.io/isort/).

There are a variety of Python tools available to use with your Kedro projects. This guide shows you how to use [`ruff`](https://beta.ruff.rs).

**`ruff`** is a fast linter and formatter that replaces `flake8`, `pylint`, `pyupgrade`, `isort`, `black` and [more](https://beta.ruff.rs/docs/rules/).
- It helps to make your code compliant to [PEP 8](https://peps.python.org/pep-0008/).
- It reformats code and sorts imports alphabetically and automatically separating them into sections by

Check warning on line 17 in docs/source/development/linting.md

View workflow job for this annotation

GitHub Actions / runner / vale

[vale] reported by reviewdog 🐶 [Kedro.Spellings] Did you really mean 'reformats'? Raw Output: {"message": "[Kedro.Spellings] Did you really mean 'reformats'?", "location": {"path": "docs/source/development/linting.md", "range": {"start": {"line": 17, "column": 8}}}, "severity": "WARNING"}
type.

### Install the tools
Install `black` and `ruff` by adding the following lines to your project's `requirements.txt`
Install `ruff` by adding the following lines to your project's `requirements.txt`
file:
```text
black # Used for formatting code
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:

```bash
pip install -r requirements.txt
```

Alternatively, you can individually install the linting tools using the following shell commands:

```bash
pip install black ruff
pip install ruff
```

#### Configure `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.
Expand All @@ -50,20 +52,20 @@
"I", # isort
"PL", # Pylint
]
ignore = ["E501"] # Black take care off line-too-long
```

```{note}
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.
```

### Run the tools

Use the following commands to run lint checks:
```bash
black --check <project_root>
ruff format --check <project_root>
ruff check <project_root>
```
You can also have `black` automatically format your code by omitting the `--check` flag.
You can also have `ruff format` automatically format your code by omitting the `--check` flag.

## Automated formatting and linting with `pre-commit` hooks

Expand All @@ -72,36 +74,44 @@
making code reviews easier and less time-consuming.

### Install `pre-commit`

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:

```text
pre-commit
```
You can also install `pre-commit` using the following command:

```bash
pip install pre-commit
```

### Add `pre-commit` configuration file

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` and black`:
Below is a sample `YAML` file with entries for `ruff`:

```yaml
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.0.270
rev: '' # Replace with latest version, for example 'v0.1.8'
hooks:
- id: ruff

- repo: https://github.com/psf/black
rev: 22.8.0
hooks:
- id: black
language_version: python3.9
args: [--fix]
- id: ruff-format
```

See GitHub for [the latest configuration for ruff's pre-commit](https://github.com/astral-sh/ruff-pre-commit).

### Install git hook scripts

Run the following command to complete installation:

```bash
pre-commit install
```

This enables `pre-commit` hooks to run automatically every time you execute `git commit`.
2 changes: 1 addition & 1 deletion docs/source/get_started/new_project.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The command line interface then asks which tools you'd like to include in the pr

You can add one or more of the options, or follow the default and add none at all:

* Linting: A basic linting setup with Black and ruff
* Linting: A basic linting setup with ruff
* Testing: A basic testing setup with pytest
* Custom Logging: Additional logging options
* Documentation: Configuration for basic documentation built with Sphinx
Expand Down
6 changes: 3 additions & 3 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ Welcome to Kedro's documentation!
:target: https://linen-slack.kedro.org/
:alt: Kedro's Slack archive

.. image:: https://img.shields.io/badge/code%20style-black-black.svg
:target: https://github.com/psf/black
:alt: Code style is Black
.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
:target: https://github.com/astral-sh/ruff
:alt: Linted and Formatted with Ruff

.. image:: https://bestpractices.coreinfrastructure.org/projects/6711/badge
:target: https://bestpractices.coreinfrastructure.org/projects/6711
Expand Down
2 changes: 1 addition & 1 deletion docs/source/starters/new_project_tools.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ With these installed, you can then make use of the following commands to format

```bash
ruff format path/to/project/root
black path/to/project/root --check
ruff check path/to/project/root
```

Though it has no impact on how your code works, linting is important for code quality because improves consistency, readability, debugging, and maintainability. To learn more about linting your Kedro projects, check our [linting documentation](../development/linting.md).
Expand Down
3 changes: 1 addition & 2 deletions docs/source/tutorial/tutorial_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ The spaceflights project dependencies are stored in `requirements.txt`(you may f

```text
# code quality packages
black~=22.0
ipython>=7.31.1, <8.0; python_version < '3.8'
ipython~=8.10; python_version >= '3.8'
ruff~=0.0.290
ruff==0.1.8

# notebook tooling
jupyter~=1.0
Expand Down
6 changes: 3 additions & 3 deletions features/steps/sh_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def run(
"""
if isinstance(cmd, str) and split:
cmd = shlex.split(cmd)
result = subprocess.run(cmd, input="", capture_output=True, **kwargs) # noqa: PLW1510
result = subprocess.run(cmd, input="", capture_output=True, **kwargs) # noqa: PLW1510, S603
result.stdout = result.stdout.decode("utf-8")
result.stderr = result.stderr.decode("utf-8")
if print_output:
Expand All @@ -59,9 +59,9 @@ def check_run(cmd: list | str, print_output: bool = False) -> None:
split_cmd = cmd

if print_output:
subprocess.check_call(split_cmd)
subprocess.check_call(split_cmd) # noqa: S603
else:
subprocess.check_call(split_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
subprocess.check_call(split_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # noqa: S603


class ChildTerminatingPopen(subprocess.Popen):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
black~=22.0
ruff==0.1.8
ipython>=8.10
jupyterlab>=3.0
notebook
Expand Down
4 changes: 1 addition & 3 deletions kedro/framework/cli/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def _create_session(package_name: str, **kwargs):
return KedroSession.create(**kwargs)


# noqa: missing-function-docstring
@click.group(name="Kedro")
def catalog_cli(): # pragma: no cover
pass
Expand All @@ -29,7 +28,6 @@ def catalog():
"""Commands for working with catalog."""


# noqa: too-many-locals,protected-access
@catalog.command("list")
@env_option
@click.option(
Expand Down Expand Up @@ -166,7 +164,7 @@ def create_catalog(metadata: ProjectMetadata, pipeline_name, env):

catalog_datasets = {
ds_name
for ds_name in context.catalog._datasets.keys() # noqa: protected-access
for ds_name in context.catalog._datasets.keys()
if not ds_name.startswith("params:") and ds_name != "parameters"
}

Expand Down
3 changes: 1 addition & 2 deletions kedro/framework/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
_get_entry_points,
load_entry_points,
)
from kedro.framework.project import LOGGING # noqa # noqa: unused-import
from kedro.framework.project import LOGGING # noqa: F401
from kedro.framework.startup import _is_project, bootstrap_project

LOGO = rf"""
Expand Down Expand Up @@ -147,7 +147,6 @@ def global_groups(self) -> Sequence[click.MultiCommand]:

@property
def project_groups(self) -> Sequence[click.MultiCommand]:
# noqa: line-too-long
"""Property which loads all project command groups from the
project and the plugins, then combines them with the built-in ones.
Built-in commands can be overridden by plugins, which can be
Expand Down
1 change: 0 additions & 1 deletion kedro/framework/cli/hooks/manager.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""This module defines a dedicated hook manager for hooks that extends Kedro CLI behaviour."""
# noqa: global-statement,invalid-name
import logging

from pluggy import PluginManager
Expand Down
8 changes: 3 additions & 5 deletions kedro/framework/cli/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ def list_commands(self, ctx):
return ["setup", "notebook", "lab", "convert"]


# noqa: missing-function-docstring
@click.group(name="Kedro")
def jupyter_cli(): # pragma: no cover
pass
Expand All @@ -50,7 +49,7 @@ def jupyter():

@forward_command(jupyter, "setup", forward_help=True)
@click.pass_obj # this will pass the metadata as first argument
def setup(metadata: ProjectMetadata, args, **kwargs): # noqa: unused-argument
def setup(metadata: ProjectMetadata, args, **kwargs):
"""Initialise the Jupyter Kernel for a kedro project."""
_check_module_importable("ipykernel")
validate_settings()
Expand All @@ -68,7 +67,7 @@ def jupyter_notebook(
env,
args,
**kwargs,
): # noqa: unused-argument
):
"""Open Jupyter Notebook with project specific variables loaded."""
_check_module_importable("notebook")
validate_settings()
Expand All @@ -94,7 +93,7 @@ def jupyter_lab(
env,
args,
**kwargs,
): # noqa: unused-argument
):
"""Open Jupyter Lab with project specific variables loaded."""
_check_module_importable("jupyterlab")
validate_settings()
Expand Down Expand Up @@ -153,7 +152,6 @@ def _create_kernel(kernel_name: str, display_name: str) -> str:
"""
# These packages are required by jupyter lab and notebook, which we have already
# checked are importable, so we don't run _check_module_importable on them.
# noqa: import-outside-toplevel
from ipykernel.kernelspec import install

try:
Expand Down
6 changes: 2 additions & 4 deletions kedro/framework/cli/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,12 @@ def _assert_pkg_name_ok(pkg_name: str):
raise KedroCliError(message)


def _check_pipeline_name(ctx, param, value): # noqa: unused-argument
def _check_pipeline_name(ctx, param, value):
if value:
_assert_pkg_name_ok(value)
return value


# noqa: missing-function-docstring
@click.group(name="Kedro")
def pipeline_cli(): # pragma: no cover
pass
Expand Down Expand Up @@ -100,7 +99,7 @@ def pipeline():
@click.pass_obj # this will pass the metadata as first argument
def create_pipeline(
metadata: ProjectMetadata, name, template_path, skip_config, env, **kwargs
): # noqa: unused-argument
):
"""Create a new modular pipeline by providing a name."""
package_dir = metadata.source_dir / metadata.package_name
conf_source = settings.CONF_SOURCE
Expand Down Expand Up @@ -208,7 +207,6 @@ def _echo_deletion_warning(message: str, **paths: list[Path]):


def _create_pipeline(name: str, template_path: Path, output_dir: Path) -> Path:
# noqa: import-outside-toplevel
from cookiecutter.main import cookiecutter

cookie_context = {"pipeline_name": name, "kedro_version": kedro.__version__}
Expand Down
Loading
Loading