Skip to content

Commit 012198a

Browse files
authored
Enable notebooks by default in preview mode (#12621)
1 parent fbab04f commit 012198a

File tree

5 files changed

+142
-4
lines changed

5 files changed

+142
-4
lines changed

crates/ruff/tests/lint.rs

+124
Original file line numberDiff line numberDiff line change
@@ -1804,3 +1804,127 @@ select = ["UP006"]
18041804

18051805
Ok(())
18061806
}
1807+
1808+
#[test]
1809+
fn checks_notebooks_in_preview_mode() -> anyhow::Result<()> {
1810+
let tempdir = TempDir::new()?;
1811+
std::fs::write(
1812+
tempdir.path().join("main.ipynb"),
1813+
r#"
1814+
{
1815+
"cells": [
1816+
{
1817+
"cell_type": "code",
1818+
"execution_count": null,
1819+
"id": "ad6f36d9-4b7d-4562-8d00-f15a0f1fbb6d",
1820+
"metadata": {},
1821+
"outputs": [],
1822+
"source": [
1823+
"import random"
1824+
]
1825+
}
1826+
],
1827+
"metadata": {
1828+
"kernelspec": {
1829+
"display_name": "Python 3 (ipykernel)",
1830+
"language": "python",
1831+
"name": "python3"
1832+
},
1833+
"language_info": {
1834+
"codemirror_mode": {
1835+
"name": "ipython",
1836+
"version": 3
1837+
},
1838+
"file_extension": ".py",
1839+
"mimetype": "text/x-python",
1840+
"name": "python",
1841+
"nbconvert_exporter": "python",
1842+
"pygments_lexer": "ipython3",
1843+
"version": "3.12.0"
1844+
}
1845+
},
1846+
"nbformat": 4,
1847+
"nbformat_minor": 5
1848+
}
1849+
"#,
1850+
)?;
1851+
1852+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
1853+
.args(STDIN_BASE_OPTIONS)
1854+
.arg("--select")
1855+
.arg("F401")
1856+
.arg("--preview")
1857+
.current_dir(&tempdir)
1858+
, @r###"
1859+
success: false
1860+
exit_code: 1
1861+
----- stdout -----
1862+
main.ipynb:cell 1:1:8: F401 [*] `random` imported but unused
1863+
Found 1 error.
1864+
[*] 1 fixable with the `--fix` option.
1865+
1866+
----- stderr -----
1867+
"###);
1868+
Ok(())
1869+
}
1870+
1871+
#[test]
1872+
fn ignores_notebooks_in_stable() -> anyhow::Result<()> {
1873+
let tempdir = TempDir::new()?;
1874+
std::fs::write(
1875+
tempdir.path().join("main.ipynb"),
1876+
r#"
1877+
{
1878+
"cells": [
1879+
{
1880+
"cell_type": "code",
1881+
"execution_count": null,
1882+
"id": "ad6f36d9-4b7d-4562-8d00-f15a0f1fbb6d",
1883+
"metadata": {},
1884+
"outputs": [],
1885+
"source": [
1886+
"import random"
1887+
]
1888+
}
1889+
],
1890+
"metadata": {
1891+
"kernelspec": {
1892+
"display_name": "Python 3 (ipykernel)",
1893+
"language": "python",
1894+
"name": "python3"
1895+
},
1896+
"language_info": {
1897+
"codemirror_mode": {
1898+
"name": "ipython",
1899+
"version": 3
1900+
},
1901+
"file_extension": ".py",
1902+
"mimetype": "text/x-python",
1903+
"name": "python",
1904+
"nbconvert_exporter": "python",
1905+
"pygments_lexer": "ipython3",
1906+
"version": "3.12.0"
1907+
}
1908+
},
1909+
"nbformat": 4,
1910+
"nbformat_minor": 5
1911+
}
1912+
"#,
1913+
)?;
1914+
1915+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
1916+
.args(STDIN_BASE_OPTIONS)
1917+
.arg("--select")
1918+
.arg("F401")
1919+
.current_dir(&tempdir)
1920+
, @r###"
1921+
success: true
1922+
exit_code: 0
1923+
----- stdout -----
1924+
All checks passed!
1925+
1926+
----- stderr -----
1927+
warning: No Python files found under the given path(s)
1928+
"###);
1929+
Ok(())
1930+
}

crates/ruff_workspace/src/configuration.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,15 @@ impl Configuration {
230230
extend_exclude: FilePatternSet::try_from_iter(self.extend_exclude)?,
231231
extend_include: FilePatternSet::try_from_iter(self.extend_include)?,
232232
force_exclude: self.force_exclude.unwrap_or(false),
233-
include: FilePatternSet::try_from_iter(
234-
self.include.unwrap_or_else(|| INCLUDE.to_vec()),
235-
)?,
233+
include: FilePatternSet::try_from_iter(self.include.unwrap_or_else(|| {
234+
let mut include = INCLUDE.to_vec();
235+
236+
if global_preview.is_enabled() {
237+
include.push(FilePattern::Builtin("*.ipynb"));
238+
}
239+
240+
include
241+
}))?,
236242
respect_gitignore: self.respect_gitignore.unwrap_or(true),
237243
project_root: project_root.to_path_buf(),
238244
},

crates/ruff_workspace/src/options.rs

+4
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ pub struct Options {
241241
/// included here not for configuration but because we lint whether e.g. the
242242
/// `[project]` matches the schema.
243243
///
244+
/// If [preview](https://docs.astral.sh/ruff/preview/) is enabled, the default
245+
/// includes notebook files (`.ipynb` extension). You can exclude them by adding
246+
/// `*.ipynb` to [`extend-exclude`](#extend-exclude).
247+
///
244248
/// For more information on the glob syntax, refer to the [`globset` documentation](https://docs.rs/globset/latest/globset/#syntax).
245249
#[option(
246250
default = r#"["*.py", "*.pyi", "**/pyproject.toml"]"#,

docs/configuration.md

+4
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,10 @@ You can also change the default selection using the [`include`](settings.md#incl
380380

381381
Ruff has built-in support for [Jupyter Notebooks](https://jupyter.org/).
382382

383+
!!! info
384+
Notebooks are linted and formatted by default when using [preview mode](preview).
385+
You can opt-out of notebook linting and formatting by adding `*.ipynb` to [`extend-exclude`](settings.md#extend-exclude).
386+
383387
To opt in to linting and formatting Jupyter Notebook (`.ipynb`) files, add the `*.ipynb` pattern to
384388
your [`extend-include`](settings.md#extend-include) setting, like so:
385389

ruff.schema.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)