From e1f238fa3a302b728e9bfde6a1825e29e8f83715 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 14 Aug 2024 11:13:49 +0530 Subject: [PATCH] Stabilize support for Jupyter Notebooks --- crates/ruff/src/commands/check.rs | 3 +- crates/ruff/tests/lint.rs | 64 +------------------ ...ow_settings__display_default_settings.snap | 1 + crates/ruff_workspace/src/configuration.rs | 12 +--- crates/ruff_workspace/src/options.rs | 7 +- crates/ruff_workspace/src/settings.rs | 1 + docs/configuration.md | 56 ++-------------- docs/faq.md | 26 +------- 8 files changed, 17 insertions(+), 153 deletions(-) diff --git a/crates/ruff/src/commands/check.rs b/crates/ruff/src/commands/check.rs index ea9058d795e8a4..24bc3d94d9d125 100644 --- a/crates/ruff/src/commands/check.rs +++ b/crates/ruff/src/commands/check.rs @@ -268,8 +268,7 @@ mod test { // Run let diagnostics = check( - // Notebooks are not included by default - &[tempdir.path().to_path_buf(), notebook], + &[tempdir.path().to_path_buf()], &pyproject_config, &ConfigArguments::default(), flags::Cache::Disabled, diff --git a/crates/ruff/tests/lint.rs b/crates/ruff/tests/lint.rs index ff6a913ac48069..8541a2492ba77b 100644 --- a/crates/ruff/tests/lint.rs +++ b/crates/ruff/tests/lint.rs @@ -1806,7 +1806,7 @@ select = ["UP006"] } #[test] -fn checks_notebooks_in_preview_mode() -> anyhow::Result<()> { +fn checks_notebooks_in_stable() -> anyhow::Result<()> { let tempdir = TempDir::new()?; std::fs::write( tempdir.path().join("main.ipynb"), @@ -1853,7 +1853,6 @@ fn checks_notebooks_in_preview_mode() -> anyhow::Result<()> { .args(STDIN_BASE_OPTIONS) .arg("--select") .arg("F401") - .arg("--preview") .current_dir(&tempdir) , @r###" success: false @@ -1867,64 +1866,3 @@ fn checks_notebooks_in_preview_mode() -> anyhow::Result<()> { "###); Ok(()) } - -#[test] -fn ignores_notebooks_in_stable() -> anyhow::Result<()> { - let tempdir = TempDir::new()?; - std::fs::write( - tempdir.path().join("main.ipynb"), - r#" -{ - "cells": [ - { - "cell_type": "code", - "execution_count": null, - "id": "ad6f36d9-4b7d-4562-8d00-f15a0f1fbb6d", - "metadata": {}, - "outputs": [], - "source": [ - "import random" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, - "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.12.0" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} -"#, - )?; - - assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) - .args(STDIN_BASE_OPTIONS) - .arg("--select") - .arg("F401") - .current_dir(&tempdir) - , @r###" - success: true - exit_code: 0 - ----- stdout ----- - All checks passed! - - ----- stderr ----- - warning: No Python files found under the given path(s) - "###); - Ok(()) -} diff --git a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap index bbd2e822c4767a..58eab9cb4be6ff 100644 --- a/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap +++ b/crates/ruff/tests/snapshots/show_settings__display_default_settings.snap @@ -60,6 +60,7 @@ file_resolver.force_exclude = false file_resolver.include = [ "*.py", "*.pyi", + "*.ipynb", "**/pyproject.toml", ] file_resolver.extend_include = [] diff --git a/crates/ruff_workspace/src/configuration.rs b/crates/ruff_workspace/src/configuration.rs index 74c7a36806920e..a49bb9729c5054 100644 --- a/crates/ruff_workspace/src/configuration.rs +++ b/crates/ruff_workspace/src/configuration.rs @@ -230,15 +230,9 @@ impl Configuration { extend_exclude: FilePatternSet::try_from_iter(self.extend_exclude)?, extend_include: FilePatternSet::try_from_iter(self.extend_include)?, force_exclude: self.force_exclude.unwrap_or(false), - include: FilePatternSet::try_from_iter(self.include.unwrap_or_else(|| { - let mut include = INCLUDE.to_vec(); - - if global_preview.is_enabled() { - include.push(FilePattern::Builtin("*.ipynb")); - } - - include - }))?, + include: FilePatternSet::try_from_iter( + self.include.unwrap_or_else(|| INCLUDE.to_vec()), + )?, respect_gitignore: self.respect_gitignore.unwrap_or(true), project_root: project_root.to_path_buf(), }, diff --git a/crates/ruff_workspace/src/options.rs b/crates/ruff_workspace/src/options.rs index cda010de67d352..eb7e066f8c849e 100644 --- a/crates/ruff_workspace/src/options.rs +++ b/crates/ruff_workspace/src/options.rs @@ -241,13 +241,12 @@ pub struct Options { /// included here not for configuration but because we lint whether e.g. the /// `[project]` matches the schema. /// - /// If [preview](https://docs.astral.sh/ruff/preview/) is enabled, the default - /// includes notebook files (`.ipynb` extension). You can exclude them by adding - /// `*.ipynb` to [`extend-exclude`](#extend-exclude). + /// Starting from Ruff version 0.6.0, the default also includes notebook files (`.ipynb` + /// extension). /// /// For more information on the glob syntax, refer to the [`globset` documentation](https://docs.rs/globset/latest/globset/#syntax). #[option( - default = r#"["*.py", "*.pyi", "**/pyproject.toml"]"#, + default = r#"["*.py", "*.pyi", "*.ipynb", "**/pyproject.toml"]"#, value_type = "list[str]", example = r#" include = ["*.py"] diff --git a/crates/ruff_workspace/src/settings.rs b/crates/ruff_workspace/src/settings.rs index b10a84aaacdde7..aee85fb84f4694 100644 --- a/crates/ruff_workspace/src/settings.rs +++ b/crates/ruff_workspace/src/settings.rs @@ -137,6 +137,7 @@ pub(crate) static EXCLUDE: &[FilePattern] = &[ pub(crate) static INCLUDE: &[FilePattern] = &[ FilePattern::Builtin("*.py"), FilePattern::Builtin("*.pyi"), + FilePattern::Builtin("*.ipynb"), FilePattern::Builtin("**/pyproject.toml"), ]; diff --git a/docs/configuration.md b/docs/configuration.md index 8571c5e23af3e8..8aaeb2fbfaec1f 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -339,23 +339,9 @@ For example, `ruff check /path/to/excluded/file.py` will always lint `file.py`. ### Default inclusions -By default, Ruff will discover files matching `*.py`, `*.ipy`, or `pyproject.toml`. +By default, Ruff will discover files matching `*.py`, `*.pyi`, `*.ipynb`, or `pyproject.toml`. To lint or format files with additional file extensions, use the [`extend-include`](settings.md#extend-include) setting. - -=== "pyproject.toml" - - ```toml - [tool.ruff] - extend-include = ["*.ipynb"] - ``` - -=== "ruff.toml" - - ```toml - extend-include = ["*.ipynb"] - ``` - You can also change the default selection using the [`include`](settings.md#include) setting. @@ -378,30 +364,12 @@ You can also change the default selection using the [`include`](settings.md#incl ## Jupyter Notebook discovery -Ruff has built-in support for [Jupyter Notebooks](https://jupyter.org/). +Ruff has built-in support for linting and formatting [Jupyter Notebooks](https://jupyter.org/). !!! info - Notebooks are linted and formatted by default when using [preview mode](preview.md). - You can opt-out of notebook linting and formatting by adding `*.ipynb` to [`extend-exclude`](settings.md#extend-exclude). - -To opt in to linting and formatting Jupyter Notebook (`.ipynb`) files, add the `*.ipynb` pattern to -your [`extend-include`](settings.md#extend-include) setting, like so: - -=== "pyproject.toml" - - ```toml - [tool.ruff] - extend-include = ["*.ipynb"] - ``` - -=== "ruff.toml" - - ```toml - extend-include = ["*.ipynb"] - ``` - -This will prompt Ruff to discover Jupyter Notebook (`.ipynb`) files in any specified -directories, then lint and format them accordingly. + Notebooks are linted and formatted by default from Ruff version `0.6.0` onwards which marks + Jupyter Notebook support as stable. You can opt-out of linting and formatting notebooks by + adding `*.ipynb` to [`extend-exclude`](settings.md#extend-exclude). If you'd prefer to either only lint or only format Jupyter Notebook files, you can use the section specific `exclude` option to do so. For example, the following would only lint Jupyter @@ -410,9 +378,6 @@ Notebook files and not format them: === "pyproject.toml" ```toml - [tool.ruff] - extend-include = ["*.ipynb"] - [tool.ruff.format] exclude = ["*.ipynb"] ``` @@ -420,8 +385,6 @@ Notebook files and not format them: === "ruff.toml" ```toml - extend-include = ["*.ipynb"] - [format] exclude = ["*.ipynb"] ``` @@ -431,9 +394,6 @@ And, conversely, the following would only format Jupyter Notebook files and not === "pyproject.toml" ```toml - [tool.ruff] - extend-include = ["*.ipynb"] - [tool.ruff.lint] exclude = ["*.ipynb"] ``` @@ -441,16 +401,10 @@ And, conversely, the following would only format Jupyter Notebook files and not === "ruff.toml" ```toml - extend-include = ["*.ipynb"] - [lint] exclude = ["*.ipynb"] ``` -Alternatively, pass the notebook file(s) to `ruff` on the command-line directly. For example, -`ruff check /path/to/notebook.ipynb` will always lint `notebook.ipynb`. Similarly, -`ruff format /path/to/notebook.ipynb` will always format `notebook.ipynb`. - ## Command-line interface Some configuration options can be provided or overridden via dedicated flags on the command line. diff --git a/docs/faq.md b/docs/faq.md index 6a4153601ea801..a9e1c5ea9887c5 100644 --- a/docs/faq.md +++ b/docs/faq.md @@ -397,30 +397,8 @@ them. You can find the supported settings in the [API reference](settings.md#lin ## Does Ruff support Jupyter Notebooks? -Ruff has built-in support for linting [Jupyter Notebooks](https://jupyter.org/). - -To opt in to linting Jupyter Notebook (`.ipynb`) files, add the `*.ipynb` pattern to your -[`extend-include`](settings.md#extend-include) setting, like so: - -=== "pyproject.toml" - - ```toml - [tool.ruff] - extend-include = ["*.ipynb"] - ``` - -=== "ruff.toml" - - ```toml - extend-include = ["*.ipynb"] - ``` - -This will prompt Ruff to discover Jupyter Notebook (`.ipynb`) files in any specified -directories, then lint and format them accordingly. - -Alternatively, pass the notebook file(s) to `ruff` on the command-line directly. For example, -`ruff check /path/to/notebook.ipynb` will always lint `notebook.ipynb`. Similarly, -`ruff format /path/to/notebook.ipynb` will always format `notebook.ipynb`. +Ruff has built-in support for linting and formatting [Jupyter Notebooks](https://jupyter.org/). Refer to the +[Jupyter Notebook section](configuration.md#jupyter-notebook-discovery) for more details. Ruff also integrates with [nbQA](https://github.com/nbQA-dev/nbQA), a tool for running linters and code formatters over Jupyter Notebooks.